2008秋季-计算机软件基础-0917课堂用例(1)
  TnD0WQEygW8e 2023年11月09日 27 0

链栈:


#include<stdio.h>

#include<stdlib.h>
struct  stacknode

{

 int  data;

 struct  stacknode *next;

 };

//
struct stacknode * InitialLinkList ()

{

 struct stacknode * head;

 head=(struct stacknode *)

     malloc(sizeof(struct stacknode ));//
 head->next=NULL;

 return head;

}

//入栈
void PushIntoStack(struct stacknode * head, int value)

{

    struct stacknode * p;

    p=(struct stacknode *)malloc(sizeof(struct stacknode ));

    p->data=value;

    p->next=head->next;

    head->next=p;

 }


 void PopFromStack(struct stacknode * head)

{

 struct stacknode * p;

 if(head->next==NULL)

   printf("Pop Failed \n");

 else

    {

        p=head->next;

     head->next=p->next;

    free(p);

     }

}

 

 void ShowStackElement(struct stacknode *head)

 {

    struct stacknode * p;

    p=head->next;

    printf("\n显示栈中元素:\n");

    while(p!=NULL)

    {

        printf(" %d ",p->data);

        p=p->next;

    }


 }


 void main()

 {

   struct stacknode *head;

   head=InitialLinkList();

   PushIntoStack(head,1);

   ShowStackElement(head);

    PushIntoStack(head,2);

   ShowStackElement(head);

    PushIntoStack(head,3);

   ShowStackElement(head);

   PopFromStack(head);

   ShowStackElement(head);

   PopFromStack(head);

   ShowStackElement(head);

    PopFromStack(head);

   ShowStackElement(head);

    PopFromStack(head);

   ShowStackElement(head);

    PopFromStack(head);

   ShowStackElement(head);

 }


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月09日 0

暂无评论

推荐阅读
TnD0WQEygW8e