欢迎来到天天文库
浏览记录
ID:37752501
大小:15.56 KB
页数:4页
时间:2019-05-30
《利用顺序栈将带头结点的单链表(a1,a2,…,an)逆置为(an,an-1,…,a1)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#includetypedefintSElemType;typedefstructnode{intdata;structnode*next;}linklist;linklist*head;#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstruct{SElemType*base;SElemType*top;intstacksize;}SeqStack;SeqStack*S;linklist*creatlist(){linklist*p,*q;intn=0;q=p=(structnode*
2、)malloc(sizeof(linklist));head=p;p->next=NULL;p=(structnode*)malloc(sizeof(linklist));scanf("%d",&p->data);while(p->data!=-1){n=n+1;q->next=p;q=p;p=(structnode*)malloc(sizeof(linklist));scanf("%d",&p->data);}q->next=NULL;return(head);}voidprint(linklist*head){linklist*p;p=head->next;if
3、(p==NULL)printf("thisisanemptylist.");else{do{printf("%6d",p->data);p=p->next;}while(p!=NULL);printf("");}}intInitStack(SeqStack*S){S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S->base)return0;S->top=S->base;S->stacksize=STACK_INIT_SIZE;return1;}intpush(SeqStac
4、k*S,SElemTypee){if(S->top-S->base>=S->stacksize){S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));if(!S->base)return0;S->top=S->base+S->stacksize;S->stacksize+=STACKINCREMENT;}*S->top++=e;return1;}intPop(SeqStack*S){if(S->top==S->base)return0;--S->to
5、p;return*S->top;}intstackempty(SeqStack*S){if(S->top==S->base)return1;elsereturn0;}linklist*backlinklist(linklist*head){linklist*p;p=head->next;InitStack(S);while(p){if(push(S,p->data));p=p->next;}p=head->next;while(!stackempty(S)){p->data=Pop(S);p=p->next;}return(head);}voidmain(){lin
6、klist*head;head=creatlist();print(head);head=backlinklist(head);print(head);}
此文档下载收益归作者所有