Chinaunix首页 | 论坛 | 博客
  • 博客访问: 926902
  • 博文数量: 335
  • 博客积分: 10287
  • 博客等级: 上将
  • 技术积分: 3300
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 15:29
文章分类

全部博文(335)

文章存档

2015年(4)

2014年(15)

2013年(17)

2012年(11)

2011年(12)

2010年(96)

2009年(27)

2008年(34)

2007年(43)

2006年(39)

2005年(37)

我的朋友

分类: C/C++

2010-03-17 00:28:42

/*用单链表对堆栈操作*/
#include"stdio.h"
#define  maxsize  20
typedef struct node
{
    char data;
    struct node *next;
}lnode;

lnode *initializtion(lnode *top)
{
    top=NULL;
    return(top);
}
lnode *push(lnode *top,int x)
{
    lnode *p;
    p=(lnode*)malloc(sizeof(lnode));
    p->data=x;
    p->next=top;
    top=p;
    printf("[%p]",&top);
    return(top);

}
lnode *pop(lnode *top)
{
    lnode *tep;
    tep=top;
    top=top->next;
    free(tep);
    return(top);
}

void print(lnode *top)
{
    if(top==NULL)
    {
        printf("the stack is NULL");
        top->data=0;
     }
    else
    {
        while(top!=NULL)
        {
            printf("[%d]",top->data);
            top=top->next;
        }
    }
}

      
void main()
{
    int select;
    int x;
    int y;
    lnode *top;
    top=initializtion(top);
    do
    {
        printf("\n(1) Input a stack data");
        printf("\n(2) Output a stack data");
        printf("\n(3) Exit");
        printf("\nPlease select one:");
        scanf("%d",&select);
        switch(select)
        {
            case 1:printf("\nPlease input the data:");
                   scanf("%d",&x);
                   top=push(top,x);
                   printf("[%p]",&top);
                   printf("\nThis stack is: ");
                   print(top);
                   break;
            case 2:top=pop(top);
                   printf("\nThis stack is: ");
                   print(top);
                   printf("\nThe get data is: %d ",top->data);
                   break;

            case 3:break;
        }
    }
    while(select<3);
    getch();
}
阅读(378) | 评论(0) | 转发(0) |
0

上一篇:C堆栈的线性存储

下一篇:C单链表操作

给主人留下些什么吧!~~