搜索链表一次

来源:互联网 发布:传统武术知乎 编辑:程序博客网 时间:2024/05/29 23:22
#include"stdio.h" #include"string.h" #include"stdlib.h" #define ElemType char #define STACK_INIT_SIZE 100 #define STACKINITSIZE 10 typedefstruct node { ElemType data; struct node * next; }Lnode,*LinkList; LinkList GreatLink(int n) { LinkList p,q,List=NULL; ElemType e; int i; for (i=0;idata=e; p->next=NULL; if(!List) List=p; else q->next=p; q=p; } return List; } void InsertLink(LinkList *l,LinkList q,int e) { LinkList p; p=(LinkList)malloc(sizeof(Lnode)); p->data=e; p->next=NULL; if(!*l) *l=p; else q->next=p; } typedefstruct { ElemType *base; ElemType *top; int stack_size; }sqStack; void InitStack(sqStack *s) { s->base=s->top=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(!s->base) exit(0); s->stack_size=STACK_INIT_SIZE; } void Push(sqStack *s,int e) { if(s->top-s->base>=s->stack_size) { s->base=(ElemType *)realloc(s->base,(s->stack_size+STACKINITSIZE)*sizeof(ElemType)); if(!s->base) exit(0); s->top=s->base+s->stack_size; s->stack_size=s->stack_size+STACKINITSIZE;} *(s->top)=e; s->top++; } void Pop(sqStack *s,ElemType *e) { if(s->top==s->base) return; *e=*--(s->top); } int StackLen(sqStack s) { return(s.top-s.base); } void main() { LinkList l,q; sqStack s; ElemType e; int len; int i; InitStack(&s); q=l=GreatLink(1); scanf("%c",&e); while(e!='#') { InsertLink(&l,q,e); q=q->next; scanf("%c",&e); } q=l; printf("the LinkList is"); while(q) { Push(&s,q->data); q=q->next; } len=StackLen(s); for(i=0;i<len;i++) { Pop(&s,&e); printf("%c",e); } printf("\n"); }

原创粉丝点击