1. 程式人生 > >資料結構之鏈式棧

資料結構之鏈式棧

#include<stdio.h> #include<stdlib.h> typedef int Elemtype; typedef struct node        //定義結點 {     Elemtype data;     struct node *up;     struct node *down; }Node; typedef struct linkstack    //定義棧 {     Node *top;     Node *bottom;     int max;     int length; }Linkstack; Linkstack *crea(int max)    //生成空棧 {     Linkstack *s=(Linkstack *)malloc(sizeof(*s));     s->top=NULL;     s->bottom=NULL;     s->max=max;     s->length=0;     return s; }

int isFull(Linkstack *s)    //判斷棧是否已滿 {     if(s->length==s->max)     {         return 1;     }     return 0; }

int isEmpty(Linkstack *s)    //判斷是否為空棧 {     if(s->length==0)     {         return 1;     }     return 0; }

void push(Linkstack *s,Elemtype data)        //入棧 {     if(isFull(s))     {         return ;     }     s->length++;     Node *pnew=(Node *)malloc(sizeof(*pnew));     pnew->data=data;     pnew->up=NULL;     pnew->down=NULL;      //    if(isEmpty(s))         //錯誤!在這之前s->length已經改變!!     if(s->top==NULL)     {             s->top=pnew;         s->bottom=pnew;     }     else     {         s->top->up=pnew;         pnew->down=s->top;         s->top=pnew;     }      }

/****************************************** 出棧方法1:     Description:         //這裡不要用成指標函式!!! */

Elemtype pop(Linkstack *s)        //出棧 {     if(isEmpty(s))     {         return ;     }     s->length--;     Elemtype data=s->top->data;     Node *p=s->top;     if(s->top==s->bottom)     {         s->bottom=NULL;         s->top=NULL;         free(p);     }     else     {         s->top=s->top->down;         s->top->up=NULL;         p->down=NULL;         free(p);              }         return data;     }

/**************************************    ******     鏈式棧輸出後必須釋放空間???     並不是,不釋放掉,下次出棧入棧也沒有影響,     但是這塊空間會被一直佔著,影響電腦執行!!! **/

/*************************************************** 出棧方法2;

**/ /* Elemtype pop(Linkstack *s)        //出棧 {     if(isEmpty(s))     {         return ;     }     s->length--;     Elemtype data=s->top->data;     s->top=s->top->down;                 return data;     } */

/**直接使用棧頂的數*****/ Elemtype getHead(Linkstack *s) {     return s->top->data; }

/********************************************************** 將棧初始化; **********/ void clearstack(Linkstack *s) {     Node *p=s->top;     while(p)     {         s->top=s->top->down;                 free(p);    //釋放棧頂所佔空間         p=s->top;     }     s->top=NULL;     s->bottom=NULL;     s->length=0; } /********************************************* 銷燬棧 ***********/ void destroystack(Linkstack *s) {     if(s)//若不是空棧     {         clearstack(s);    //呼叫初始化函式將其初始化     }     free(s);    //釋放空棧所佔記憶體; }

int main() {     Linkstack *s=crea(10);     push(s,3);     push(s,4);     push(s,5);        //入棧     Elemtype a=pop(s);    //出棧     printf("%d\n",a);    //5     a=pop(s);     printf("%d\n",a);    //4     a=pop(s);     printf("%d\n",a);    //3     a=pop(s);     printf("%d\n",a);    //1     a=pop(s);     printf("%d\n",a);    //1     push(s,10);     push(s,40);     push(s,50);     a=pop(s);     printf("%d\n",a);    //50     a=getHead(s);                //直接去棧頂的數     printf("zd=%d\n",a);    //zd=40    直接去棧頂的數         a=pop(s);     printf("%d\n",a);    //40     a=pop(s);     printf("%d\n",a);    //10     clearstack(s);        //將棧初始化     a=pop(s);    //讓空棧出棧     printf("%d\n",a);    //1             destroystack(s);          return 0; }