C語言實現鏈棧
阿新 • • 發佈:2018-12-30
link sca 函數 print urn max efi fin size
我自己寫的代碼部分:
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; 42free(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; }
C語言實現鏈棧