//鏈堆疊操作(帶頭節點)
#include <stdio.h>
#include <malloc.h>
typedef struct LinkStack{
int data;
struct LinkStack* next;
}Stack,*LStack;
Stack * InitStack(LStack *); //初始化
void Push(LStack , int); //入堆疊
void Pop(LStack); //出堆疊
void output(LStack); //遍歷
int main(void){
LStack S;
InitStack(&S);
if(S->next == NULL){
printf("初始化成功!\n");
}else{
printf("初始化失敗!\n");
}
Push(S,1);
Push(S,2);
Push(S,3);
Push(S,4);
Push(S,5);
printf("當前鏈堆疊中的資料是:");
output(S);
printf("\n");
Pop(S);
Pop(S);
Pop(S);
printf("當前鏈堆疊中的資料是:");
output(S);
return 0;
}
//初始化
Stack * InitStack(LStack *S){
(*S) = (Stack *)malloc(sizeof(Stack));
(*S)->next = NULL;
return (*S);
}
//入堆疊
void Push(LStack S , int e){
Stack *p = (Stack *)malloc(sizeof(Stack));
p->data = https://www.cnblogs.com/Timesi/p/e;
p->next = S->next;
S->next = p;
}
//出堆疊
void Pop(LStack S){
Stack *p = S->next;
int e;
if(S->next != NULL){
e = p->data;
printf("當前彈出資料是:%d/n",e);
S->next = p->next;
printf("當前堆疊頂資料是:%d/n",S->next->data);
free(p);
}else{
printf("堆疊為空!/n");
}
}
//遍歷
void output(LStack S){
Stack *p = S->next;
if(p == NULL){
printf("堆疊為空!");
}else{
while(p != NULL){
printf("%d ",p->data);
p = p->next;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/77839.html
標籤:其他
