清华大学出版社 TSINGHUA UNIVERSITY PRESS #includestdlib h #includestdio h struct node *定义结点类型* i int d struct node next main t int x; struct node *head, *p, *q; head=NULL;/*置链表空来 q==NULL; scanf(d,&x);/*输入一个正整数*/
#include "stdlib.h" #include "stdio.h" struct node /*定义结点类型*/ { int d; struct node *next; } main() { int x; struct node *head, *p, *q; head=NULL; /*置链表空*/ q=NULL; scanf(“%d” , &x); /*输入一个正整数*/
清华大学出版社 TSINGHUA UNIVERSITY PRESS while(x>0)/*若输入值大于0*/ p=( struct node*) malloc( sizeof( struct node));/*申请一个结点*/ p-〉d=x;p->next=NUL:;/*置当前结点的数据域和指针域* f(head==NULL)head=p;/*若链表空,则将头指针指向当前结点p米 else g->next=p /*将当前结点链接在最后* q=p;/*置当前结点为链表最后一个结点 canf (%d",&x p=head while(p!=NULL)/米从链表第一个结点开始打印各结点元素值,并删除* printf("%5d",p->d);/*打印当前结点中的数据*/ q=p;p=p->next;free(q);/*删除当前结点并释放该结点空间* printf( \n")
while(x>0) /*若输入值大于0*/ { p=(struct node *)malloc(sizeof(struct node));/*申请一个结点*/ p->d=x; p->next=NULL; /*置当前结点的数据域和指针域*/ if(head==NULL) head=p;/*若链表空,则将头指针指向当前结点p*/ else q->next=p; /*将当前结点链接在最后*/ q=p; /*置当前结点为链表最后一个结点*/ scanf("%d", &x); } p=head; while(p!=NULL) /*从链表第一个结点开始打印各结点元素值,并删除*/ { printf("%5d", p->d); /*打印当前结点中的数据*/ q=p;p=p->next;free(q);/*删除当前结点并释放该结点空间*/ } printf("\n"); }
清华大学出版社 TSINGHUA UNIVERSITY PRESS 双向链表 HEAD 0
双向链表
清华大学出版社 TSINGHUA UNIVERSITY PRESS 3.1.3带链的栈 top a
3.1.3 带链的栈
清华大学出版社 TSINGHUA UNIVERSITY PRESS ●可利用栈及其运算 TOP a)将结点p送回可利用栈 p TOP 0 0b)在从可利用栈取得一个结点p
●可利用栈及其运算