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



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

#include <stdlib.h>
#include <stdio.h>

#define Stack_Length 6
#define OK 1
#define ERROR 0

typedef 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;
}
时间: 2024-08-30 16:45:14

2.1.1链栈的设计与实现(不推荐)的相关文章

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

链表栈的基本操作包括栈的建立.求长度.取栈顶元素.入栈.出栈.判断栈是否空等具体操作. //调试环境:DevC++ //库文件和预设定义 #include <stdio.h> #include <stdlib.h> #define Stack_Length 6 #define OK 1 #define ERROR 0 typedef int SElemType; //存储形式 typedef struct SNode { SElemType data; struct SNode *

【算法设计-链栈和链队列】链栈和链队列的实现

1.链队列.利用带有头结点的单链表来实现链队列,插入和删除的复杂度都为o(1) 代码: #include<stdio.h> #include<stdlib.h> typedef struct Qnode { int data; Qnode *next; }Qnode; typedef struct LinkQueue { Qnode *front; Qnode *rear; }LinkQueue; void initialize(LinkQueue *LinkQueue) { Li

数据结构4_链栈

用指针方式实现栈,相对于顺序栈,指针方式不用指定栈的大小, 先定义一个栈节点类,再定义一个链栈类,为使链栈类能访问栈节点的元素,设链栈类为栈节点类的友元类. #include<iostream>using namespace std;class LinkStack;class StackNode   //设计每个节点的类型{    char *data;    StackNode *next;    friend class LinkStack;   //设置友元类,以便LinkStack类访

链栈的实现

链栈即链式栈,也就是说我们不用再考虑空间的大小,可随心所欲的进行数据的插入/删除了.和顺序栈一样,仍然要保持其stack的特性,只在一端进行插入和删除,后进先出. 示例代码: #ifndef _LINKSTACK_H #define _LINKSTACK_H typedef int ElemType; typedef int Status; typedef struct linkStack { ElemType data; struct linkStack * top; }linkStack;

堆栈(链栈)

#include<cstdlib> #include<iostream> #include<fstream> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; typedef char SElemType; typedef struct StackNode{ SElemType data; struct StackNode *next; }S

链栈和链队列的类实现

#include<iostream>#include<cassert> using namespace std; template <class T>//链栈 struct LinkNode{T data;LinkNode<T> *Link;LinkNode(LinkNode<T> *pr=NULL){Link=pr;}LinkNode(const T& item,LinkNode<T> *pr=NULL){data=item

数据结构(java语言描述)链栈的定义

1.定义栈接口 package stack;public interface Istack {    public void clear();    public boolean isEmpty();    public int length();    public Object peek();    public void push(Object x) throws Exception;    public Object pop();} 2.定义Node结点 package stack; i

链栈-书上习题

元素的入栈出栈 链栈: 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 struct stack 5 { 6 char data; 7 stack *next; 8 }; 9 stack* initstack() /*为栈头指针申请内存*/ 10 { 11 stack *s=new stack; 12 s->next=NULL; 13 return s; 14 } 15 int isempt

java实现链栈

前面学习了java实现顺序栈:http://www.cnblogs.com/lixiaolun/p/4644134.html 接下来,学习java实现链栈. 链栈类代码: package linkedstack; public class LinkStack { private Element base; private Element top; class Element { public Object data; public Element next; } /** * 初始化栈 * */