關於線性表中棧的順序儲存和鏈式儲存方式的實現方式
阿新 • • 發佈:2021-10-14
資料結構五
一、關於線性表中棧的順序儲存和鏈式儲存方式的實現方式
1.棧的順序儲存
#include "stdio.h" #include "stdlib.h" #define MaxSize 10 //定義棧中元素的最大個數 typedef struct { int data[MaxSize]; //靜態陣列存放棧中元素 int top;//棧頂指標 }SqStack; //判斷棧是否為空 int EmptyStack(SqStack* s){ if(s->top==-1) return 1; else return 0; } void InitStack(SqStack* s){ s->top=-1;//初始化棧頂指標 } //新元素入棧 int Push(SqStack* s,int x){ if(s->top==MaxSize-1) return 0; s->top++; s->data[s->top]=x; return 1; } //出棧操作 int Pop(SqStack* s,int* x){ if(s->top==-1) return 0; *x=s->data[s->top]; s->top--; return 1; } int GetTop(SqStack* s,int* x){ if(s->top==-1)return 0; *x=s->data[s->top]; return 1; } int main(){ //1.宣告一個棧 SqStack s; //2.初始化棧 InitStack(&s); //3.判斷棧是否為空 int IsEmpty= EmptyStack(&s); printf("The sequence-stack is %s.\n",IsEmpty?"empty":"not empty"); //4.插入元素 if(Push(&s,2)){ printf("The sequence-stack inserts one data,and data which is inserted is %d\n",s.data[s.top]); }else{ printf("The sequence-stack doesn't insert one data,and sequence-stack is full!\n"); } Push(&s,5); //5.刪除一個元素 int x=-1; if(Pop(&s,&x)){ printf("The sequence-stack deletes one data,and data which is deleted is %d.\n",x); }else{ printf("The sequence-stack doesn't delete one data,and sequence-stack is empty!\n"); } //獲取棧頂元素 if(GetTop(&s,&x)){ printf("The sequence-stack reads top,and top data is %d.\n",x); }else{ printf("The sequence-stack doesn't read top,and sequence-stack is empty!\n"); } return 0; }
實現結果:
D:\project\clion\ch1\cmake-build-debug\sequential_stack.exe The sequence-stack is empty. The sequence-stack inserts one data,and data which is inserted is 2 The sequence-stack deletes one data,and data which is deleted is 5. The sequence-stack reads top,and top data is 2. Process finished with exit code 0
2.棧的鏈式儲存
#include "stdlib.h" #include "stdio.h" struct LinkNode{ int data; struct LinkNode* next; }; //建立一個節點 struct LinkNode* CreateNode(int data){ struct LinkNode* p=(struct LinkNode*) malloc(sizeof(struct LinkNode)); if(!p){ printf("No enough memory to allocate!\n"); exit(0); } p->data=data; p->next=NULL; return p; } //插入一個節點(入棧) struct LinkNode* Push(struct LinkNode* L,int x){ struct LinkNode* p=L,*s=NULL; s= CreateNode(x); if(p->next!=NULL) s->next=p->next; p->next=s; return L; } //刪除一個節點(出棧) struct LinkNode* Pop(struct LinkNode* L,int* x){ struct LinkNode* p=L,*pr=NULL;//p是頭節點 *x=p->next->data;//獲取資料 pr=p->next; if(p->next!=NULL){ p->next=pr->next; free(pr); } return L; } //讀取棧頂元素 int GetTop(struct LinkNode* L,int* x){ struct LinkNode* p=L->next; if(!p) return 0; *x=p->data; return 1; } //棧的判空 int EmptyStack(struct LinkNode* L){ return L->next==NULL; } int main(){ int x= -1; //1.建立頭指標 struct LinkNode* L=NULL; //2.建立頭節點 L= CreateNode(0); //棧的判空 printf("The linked-stack is %s\n", EmptyStack(L)?"empty":"not empty"); //3.插入元素 L= Push(L,2); //棧的判空 printf("The linked-stack is %s\n", EmptyStack(L)?"empty":"not empty"); L= Push(L,5); //讀棧頂 if(GetTop(L,&x)){ printf("The top of linked-stack is %d\n",x); }else{ printf("The linked-stack is empty!\n"); } //4.刪除元素 L= Pop(L,&x); printf("The linked-stack deletes top of stack,and data which is deleted is %d\n",x); //讀棧頂 if(GetTop(L,&x)){ printf("The top of linked-stack is %d\n",x); }else{ printf("The linked-stack is empty!\n"); } //刪除棧頂 L= Pop(L,&x); printf("The linked-stack deletes top of stack,and data which is deleted is %d\n",x); //5.讀取棧頂元素 if(GetTop(L,&x)){ printf("The top of linked-stack is %d\n",x); }else{ printf("The linked-stack is empty!\n"); } //6.判斷棧是否為空 printf("The linked-stack is %s\n", EmptyStack(L)?"empty":"not empty"); return 0; }
實現結果:
D:\project\clion\ch1\cmake-build-debug\linked_stack.exe
The linked-stack is empty
The linked-stack is not empty
The top of linked-stack is 5
The linked-stack deletes top of stack,and data which is deleted is 5
The top of linked-stack is 2
The linked-stack deletes top of stack,and data which is deleted is 2
The linked-stack is empty!
The linked-stack is empty
Process finished with exit code 0