数据结构--链栈的实现

来源:互联网 发布:最心酸的一句话知乎 编辑:程序博客网 时间:2024/06/06 23:45

1)栈是一种先进后出的数据结构,这就意味着它的插入和删除都只能在一端进行。可以把栈想象成一端开口的井,所有元素压栈出出栈都是从井口进入,出去。

2)用链表实现栈,通常保留头结点,然后进栈就是在头结点处插入一个结点,出栈就是在头结点处删除一个结点。


结构体声明和函数声明:

typedef struct Node{int data;struct Node *next;} Stack, *Stack_p;Stack_p Initialization(void);Stack_p Is_empty(Stack_p top);Stack_p Push(Stack_p top, int data);void Pop(Stack_p top);void Show_top(Stack_p top);void Output(Stack_p top);

初始化空栈

初始化空栈首先为链表头结点分配内存,然后将头结点的指针域指向NULL,表示这是一个空栈。

Stack_p Initialization(void) //初始化栈 {Stack_p top;top = (Stack_p)malloc(sizeof(Stack));top->next = NULL; //置为NULL, 空栈return top;}


判空栈

根据头结点的指针域指向NULL为空栈,所以只要判断头指针的下一个结点是否为NULL就可以判断是否是空栈

Stack_p Is_empty(Stack_p top) //判空栈 {if (top->next == NULL){return NULL;}}

元素进栈

元素进栈顶其实就是在链表头结点的下一个位置插入一个元素,因为链表没有空间限制,所以不同事先判断是否满栈。

Stack_p Push(Stack_p top, int data) //元素进栈 {Stack_p new_element;new_element = (Stack_p)malloc(sizeof(Stack));new_element->data = data;new_element->next = top->next;top->next = new_element;return top;}

元素出栈

元素出栈首先要判断是否是空栈,对一个空栈执行出栈操作因引发错误。而且删除的结点要记得释放它的内存,否则会引发内存泄漏的问题。

和进栈一样,出栈就是将头结点的下一个结点删除。

void Pop(Stack_p top) //元素出栈 {Stack_p delete_element;if (Is_empty(top) != NULL){delete_element = top->next;top->next = delete_element->next;printf("元素%d已出栈\n\n", delete_element->data);free(delete_element);}else printf("\n这是一个空栈\n\n");}

全部代码:

#include <stdio.h>#include <stdlib.h>//这个例程保留栈顶指针,栈顶指针没有数据 typedef struct Node{int data;struct Node *next;} Stack, *Stack_p;Stack_p Initialization(void);Stack_p Is_empty(Stack_p top);Stack_p Push(Stack_p top, int data);void Pop(Stack_p top);void Show_top(Stack_p top);void Output(Stack_p top); int main(void){int option;Stack_p top;while (1){printf("1.初始化栈\n2.元素进栈\n3.元素出栈\n4.判空栈\n5.取栈顶元素\n6.出栈顺序\n7.退出\n");printf("选择:");scanf("%d",&option );switch (option){case 1: top = Initialization(); break;case 2: printf("输入进栈元素:");scanf("%d", &option);top = Push(top, option);break;case 3: Pop(top);break;case 4: if (Is_empty(top) == NULL){printf("\n是空栈\n\n");}else printf("\n不是空栈\n\n");break;case 5: Show_top(top);break;case 6: Output(top);break;case 7: return 0;}}return 0;}Stack_p Initialization(void) //初始化栈 {Stack_p top;top = (Stack_p)malloc(sizeof(Stack));top->next = NULL;return top;}Stack_p Is_empty(Stack_p top) //判空栈 {if (top->next == NULL){return NULL;}}Stack_p Push(Stack_p top, int data) //元素进栈 {Stack_p new_element;new_element = (Stack_p)malloc(sizeof(Stack));new_element->data = data;new_element->next = top->next;top->next = new_element;return top;}void Pop(Stack_p top) //元素出栈 {Stack_p delete_element;if (Is_empty(top) != NULL){delete_element = top->next;top->next = delete_element->next;printf("元素%d已出栈\n\n", delete_element->data);free(delete_element);}else printf("\n这是一个空栈\n\n");}void Show_top(Stack_p top) //取栈顶元素 {if (Is_empty(top) == NULL){printf("\n空栈,没有栈顶元素\n\n");}else{printf("\n栈顶元素为%d\n\n", top->next->data);}}void Output(Stack_p top) //出栈顺序 {Stack_p temp;for (temp = top->next; temp != NULL; temp = temp->next){printf("%d ", temp->data);}printf("\n\n");}