栈(C语言实现)
来源:互联网 发布:vb,VF,SQL属于什么软件 编辑:程序博客网 时间:2024/04/27 11:17
数据结构——栈(C语言实现)
1 /*stack头文件*/
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 typedef struct Node
6 {
7 int data;
8 struct Node* next;
9 }StackNode;
10
11 void initStack(StackNode* top)
12 {
13 top->next=NULL;
14 }
15
16 int IsEmpty(StackNode* top)
17 {
18 if(top->next==NULL)
19 return 1;
20 return 0;
21 }
22
23 void Push(StackNode* top,int item)
24 {
25 StackNode* temp=(StackNode*)malloc(sizeof(StackNode));
26 if(temp==NULL)
27 {
28 printf("malloc memory fail");
29 exit(1);
30 }
31 temp->data=item;
32 temp->next=top->next;
33 top->next=temp;
34 }
35
36 int Pop(StackNode* top)
37 {
38 StackNode* temp;
39 int tempint;
40 if(top==NULL)
41 {
42 printf("There is no elements/n");
43 exit(1);
44 }
45 temp=top->next;
46 tempint= temp->data;
47 top->next=temp->next;
48 free(temp);
49 return tempint;
50 }
51
52 int GetTop(StackNode* top)
53 {
54 if(top==NULL)
55 {
56 printf("There is no elements/n");
57 exit(1);
58 }
59 return top->next->data;
60 }
61
62 void ClearStack(StackNode* top)
63 {
64 StackNode* temp;
65 while(top)
66 {
67 temp=top;
68 top=top->next;
69 free(temp);
70 }
71 }
1 /*测试stack*/
2 #include "StackList.h"
3
4 int main()
5 {
6 int temp;
7 StackNode* top;
8 top=(StackNode*)malloc(sizeof(StackNode));
9 initStack(top);
10 Push(top,10);
11 Push(top,20);
12 temp=Pop(top);
13 printf("%d/n",temp);
14 temp=GetTop(top);
15 printf("%d/n",temp);
16
17 Pop(top);
18 // Pop(top);
19 temp=IsEmpty(top);
20 printf("%d/n",temp);
21 ClearStack(top);
22
23 }
http://www.cnblogs.com/hstcghost/archive/2010/11/07/1871050.html
- 栈(C语言实现)
- 栈C语言实现
- C语言实现栈
- C语言实现栈
- C语言实现栈
- C语言实现栈
- C语言实现栈
- c语言实现栈
- C语言实现栈
- C语言栈实现
- C语言实现栈
- 顺序栈的实现(C语言)
- C语言 迷宫(栈实现)
- 数据结构--顺序栈实现(c语言)
- 顺序栈的实现(C语言)
- c语言计算器(栈实现)
- 栈的链式实现(C语言)
- C语言实现栈(数组)
- ERP软件未来之路:BI、BPM还是E-commerce?
- 提问的智慧
- list 的LVS_EX_SUBITEMIMAGES
- Android有效解决加载大图片时内存溢出的问题
- LINUX 下安装摄像头驱动并测试其功能的过程
- 栈(C语言实现)
- 如何有效地报告 Bug
- PropertyInfo 理解
- 关于求最值问题中显示其中间路径
- 在RedHat Linux 5.5 (x32/x64)上安装Oracle 10g r2(10.2.0.5)
- 自定义SQL Server Profiler 模板
- RadPHP XE
- 如何报告Bug,常用信息的收集,方法等
- MongoDB调查总结