Code

/*链表实现栈的一系列操作*/

#include<stdio.h>
#include<stdlib.h> #define OK 1
#define ERROR 0 typedef struct node
{
int data;
struct node *next;
}LinkStackNode,*LinkStack; /**********************各个子函数的定义*********************/
void initStack(LinkStack *top); //初始化链栈
int push(LinkStack top,int n); //链栈进栈操作
void pop(LinkStack top); //链栈出栈操作
int getTop(LinkStack top,int *s); //读取链栈栈顶元素 int main()
{
LinkStack top;
int choice;
while(true)
{
printf("*****************Please enter your choice*****************\n\n");
printf(" choice 1:Stack initialization\n");
printf(" choice 2:Into the stack\n");
printf(" choice 3:Out of the stack\n");
printf(" choice 4:Read the stack elements\n");
printf(" choice 0:exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case :
initStack(&top);
break;
case :
int n;
printf("Please enter the number into the stack elements:");
scanf("%d",&n);
(push(top,n)==)?printf("%d个元素依次进栈成功\n",n):printf("%d个元素依次进栈失败\n",n);
break;
case :
pop(top);
break;
case :
int* s;
(getTop(top,s)==)? printf("栈顶元素是:%d.\n",*s):printf("An empty stack error!!!!\n"); //三目运算符
break;
case :
exit();
break;
default:
printf("ERROR!!\n");
exit();
break;
}
}
return ;
} /**********************各个子函数功能的实现*********************/
void initStack(LinkStack *top)
{ //初始化
*top=(LinkStack)malloc(sizeof(LinkStackNode)); //带头结点的单链表
(*top)->next=NULL;
} int push(LinkStack top,int n) //进栈 ,将元素压入栈中
{
LinkStackNode *temp;
int n1,n2;
printf("Please enter into the stack elements in turn:\n");
for(n1=;n1<n;n1++)
{
scanf("%d",&n2);
temp=(LinkStackNode *)malloc(sizeof(LinkStackNode)); //结点申请空间
if(temp==NULL) return ERROR; //申请空间失败
temp->data=n2;
temp->next=top->next; //采用头插法存储元素
top->next=temp;
}
return OK;
} void pop(LinkStack top) //栈顶元素出栈
{
int a;
LinkStackNode *temp;
if(top->next==NULL)
{ //栈为空,出栈失败
printf("An empty stack error!!!!\n");
}
else
{
temp=top->next;
a=temp->data;
top->next=temp->next;
free(temp);
printf("栈顶元素%d出栈成功.\n",a);
}
} int getTop(LinkStack top,int *s) //获读取栈顶元素
{
if(top->next==NULL) //栈为空,读取栈顶元素失败
{
return ERROR;
}
else
{
*s=(top->next)->data; //读取栈顶元素,不出栈
return OK;
}
}

C语言实现链栈的初始化&进栈&出栈&读取栈顶元素的更多相关文章

  1. C语言实现顺序栈的初始化&进栈&出栈&读取栈顶元素

    /*顺序表实现栈的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define Stack_Size 50 //设栈中元素个数为50 ...

  2. C语言实现链队列的初始化&进队&出队

    /*链表实现队列的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typed ...

  3. C语言实现循环队列的初始化&进队&出队&读取队头元素&判空-2

    /*顺序表实现队列的一系列操作(设置flag标志不损失数组空间)*/ #include<stdio.h> #include<stdlib.h> #define Queue_Si ...

  4. C语言数据结构-链式栈的实现-初始化、销毁、长度、取栈顶元素、查找、入栈、出栈、显示操作

    1.数据结构-链式栈的实现-C语言 //链式栈的链式结构 typedef struct StackNode { int data; struct StackNode *next; } StackNod ...

  5. C语言初始化——栈的初始化

    栈是一种具有后进先出性质的数据组织方式,也就是说后存放的先取出,先存放的后取出.栈底是第一个进栈的数据所处的位置,栈顶是最后一个进栈的数据所处的位置. 1.满栈与空栈 根据SP指针指向的位置,栈可以分 ...

  6. C语言数据结构-栈的实现-初始化、销毁、长度、取栈顶元素、查找、入栈、出栈、显示操作

    1.数据结构-栈的实现-C语言 #define MAXSIZE 100 //栈的存储结构 typedef struct { int* base; //栈底指针 int* top; //栈顶指针 int ...

  7. C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)

    /**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋 ...

  8. 纯C语言实现链栈

    #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct StackNode{ E ...

  9. C语言实现链栈以及基本操作

    链栈,即用链表实现栈存储结构.链栈的实现思路同顺序栈类似,顺序栈是将数顺序表(数组)的一端作为栈底,另一端为栈顶:链栈也如此,通常我们将链表的头部作为栈顶,尾部作为栈底,如下下图所示: 将链表头部作为 ...

随机推荐

  1. [CF1138B]Circus

    Description: 给你2个长度为n的01串 从中选出\(n/2\)个,使得选出的数中第一排1的个数等于未选出数中第二排1的个数 输出一种方案即可,没有输出-1 Hint: \(n \le 50 ...

  2. 在虚拟机中安装Centos系统

    1.首先下载VMware 2.然后可以去http://mirrors.aliyun.com下载映像ISO 3.打开VM,点击创建新的虚拟机 4.选择典型模式 5.稍后安装操作系统 6.选择你所要安装的 ...

  3. IOS开发中关于runtime的认识

    首先要知道我们写的代码在程序运行过程中都会被转化成runtime的C代码执行. runtime突出的一点就是OC中消息传递机制的应用.objc_msgsend(target,SEL); 首先我们先看一 ...

  4. Linux下不同文件不同颜色的意义

    通常,我们使用ls查看文件时,会出现不同颜色的文件,今天我就大概写一下不同颜色的代表意义: 蓝色————目录: 绿色————可执行文件: 红色————压缩文件: 浅蓝色————链接文件: 紫红色——— ...

  5. swust oj 1010

    折半查找的实现 1000(ms) 10000(kb) 2877 / 11213 编写程序实现折半查找算法. 输入 第一行是查找表的长度n 第二行是查找表中的数据元素 : 第三行是要查找的数据元素的关键 ...

  6. 【.NET Core微服务实战-统一身份认证】开篇及目录索引

    简介 ​ 学习.NETCORE也有1年多时间了,发现.NETCORE项目实战系列教程很少,都是介绍开源项目或基础教程,对于那些观望的朋友不能形成很好的学习思路,遇到问题怕无法得到解决而不敢再实际项目中 ...

  7. 二叉查找树(BST)的实现

    一.二叉树介绍 二叉查找树(Binary Search Tree,BST),又称二叉排序树,也称二叉搜索树,它或者是一颗空树,或者具有如下性质的树:若它的左子树不为空,则左子树上所有节点的值都小于根节 ...

  8. JAVA四类八种基本数据类型

    boolean类型 Boolean在内存中占用一个字节. 当java编译器把java源代码编译为字节码时,会用int或byte来表示boolean.在java虚拟机中,用整数零来表示false,用任意 ...

  9. [Swift]LeetCode126. 单词接龙 II | Word Ladder II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  10. [Swift]LeetCode400. 第N个数字 | Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...