/**
***@Title :考研數據結構複習
***@Subject :棧(鏈表)
***@Author :lxfhahaha
***@language: C語言
***@Time : 2018/9/26 20:55
*****/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Inital_Size 50
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Bool;
typedef int SElemType;
typedef int Status;
typedef struct StackNode{
SElemType data;
struct StackNode *next;
}StackNode,*StackList;
//操作結果:構造一個空棧,該變量指向棧頂位置
Status InitStack(StackList *stack){
*stack=(StackList)malloc(sizeof(StackNode));
if(!(*stack)){
printf("Malloc Error!\n");
exit(0);
}
(*stack)->next=NULL;
(*stack)->data=0; //第一個,棧底
return OK;
}
//若stack爲空,返回TRUE,非空返回FALSE
Bool StackEmpty(StackList stack){
if(!stack){
printf("Error!! Please inital first!!\n");
exit(0);
}
return stack->next== NULL ? TRUE : ERROR;
}
//返回stack中數據元素個數
int StackLength(StackList stack){
if(!stack){
printf("Error!! Please inital first!!\n");
exit(0);
}
int length=0;
for(stack=stack->next;stack!=NULL;stack=stack->next,length++);
return length;
}
//若棧不空,則若e返回棧頂的元素,並返回OK,否則返回ERROR
Status GetTop(StackList stack,SElemType *e){
if(!stack){
printf("Error!! Please inital first!!\n");
exit(0);
}
if(stack->next==NULL){
*e=NULL;
return ERROR;
}
*e=stack->data;
return OK;
}
//插入元素e爲新的棧頂元素
Status Push(StackList *stack,SElemType e){
if(!(*stack)){
printf("Error!! Please inital first!!\n");
exit(0);
}
StackNode *node=(StackNode*)malloc(sizeof(StackNode));
if(!node){
printf("Malloc Error!\n");
exit(0);
}
node->data=e;
node->next=*stack;
*stack=node;
return OK;
}
//若棧不空,則刪除stack的棧頂元素,用e返回其值,並返回OK;否則返回ERROR
Status Pop(StackList *stack,SElemType *e){
if(!(*stack)){
printf("Error!! Please inital first!!\n");
exit(0);
}
if((*stack)->next==NULL){
*e=NULL;
return ERROR;
}
*e=(*stack)->data;
StackNode *p=*stack;
*stack=(*stack)->next;
free(p);
return OK;
}
//銷燬棧,stack不再存在
Status DestroyStack(StackList *stack){
if(!(*stack)) return OK; //不存在
StackNode *p = *stack;
while((*stack)->next!=NULL){
(*stack)=(*stack)->next;
free(p);
p=*stack;
}
free(*stack);
*stack=NULL;
return OK;
}
//把stack置爲空棧
Status ClearStack(StackList *stack){
if(!(*stack)) return ERROR; //不存在
StackNode *p = *stack;
while((*stack)->next!=NULL){
(*stack)=(*stack)->next;
free(p);
p=*stack;
}
return OK;
}
//從棧頂到棧底依次對棧中每個元素調用visit(),一旦visit()失敗,則操作失敗
Status StackTraverse(StackList stack,Status (*visit)(SElemType)){
if(!stack){
printf("Error!! Please inital first!!\n");
exit(0);
}
int length=StackLength(stack);
while(length--){
if(!(*visit)(stack->data))
exit(0);
stack=stack->next;
}
return OK;
}
//test1——print
Status PrintOne(SElemType one){
printf("%d <- ",one);
return OK;
}
int main()
{
StackList stack;
int i,j;
SElemType a,b,c,d;
InitStack(&stack);
srand((int)time(0));
for(i=1;i<=10;i++)
Push(&stack,(SElemType)rand()%100);
printf("[");
StackTraverse(stack, PrintOne);
printf("]");
printf("\nlength:%d\n",StackLength(stack));
GetTop(stack,&a);
printf("IsEmpty:%d Top:%d\n\n",StackEmpty(stack),a);
ClearStack(&stack);
printf("[");
StackTraverse(stack, PrintOne);
printf("]");
printf("\nlength:%d\n",StackLength(stack));
GetTop(stack,&a);
printf("IsEmpty:%d Top:%d\n\n",StackEmpty(stack),a);
for(i=1;i<=10;i++)
Push(&stack,(SElemType)rand()%100);
printf("[");
StackTraverse(stack, PrintOne);
printf("]");
printf("\nlength:%d\n",StackLength(stack));
GetTop(stack,&a);
printf("IsEmpty:%d Top:%d\n\n",StackEmpty(stack),a);
Pop(&stack, &b);
printf("Pop one:%d\n",b);
printf("[");
StackTraverse(stack, PrintOne);
printf("]");
printf("\nlength:%d\n",StackLength(stack));
GetTop(stack,&a);
printf("IsEmpty:%d Top:%d\n\n",StackEmpty(stack),a);
DestroyStack(&stack);
printf("[");
StackTraverse(stack, PrintOne);
printf("]");
return 0;
}
考研數據結構複習——棧(鏈表)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.