Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数据结构之链栈 #13

Open
jyzwf opened this issue Jul 23, 2017 · 0 comments
Open

数据结构之链栈 #13

jyzwf opened this issue Jul 23, 2017 · 0 comments

Comments

@jyzwf
Copy link
Owner

jyzwf commented Jul 23, 2017

链栈的即有一个头结点,然后依次在头结点后面插入节点,形成链栈
image

基本操作有:

  1. initStack 初始化链栈
  2. stackEmpty 判断链栈是否为空
  3. pushStack 进栈
  4. popStack 退栈
  5. getTop 取栈顶元素
  6. stackLength 获取栈中元素长度
  7. destortStack 销毁链栈

主文件

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

#include "linkStack.h"

int main()
{
       LinkStack S;
       LinkStack s;

       char ch[50],e,*p;

       initStack(&S);
       printf("请输入进栈的字符;\n");
       gets(ch);

       p = &ch[0];

       while(*p)
       {
              pushStack(S,*p);
              p++;
       }

       printf("显示当前栈顶元素:\n");
       getTop(S,&e);
       printf("%4c\n",e);

       printf("输出栈中元素的个数: %d \n",stackLength(S));

       printf("元素出栈的序列:\n");

       while(!stackEmpty(S))
       {
              popStack(S,&e);
              printf("%4c\n",e);
       }

       printf("\n");
       return 0;
}

头文件

#ifndef LINKSTACK_H_INCLUDED
#define LINKSTACK_H_INCLUDED

struct node
{
       char data;
       struct node *next;
};

typedef struct node LStackNode,*LinkStack;

// 初始化链栈
void initStack(LinkStack *top)
{
       if((*top = (LinkStack)malloc(sizeof(LStackNode)))==NULL)
              exit(-1);
       (*top)->next = NULL;
}

// 判断链栈是否为空
int stackEmpty(LinkStack top)
{
       if(top->next==NULL)
              return 1;
       return 0;
}



// 进栈
int pushStack(LinkStack top,char e)
{
       LinkStack p;
       if((p=(LinkStack)malloc(sizeof(LStackNode)))==NULL)     // 节点指针
       {
              printf("内存分配失败");
              exit(-1);
       }
       p->data = e;
       p->next = top->next;
       top->next = p;
       return 1;
}


// 退栈
int popStack(LinkStack top,char *e)
{
       LinkStack p;
       p=top->next;
       if(!p)
       {
              printf("栈为空");
              return 0;
       }

       top->next=p->next;
       *e = p->data;
       free(p);
       return 1;
}

// 取栈顶元素
int getTop(LinkStack top,char *e)
{
       LinkStack p;
       p = top->next;
       if(!p)
       {
              printf("栈为空");
              return 0;
       }
       *e = p->data;
       return 1;
}


// 获取栈中元素长度
int stackLength(LinkStack top)
{
       LinkStack p;
       int count=0;

       p = top->next;
       while(p!=NULL)
       {
              count+=1;
              p = p->next;
       }

       return count;
}


// 销毁链栈
void destortStack(LinkStack top)
{
       LinkStack p,q;
       p = top;
       while(p)
       {
              q=p;
              p = p->next;
              free(q);
       }

}

#endif // LINKSTACK_H_INCLUDED

结果

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant