2.1.1链栈的设计与实现(不推荐)



由于写链栈时用的是base向top指向,导致时间增加,虽然也能实现,但是看着特蛋疼,推荐看2.1.2的链栈的设计与实现

#include <stdlib.h>#include <stdio.h>#define Stack_Length 6#define OK 1#define ERROR 0typedef int SElemType;typedef struct SNode{SElemType data;struct SNode *next;}SNode, *LinkStack;void CreateTwo(LinkStack &head, LinkStack &top, int n){int i;SNode *p;head = (LinkStack)malloc(sizeof(SNode));head->next = NULL;top = head;printf("Please input the data for LinkList Nodes:\n");for(i = n; i > 0; i–){p = (SNode*)malloc(sizeof(SNode));scanf("%d", &p->data);top->next = p;top = p;}p->next = NULL;}int Push(LinkStack &top, SElemType e){SNode *q;q = (LinkStack)malloc(sizeof(SNode));if(!q){printf("Overflow\n");return ERROR;}q->data = e;top->next = q;top = q;top->next = NULL;return OK;}int Pop(LinkStack &base, LinkStack &top, SElemType &e){SNode *q;top = base;if(!base->next) { printf("ERROR\n"); return ERROR; }else{while(top->next->next){top = top->next;}e = top->next->data;q = top->next;top->next = q->next;//把q->next指向的空给top->next;free(q);}return(OK);}//***********测试程序********************//int main(){int e;LinkStack base;LinkStack top;//测试能否建立链表CreateTwo(base, top, 3);LinkStack p;printf("\nThe old LinkStack is(bottom to top):\n");p = base;while(p->next){p = p->next;printf("%d ",p->data);}printf("\nPlease input the data to push:");scanf("%d", &e);//测试入栈功能if(Push(top, e))printf("success to push");top = base;printf("\nThe new LinkStack is:\n");while(top->next){top = top->next;printf("%d ", top->data);}printf("\n");//测试出栈功能if(Pop(base, top, e)) printf("Pop succeed!\n");printf("The poped is %d\n", e);return 0;}

,人之所以能,是相信能。

2.1.1链栈的设计与实现(不推荐)

相关文章:

你感兴趣的文章:

标签云: