C语言实现链栈
我自己写的代码部分:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 typedef struct node 5 { 6 char data; 7 struct node *next; 8 }Lstack,*Lpstack; 9 10 void initStack(Lstack **top) 11 { 12 if(((*top)=(Lstack *)malloc(sizeof(Lstack)))==NULL) 13 exit(-1); 14 (*top)->next=NULL; 15 } 16 17 int Push(Lstack *top,char e) 18 { 19 Lstack *p; 20 if((p=(Lstack *)malloc(sizeof(Lstack)))==NULL) 21 { 22 printf("分配内存失败!\n"); 23 exit(-1); 24 return 0; 25 } 26 p->next=top->next; 27 p->data=e; 28 top->next=p; 29 } 30 /* 31 //取出栈顶的元素,并将该栈顶元素弹出去 32 char Pop(Lstack *top,char e) 33 { 34 Lstack *p; 35 p=top->next; 36 if(p==NULL){ 37 printf("内存已空!\n"); 38 exit(0); 39 } 40 e=p->data; 41 top->next=p->next; 42 free(p); 43 return e; 44 } 45 //主函数中遍历栈中元素则可更改为 46 while(s->next){ 47 printf("%c ",Pop(s,e)); 48 } 49 */ 50 int Pop(Lstack *top,char *e) 51 { 52 Lstack *p; 53 p=top->next; 54 if(p==NULL){ 55 printf("内存已空!\n"); 56 return 0; 57 } 58 e=p->data; 59 top->next=p->next; 60 free(p); 61 return 1; 62 } 63 64 int getLength(Lstack *top) 65 { 66 int cnt=0; 67 Lstack *p=top; 68 while(p->next!=NULL){ 69 p=p->next; 70 cnt++; 71 } 72 return cnt; 73 } 74 char getTop(Lstack *top,char e) 75 { 76 Lstack *p; 77 p=top->next; 78 if(p==NULL){ 79 printf("栈已空!\n"); 80 return 0; 81 } 82 e=p->data; 83 return e; 84 } 85 void clear(Lstack *top) 86 { 87 Lstack *p,*q; 88 p=top; 89 while(!p){ 90 q=p; 91 p=p->next; 92 free(q); 93 } 94 } 95 int main() 96 { 97 char e; 98 char a[15]; 99 int i,len; 100 Lstack *s; 101 scanf("%s",a); 102 initStack(&s); 103 len=strlen(a); 104 for(i=0;i<len;i++){ 105 Push(s,a[i]); 106 } 107 printf("len = %d\n",getLength(s)); 108 printf("弹出栈顶元素! \n"); 109 Pop(s,&e); 110 printf("len = %d\n",getLength(s)); 111 112 printf("取得栈顶元素:%c\n",getTop(s,e)); 113 114 printf("Clear Stack!\n"); 115 clear(s); 116 printf("len = %d\n",getLength(s)); 117 118 while(s->next){ 119 printf("%c ",getTop(s,e)); 120 Pop(s,&e); 121 } 122 printf("len = %d\n",getLength(s)); 123 return 0; 124 }
参考别人的代码:
#include<string.h> #include<stdio.h> #include<malloc.h> #include<stdlib.h> #define maxsize 100 typedef struct node { char data; struct node *next; }lnode ,*linkstack; void init(linkstack *top) { if(((*top)=(linkstack)malloc(sizeof(lnode)))==NULL )//(给*top分配一个存储空间让top指向这个空间) exit(-1); (*top)->next=NULL; } /* int empty(linkstack top) { if(top->next==NULL) return 1; else return 0; } */ char get(linkstack top,char e) { lnode *p; p=top->next; if(!p) { printf("Stack Empty!"); exit(0); } else { e=p->data; } return e; } int push(linkstack top,char e) { lnode *p; if( (p=(linkstack)malloc(sizeof(lnode)))==NULL )//(给*top分配一个存储空间让top指向这个空间) { printf("分配内存失败"); exit(-1); return 0; } p->data=e; p->next=top->next; top->next=p; return 1; } int pop(linkstack top,char *e) { linkstack p=top->next; if(p==NULL) { printf("栈已空!"); return 0; } top->next=p->next; *e=p->data; free(p); return 1; } int length(linkstack top) { int i=0; lnode *p=top; while(p->next!=NULL) { p=p->next; i++; } return i; } void clear(linkstack top) { lnode *p,*q; p=top; while(!p) { q=p; p=p->next; free(q); } } //形参有*代表是一个指针,那么传递实参的时候可以传递指针变量,此时直接用指针变量名; //或者传递普通变量的地址,此时用取地址符&+变量名。 int main() { linkstack s; int i,len; char a[100]; scanf("%s",a); len=strlen(a); char e; init(&s); printf("将数组中的元素依次入栈!\n"); for(i=0;i<len;i++) { push(s,a[i]); } printf("栈顶元素:%c\n",get(s,e)); printf("将f入栈\n"); push(s,'f'); printf("将g入栈\n"); push(s,'g'); printf("栈中元素个数为:%d\n",length(s)); printf("将栈顶出栈:\n"); pop(s,&e); printf("将栈顶出栈:\n"); pop(s,&e); printf("将栈顶出栈:\n"); pop(s,&e); printf("栈中元素个数为:%d\n",length(s)); printf("出栈元素的序列:"); while(s->next) { pop(s,&e); printf("%c ",e); } printf("\n"); return 0; }