欢迎来到天天文库
浏览记录
ID:12426201
大小:19.75 KB
页数:3页
时间:2018-07-17
《将单向链表reverse,如abcd变成dcba,只能搜索链表一次。》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、2将单向链表reverse,如ABCD变成DCBA,只能搜索链表一次。#include"stdio.h"#include"string.h"#include"stdlib.h"#defineElemTypechar#defineSTACK_INIT_SIZE100#defineSTACKINITSIZE10typedefstructnode{ElemTypedata;structnode*next;}Lnode,*LinkList;LinkListGreatLink(intn){LinkListp,q,List=NULL;ElemTypee;inti;for(i=0;i2、LinkList)malloc(sizeof(Lnode));scanf("%d",&e);p->data=e;p->next=NULL;if(!List)List=p;elseq->next=p;q=p;}returnList;}voidInsertLink(LinkList*l,LinkListq,inte){LinkListp;p=(LinkList)malloc(sizeof(Lnode));p->data=e;p->next=NULL;if(!*l)*l=p;elseq->next=p;}typedefstruct{ElemType*base;ElemType*top;intsta3、ck_size;}sqStack;voidInitStack(sqStack*s){s->base=s->top=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));if(!s->base)exit(0);s->stack_size=STACK_INIT_SIZE;}voidPush(sqStack*s,inte){if(s->top-s->base>=s->stack_size){s->base=(ElemType*)realloc(s->base,(s->stack_size+STACKINITSIZE)*sizeof(ElemType4、));if(!s->base)exit(0);s->top=s->base+s->stack_size;s->stack_size=s->stack_size+STACKINITSIZE;}*(s->top)=e;s->top++;}voidPop(sqStack*s,ElemType*e){if(s->top==s->base)return;*e=*--(s->top);}intStackLen(sqStacks){return(s.top-s.base);}voidmain(){LinkListl,q;sqStacks;ElemTypee;intlen;inti;InitStack(&s5、);q=l=GreatLink(1);scanf("%c",&e);while(e!='#'){InsertLink(&l,q,e);q=q->next;scanf("%c",&e);}q=l;printf("theLinkListis");while(q){Push(&s,q->data);q=q->next;}len=StackLen(s);for(i=0;i
2、LinkList)malloc(sizeof(Lnode));scanf("%d",&e);p->data=e;p->next=NULL;if(!List)List=p;elseq->next=p;q=p;}returnList;}voidInsertLink(LinkList*l,LinkListq,inte){LinkListp;p=(LinkList)malloc(sizeof(Lnode));p->data=e;p->next=NULL;if(!*l)*l=p;elseq->next=p;}typedefstruct{ElemType*base;ElemType*top;intsta
3、ck_size;}sqStack;voidInitStack(sqStack*s){s->base=s->top=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));if(!s->base)exit(0);s->stack_size=STACK_INIT_SIZE;}voidPush(sqStack*s,inte){if(s->top-s->base>=s->stack_size){s->base=(ElemType*)realloc(s->base,(s->stack_size+STACKINITSIZE)*sizeof(ElemType
4、));if(!s->base)exit(0);s->top=s->base+s->stack_size;s->stack_size=s->stack_size+STACKINITSIZE;}*(s->top)=e;s->top++;}voidPop(sqStack*s,ElemType*e){if(s->top==s->base)return;*e=*--(s->top);}intStackLen(sqStacks){return(s.top-s.base);}voidmain(){LinkListl,q;sqStacks;ElemTypee;intlen;inti;InitStack(&s
5、);q=l=GreatLink(1);scanf("%c",&e);while(e!='#'){InsertLink(&l,q,e);q=q->next;scanf("%c",&e);}q=l;printf("theLinkListis");while(q){Push(&s,q->data);q=q->next;}len=StackLen(s);for(i=0;i
此文档下载收益归作者所有