C语言复习--实现栈

C
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define STACK_SIZE 100
 4 typedef char TYPE;
 5 typedef struct Node* pNode;
 6 typedef struct Node node;
 7 typedef pNode Stack;
 8 //char buffer[100];
 9 struct Node
10 {
11     TYPE data;
12     struct Node* next;
13 };
14 int isEmpty(Stack s);
15 void Pop(Stack s);
16 void Push(Stack s,TYPE element);
17 TYPE Top_of_stack(Stack s);
18 Stack CreatStack();
19 void makeEmpty(Stack s);
20 
21 Stack CreatStack()
22 {
23     Stack s=(Stack)malloc(sizeof(node));
24     s->next=NULL;
25     makeEmpty(s);
26     return s;
27 }
28 void makeEmpty(Stack s)
29 {
30     if(s==NULL)
31     printf("you need creat a stack at first");
32     while(!isEmpty(s))
33     Pop(s);
34 }
35 int isEmpty(Stack s)
36 {
37     return s->next==NULL;
38 }
39 void Pop(Stack s)
40 {
41     if(isEmpty(s))
42       printf("Stack is empty");
43     else
44     {
45         pNode temp=s->next;
46         s->next=s->next->next;
47         free(temp);
48     }
49 
50 }
51 void Push(Stack s,TYPE element)
52 {
53     pNode temp=(Stack)malloc(sizeof(node));
54     if(temp)
55     {
56         temp->data=element;
57         temp->next=s->next;
58         s->next=temp;
59     }
60 }
61 TYPE Top_of_stack(Stack s)
62 {
63     if(isEmpty(s))
64     {
65         printf("Stack is empty");
66         return 0;
67     }
68     else
69     return s->next->data;
70 }
71 
72 int main()
73 {
74     Stack s =CreatStack();
75     makeEmpty(s);
76     Push(s,'c');
77     Push(s,'d');
78     Push(s,'e');
79     while(!isEmpty(s))
80     {
81         printf("%c",Top_of_stack(s));
82         Pop(s);
83     }
84 
85   return 0;
86 }
 Code

 

posted on 2014-04-23 14:57  阿南要加油  阅读(151)  评论(0编辑  收藏  举报

导航