資料結構-鏈棧
阿新 • • 發佈:2019-01-02
#include<stdio.h>
#include<malloc.h>
#include<error.h>
/*
* 頭節點為空
* */
typedef int datatype;
typedef struct _node_
{
datatype data;
struct _node_ *next;
}linknod,*linkstack;
linkstack creat_empty_linkstack()
{
linkstack l;
l=(linkstack)malloc(sizeof(linknod));
l->next=NULL;
return l;
}
int empty_linkstack(linkstack l)
{
return NULL==l->next;
}
int push_linkstack(linkstack h,datatype x)
{
linkstack q;
q=(linkstack)malloc(sizeof(linknod));
q->data=x;
q->next=h->next;
h->next=q;
return 1;
}
datatype pop_linkstack(linkstack h)
{
linkstack q;
datatype temp;
q=h;
q=h->next;
h->next=q->next;
temp=q->data;
free(q);
return temp;
}
datatype get_pop(linkstack h)
{
return h->next->data;
}
int clear_linkstack(linkstack h)
{
linkstack q;
if(empty_linkstack(h))
{
return 0;
}
else
{
q=h->next;
while(q!=NULL)
{
h->next=q->next;
q=h->next;
free(q);//釋放指標所指到記憶體空間
}
q=NULL;
return 1;
}
}
int lenth_linkstack(linkstack h)
{
int len=0;
linkstack q;
if(empty_linkstack(h))
{
return 0;
}
else
{
q=h->next;
while(q!=NULL)
{
len++;
q=q->next;
}
return len;
}
}
int main(int argc,char *argv[])
{
linkstack l;
l=creat_empty_linkstack();
printf("清空:%d\n",clear_linkstack(l));
push_linkstack(l,200);
printf("棧頂指標:%d\n",get_pop(l));
push_linkstack(l,300);
printf("棧頂指標:%d\n",get_pop(l));
printf("lenth:%d\n",lenth_linkstack(l));
printf("清空:%d\n",clear_linkstack(l));
push_linkstack(l,200);
printf("棧頂指標:%d\n",get_pop(l));
printf("入棧資料:%d\n",l->next->data);
printf("判斷棧是否為空:%d\n",empty_linkstack(l));
printf("出棧資料:%d\n",pop_linkstack(l));
printf("判斷棧是否為空:%d\n",empty_linkstack(l));
return 0;
}
#include<malloc.h>
#include<error.h>
/*
* 頭節點為空
* */
typedef int datatype;
typedef struct _node_
{
datatype data;
struct _node_ *next;
}linknod,*linkstack;
linkstack creat_empty_linkstack()
{
linkstack l;
l=(linkstack)malloc(sizeof(linknod));
l->next=NULL;
return l;
}
int empty_linkstack(linkstack l)
{
return NULL==l->next;
}
int push_linkstack(linkstack h,datatype x)
{
linkstack q;
q=(linkstack)malloc(sizeof(linknod));
q->data=x;
q->next=h->next;
h->next=q;
return 1;
}
datatype pop_linkstack(linkstack h)
{
linkstack q;
datatype temp;
q=h;
q=h->next;
h->next=q->next;
temp=q->data;
free(q);
return temp;
}
datatype get_pop(linkstack h)
{
return h->next->data;
}
int clear_linkstack(linkstack h)
{
linkstack q;
if(empty_linkstack(h))
{
return 0;
}
else
{
q=h->next;
while(q!=NULL)
{
h->next=q->next;
q=h->next;
free(q);//釋放指標所指到記憶體空間
}
q=NULL;
return 1;
}
}
int lenth_linkstack(linkstack h)
{
int len=0;
linkstack q;
if(empty_linkstack(h))
{
return 0;
}
else
{
q=h->next;
while(q!=NULL)
{
len++;
q=q->next;
}
return len;
}
}
int main(int argc,char *argv[])
{
linkstack l;
l=creat_empty_linkstack();
printf("清空:%d\n",clear_linkstack(l));
push_linkstack(l,200);
printf("棧頂指標:%d\n",get_pop(l));
push_linkstack(l,300);
printf("棧頂指標:%d\n",get_pop(l));
printf("lenth:%d\n",lenth_linkstack(l));
printf("清空:%d\n",clear_linkstack(l));
push_linkstack(l,200);
printf("棧頂指標:%d\n",get_pop(l));
printf("入棧資料:%d\n",l->next->data);
printf("判斷棧是否為空:%d\n",empty_linkstack(l));
printf("出棧資料:%d\n",pop_linkstack(l));
printf("判斷棧是否為空:%d\n",empty_linkstack(l));
return 0;
}