数据结构【链栈】

1【链栈】

  链栈是栈这一受限的线性表的链式映像实现.为了方便操作,链栈的采用带头结点的单链表存储结构。

       若链栈为空,则栈头结点指针域为空;

       若链栈非空,则栈头结点指向链栈的第一个元素,可将其赋值给工作指针进行相应的各种操作.

 

  1 #include <malloc.h>
  2 #include <stdio.h>
  3 #include <process.h>
  4 #define OK 1
  5 #define Error 0
  6 typedef char ElemType;
  7 
  8 int yes=0;
  9 
 10 typedef struct stacknode  /*链栈结构*/
 11 {
 12  ElemType data;
 13  struct stacknode *next;
 14 }StackNode;
 15 typedef struct 
 16 {
 17  StackNode *top;
 18 
 19 
 20 }LinkStack;
 21 void InitStack(LinkStack *S)/*链栈的初始化*/
 22 {
 23   S->top=NULL;
 24 }
 25 void CreatLinkStack(LinkStack *Q)/*创建链栈操作*/
 26 {
 27  int i,length;
 28  char ch;
 29  StackNode *p;
 30  printf("请输入链栈长度:\n");
 31  scanf("%d",&length);
 32  for(i=0;i<length;i++)
 33  {
 34  printf("请输入链栈的元素:\n");
 35  scanf("%c",&ch);
 36  if((ch=getchar()!='\n'))
 37  {
 38    p=(StackNode *)malloc(sizeof(StackNode));
 39    p->data=ch;
 40    p->next=Q->top;
 41    Q->top=p;
 42  }
 43 }
 44  yes=1;
 45 }
 46 
 47 void Push(LinkStack *S,ElemType e)
 48 {
 49  
 50    StackNode *p;
 51    p=(StackNode *)malloc(sizeof(StackNode));
 52    p->data=e;
 53    p->next=S->top;
 54    S->top=p;
 55 
 56 }
 57 
 58 ElemType pop(LinkStack *S)
 59 {
 60  ElemType x;
 61  StackNode *p=S->top;
 62  if(!p) return -1;
 63  x=p->data;
 64  S->top=p->next;
 65  free(p);
 66  return x;
 67 
 68 }
 69 void LenLinkstack(LinkStack *q)
 70 {
 71  StackNode *r=q->top;
 72  int s=0;
 73  printf("此时的链栈长度为:\n");
 74  while(r)
 75  {
 76   r=r->next;
 77   ++s;
 78 
 79  }
 80  printf("%d\n",s);
 81 }
 82 void DispLinkStack(LinkStack *S)
 83 {
 84   int i=1;
 85   StackNode *p=S->top;
 86   while(p)
 87   {
 88    printf("%3d %3c\n",i,p->data);
 89    p=p->next;
 90    ++i;
 91   }
 92 
 93 }
 94 ElemType LinkStackEmpty(LinkStack *S)
 95 {
 96  return S->top==NULL;
 97  
 98 }
 99 ElemType StackTop(LinkStack *S)
100 {
101  return S->top->data;
102 
103 }
104 void DestroyLinkStack(LinkStack S)
105 {
106  StackNode *q,*p=S.top;
107  if(!p)
108      printf("链栈已经销毁!\n");
109  else
110      while(p)
111      {
112       q=p;
113       p=p->next;
114       free(q);
115       
116      }
117 printf("链栈销毁成功!\n");
118 yes=0;
119 
120 
121 
122 }
123 void menu()
124 {
125  printf("\t\t\t链栈基本操作\n");
126  printf("***********************************************\n");
127  printf("1 建立链栈!\n");
128  printf("2 链栈入栈操作!\n");
129  printf("3 链栈出栈操作!\n");
130  printf("4 求链栈的长度!\n");
131  printf("5 判断链栈是否为空!\n");
132  printf("6 输出链栈!\n");
133  printf("7 链栈销毁!\n");
134  printf("0 退出!\n");
135  printf("************************************************\n");
136 
137 }
138 int main(void) /*程序入口*/
139 {
140   LinkStack ptr;
141   int sel;
142   char ch;
143   while(1)
144   {
145     menu();
146     printf("请输入命令0--7:\n");
147     scanf("%d",&sel);
148     switch(sel)
149     {
150     case 1:if(yes==1)
151            {
152             printf("此时链栈已创建!不能再次创建!\n");
153             break;
154            }
155         else
156             InitStack(&ptr);
157             CreatLinkStack(&ptr);
158             break;
159     case 2:if(yes==0)
160            {
161             printf("此时链表未创建!不能入栈!\n");
162             break;
163            }
164         else
165         {
166           printf("请输入栈的元素值:");
167           scanf("%c",&ch);
168           if((ch=getchar())!='\n')
169               Push(&ptr,ch);
170               break;
171         }
172     case 3:if(yes==0)
173            {
174              printf("此时链表未创建!不能出栈!\n");
175              break;
176            }
177         else
178         {
179          if(ptr.top==NULL)
180              printf("链栈已空!\n");
181          else
182          {
183            ch=pop(&ptr);
184            printf("目前出栈的元素是%c.\n",ch);
185            break;
186          }
187         }
188     case 4:if(yes==0)
189            {
190             printf("此时链表未创建!不能求其长度!\n");
191             break;
192            }
193         else
194             LenLinkstack(&ptr);
195             break;
196     case 5:if(yes==0)
197            {
198             printf("此时链栈未创建!不能判断是否为空!\n");
199             break;
200            }
201         else
202         {
203          if(LinkStackEmpty(&ptr))
204              printf("此时链栈为空栈!\n");
205          else
206              printf("此时链栈非空!\n");
207         }
208         break;
209     case 6:if(yes==0)
210            {
211             printf("此时链表未创建!不能输出栈!\n");
212             break;
213            }
214         DispLinkStack(&ptr);
215         break;
216     
217     case 7:if(yes==0)
218            {
219             printf("此时链栈未创建!不能进行销毁!\n");
220             break;
221            }
222         else
223             DestroyLinkStack(ptr);
224             break;
225 
226     case 0:exit(1);break;
227     default:printf("命令输入有误!重新输入:\n");
228         break;
229   }
230 }
231 return OK;
232 }

 

posted @ 2018-09-05 21:32  Justice-V  阅读(261)  评论(0)    收藏  举报