数据结构:栈(用链表实现)
2015-05-09 21:04
260 查看
以下是完整代码:
这里想说的是typedef,typedef会“封装”出一个新的数据类型,与#define有很大的不同
/* * this file is an implementation of stack with linked list * file name: stack.c * author: John Woods * date: 2015/5/9 * statement: anyone can use this file for any purpose */ #include <stdio.h> #include <stdlib.h> #define BOOL int #define TRUE 1 #define FALSE 0 typedef struct SNode { int value; struct SNode * next; }* SNode; typedef struct Stack { int depth; SNode top; }* Stack; /* operation declaration */ void stackInit(Stack * p_myStack); void pop(Stack myStack); void push(Stack myStack); void stackClear(Stack myStack); void stackDestroy(Stack * p_myStack); BOOL isExist(Stack myStack); /* menu */ void menu(); /* entrance: main function */ int main(void) { Stack myStack = NULL; int choice; char c; while(TRUE) { menu(); while(!scanf("%d", &choice)) while('\n' != (c=getchar()) && EOF != c); switch(choice) { case 1: stackInit(&myStack); break; case 2: stackClear(myStack); break; case 3: push(myStack); break; case 4: pop(myStack); break; case 5: stackDestroy(&myStack); break; default: exit(EXIT_SUCCESS); break; } } return 0; } /* menu implementation */ void menu() { printf("\n\t****************MENU****************\n"); printf("\t* 1.initial stack 2.clear stack *\n"); printf("\t* 3.push element 4.pop element *\n"); printf("\t* 5.destroy stack 6.exit *\n"); printf("\t****************MENU****************\n"); printf("Your choice:"); } /* operation implementation */ void stackInit(Stack * p_myStack) { if(isExist(*p_myStack)) { printf("This stack is already exist, cannot initial it!\n"); return; } if(NULL == (*p_myStack = (Stack)malloc(sizeof(struct Stack)))) { printf("Out of memory!\n"); return; } (*p_myStack)->top = NULL; (*p_myStack)->depth = 0; printf("Initial successfully!\n"); } void pop(Stack myStack) { SNode pNode = NULL; int out; char c; if(!isExist(myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } if(0 == myStack->depth) { printf("This stack is empty! Cannot pop the top value!\n"); return; } while(TRUE) { out = myStack->top->value; pNode = myStack->top; myStack->top = myStack->top->next; myStack->depth--; free(pNode); pNode = NULL; printf("The value has been popped is %d\n", out); if(0 == myStack->depth) { printf("This stack is empty now, cannot continue popping!\n "); break; } while('\n' != (c=getchar()) && EOF != c); printf("Go on?(y/n):"); if('y' != (c=getchar()) && 'Y' != c) break; } } void push(Stack myStack) { SNode pNode = NULL; int value; char c; if(!isExist(myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } while(TRUE) { if(!(pNode = (SNode)malloc(sizeof(struct SNode)))) { printf("Out of memory!\n"); return; } printf("Please input the value:"); while(!scanf("%d", &value)) while('\n' != (c=getchar()) && EOF != c); pNode->value = value; pNode->next = myStack->top; myStack->top = pNode; myStack->depth++; pNode = NULL; printf("Push successfully!\n"); while('\n' != (c=getchar()) && EOF != c); printf("Go on?(y/n):"); if('y' != (c=getchar()) && 'Y' != c) break; } } void stackClear(Stack myStack) { if(!isExist(myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } SNode pNode = NULL; while(myStack->top) { pNode = myStack->top; myStack->top = myStack->top->next; free(pNode); } myStack->top = NULL; myStack->depth = 0; printf("Clear successfully!\n"); } void stackDestroy(Stack * p_myStack) { if(!isExist(*p_myStack)) { printf("This stack is not exist! Please initial it first!\n"); return; } stackClear(*p_myStack); free(*p_myStack); *p_myStack = NULL; printf("Destroy successfully!\n"); } BOOL isExist(Stack myStack) { if(NULL == myStack) return FALSE; else return TRUE; }
这里想说的是typedef,typedef会“封装”出一个新的数据类型,与#define有很大的不同
/* typedef */ typedef struct SName{ int data; } * SName; /* 若定义如下变量 */ SName temp = (SName)malloc(sizeof(struct SName)); /* 虽然 temp 实际上是个指针,但若当做参数被其它函数调用,其值仍是不会被修改的,在stack.c的stackInit()和stackDestroy()中传递参数要注意 */
相关文章推荐
- 数据结构学习笔记之链表分析与实现(三)
- 数据结构之链表c语言实现
- 数据结构_链表实现无限循环的"环"结构/循环队列
- 数据结构:双向链表实现队列与循环链表
- 数据结构---双向链表实现队列与循环链表
- 【数据结构】C语言实现单链表
- 数据结构 第二章 线性表 英语成绩表的单链表实现
- Java中链表、堆栈、队列、二叉树、散列表等数据结构的实现
- 数据结构--单链表实现队列1
- 几种简单的数据结构的C++实现,树,图,链表,环形队列,栈等---环形队列篇
- 【数据结构】C语言实现无头节点单链表
- (转)java实现基本数据结构(堆,栈,链表)——推荐后面的链表扩展部分
- Java数据结构之链表的实现
- 数据结构_链表_多项式求导_C++实现
- 【数据结构】c语言链表实现报数问题
- 数据结构-链表 JAVA语言实现
- 数据结构---栈的链表实现
- 数据结构复习——线性表的链式存储实现(单向链表)
- 数据结构---单链表实现(源码)
- 严蔚敏版数据结构课本代码——链表结构实现算法2.2(BUCT-JK1602-LLP)