LinkList_Stack(鏈式棧)

#include
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;


node *init()
{
return NULL;
}


int empty(node *top)
{
return (!top ? 1 : 0);
}


Datatype read(node *top)
{
if (!empty(top))
return top->info;
else
{
printf("棧爲空!\n");
return NULL;
}
}


void display(node *top)
{
if (!top)
printf("棧爲空,無法進行輸出!\n");
else
{
node *p = top;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}


node *insert(node *top, Datatype x)
{
node *p, *q;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (top == NULL)
top = p;
else
{
p->next = top;
top = p;
}
return top;
}




node *dele(node *top)
{
node *p = top;
if (!p)
printf("棧爲空,無法進行刪除!\n");
else
{
top = top->next;
free(p);
}
return top;
}




void main()
{
node *top;
top = init();
int i;;
for (i = 0; i<10; i++)
top = insert(top, i * 2);
display(top);
putchar('\n');
printf("棧頂結點的值爲:%d\n", top->info);
top = insert(top, 666);
printf("666進棧:");
display(top);
putchar('\n');
top= dele(top);
printf("刪除棧頂的結點後爲:");
display(top);
putchar('\n');
top = dele(top);
printf("刪除棧頂的結點後爲:");
display(top);
putchar('\n');
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章