資料結構---棧的鏈表實現

來源:互聯網
上載者:User

標籤:棧   鏈表   資料結構   先進後出   

棧的鏈表實現C代碼如下:

#include <stdio.h>typedef int ElemType;typedef struct node{    ElemType Data;    struct node *next;}Node;typedef struct stack{    Node *top;}Stack;//初始化棧void InitStack(Stack *S){    S->top=NULL;}//入棧int PushStackValue(Stack *S){    printf("Input the Value of stack member:\n(0-exit)\n");    int value;    int i=1;    Node *NewNode=(Node *)malloc(sizeof(Node));    printf("Please input the %dst value of stack:\n",i);    scanf("%d",&value);    NewNode->Data=value;    NewNode->next=S->top;    S->top=NewNode;    i++;    while(value)    {        Node *NewNode=(Node *)malloc(sizeof(Node));        printf("Please input the %dst value of stack:\n",i);        scanf("%d",&value);        if(value!=0)            i++;        NewNode->Data=value;        NewNode->next=S->top;        S->top=NewNode;    }    S->top=S->top->next;    return i-1;}//出棧int PopStackValue(Stack *S,int len){    if(len>0)    {        printf("the stack %dst value pop out: %d\n",len,S->top->Data);    }    else    {        printf("The Stack is empty\n");    }    len--;    S->top=S->top->next;    return len;}//判斷棧空void IsEmpty(Stack *S){    if(S->top==NULL)    {        printf("The Stack is empty.\n");    }    else    {        printf("The stack is not empty.\n");    }}//清空棧void ClearStack(Stack *S){    S->top=NULL;}//遍曆棧Stack * ScanStack(Stack *S){    Stack *Temp=S;    printf("The all stack member(from top to bottom) is:\n");    while(S->top!=NULL)    {        printf("%d ",S->top->Data);        S->top=S->top->next;    }    printf("\n");    return Temp;}void main(){    Stack *S=(Stack *)malloc(sizeof(Stack));    int len;    InitStack(&S);    len=PushStackValue(&S);    len=PopStackValue(&S,len);    len=PopStackValue(&S,len);    S=ScanStack(&S);    IsEmpty(&S);}

運行結果:





轉載請註明作者:小劉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.