链式栈的基本操作实现c++
2016-08-06 20:50
477 查看
注:若有错误或需要补充的内容请告知,新手上路,谢谢。
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<ctype.h>
using namespace std;
/*结点结构体*/
typedef struct node
{
int data;
struct node *next;
}stacknode;
/*链式栈结构体*/
typedef struct linkstack
{
stacknode *base;
stacknode *top;
}linkstack;
/*链式栈初始化*/
linkstack *init_stack()
{
linkstack *s;
s=(linkstack*)malloc(sizeof(linkstack));
s->base=(stacknode*)malloc(sizeof(stacknode));
s->top=s->base;
s->base->next=NULL;
return s;
}
/*判断链式栈空*/
bool is_empty_linkstack(linkstack s)
{
if(s.base==s.top)
{
return true;
}
else
{
return false;
}
}
/*链式栈入栈*/
void push(linkstack *s,int e)
{
stacknode *p;
p=(stacknode*)malloc(sizeof(stacknode));
p->data=e;
p->next=s->top;
s->top=p;
}
/*链式栈出栈*/
void pop(linkstack *s,int &e)
{
if(is_empty_linkstack(*s))
{
printf("栈空");
}
else
{
stacknode *p=s->top;
e=p->data;
s->top=p->next;
free(p);
}
}
/*销毁栈*/
void delete_stack(linkstack *s)
{
stacknode *p;
while(!is_empty_linkstack(*s))
{
p=s->top;
s->top=p->next;
free(p);
}
printf("栈销毁成功\n");
}
/*链式栈的长度*/
int length(linkstack s)
{
stacknode *p=s.top;
int i=0;
while(p!=s.base)
{
i++;
p=p->next;
}
return i;
}
/*返回栈顶元素*/
void get_top(linkstack s,int &e)
{
if(is_empty_linkstack(s))
{
printf("栈空");
}
else
{
e=s.top->data;
}
}
int main()
{
linkstack *s;
int l,e;
s=init_stack();
printf("请输入入栈的元素个数:");
scanf("%d",&l);
printf("请依次输入要入栈的元素:");
for(;l>0;l--)
{
scanf("%d",&e);
push(s,e);
}
printf("依次出栈的元素为:");
while(!is_empty_linkstack(*s))
{
pop(s,e);
printf("%d",e);
}
printf("\n");
delete_stack(s);
return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<ctype.h>
using namespace std;
/*结点结构体*/
typedef struct node
{
int data;
struct node *next;
}stacknode;
/*链式栈结构体*/
typedef struct linkstack
{
stacknode *base;
stacknode *top;
}linkstack;
/*链式栈初始化*/
linkstack *init_stack()
{
linkstack *s;
s=(linkstack*)malloc(sizeof(linkstack));
s->base=(stacknode*)malloc(sizeof(stacknode));
s->top=s->base;
s->base->next=NULL;
return s;
}
/*判断链式栈空*/
bool is_empty_linkstack(linkstack s)
{
if(s.base==s.top)
{
return true;
}
else
{
return false;
}
}
/*链式栈入栈*/
void push(linkstack *s,int e)
{
stacknode *p;
p=(stacknode*)malloc(sizeof(stacknode));
p->data=e;
p->next=s->top;
s->top=p;
}
/*链式栈出栈*/
void pop(linkstack *s,int &e)
{
if(is_empty_linkstack(*s))
{
printf("栈空");
}
else
{
stacknode *p=s->top;
e=p->data;
s->top=p->next;
free(p);
}
}
/*销毁栈*/
void delete_stack(linkstack *s)
{
stacknode *p;
while(!is_empty_linkstack(*s))
{
p=s->top;
s->top=p->next;
free(p);
}
printf("栈销毁成功\n");
}
/*链式栈的长度*/
int length(linkstack s)
{
stacknode *p=s.top;
int i=0;
while(p!=s.base)
{
i++;
p=p->next;
}
return i;
}
/*返回栈顶元素*/
void get_top(linkstack s,int &e)
{
if(is_empty_linkstack(s))
{
printf("栈空");
}
else
{
e=s.top->data;
}
}
int main()
{
linkstack *s;
int l,e;
s=init_stack();
printf("请输入入栈的元素个数:");
scanf("%d",&l);
printf("请依次输入要入栈的元素:");
for(;l>0;l--)
{
scanf("%d",&e);
push(s,e);
}
printf("依次出栈的元素为:");
while(!is_empty_linkstack(*s))
{
pop(s,e);
printf("%d",e);
}
printf("\n");
delete_stack(s);
return 0;
}
相关文章推荐
- 数据结构顺序栈和链栈基本操作----c++实现
- C++实现shapefile文件的读写和基本功能的操作
- C++实现有向权图的基本操作,界面友好,操作方便,运行流畅
- 【数据结构】双向循环线性表的基本操作--C++/C实现
- c++学习笔记—单链表基本操作的实现
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- 【数据结构】双向循环线性表的基本操作--C++/C实现
- C++实现链式栈,运用模板,界面友好,操作方便,运行流畅
- C++实现链式栈,运用模板,界面友好,操作方便,运行流畅
- 链式栈的基本操作与实现
- 双链表基本操作C++实现
- 栈的基本操作实现-c/c++
- 循环双链表基本操作C++实现
- C++实现树的基本操作,界面友好,操作方便,运行流畅,运用模板
- 详细的单链表基本操作C/C++实现
- C++链式栈的简单实现(只有基本功能)
- 使用C++实现链表的基本操作
- C++队列实现的基本操作
- c++学习笔记—二叉树基本操作的实现
- c++学习笔记—单链表基本操作的实现