注册 登录
编程论坛 数据结构与算法

关于数据结构的问题(2)

lufeng1720 发布于 2010-04-21 16:55, 585 次点击
#include <stdio.h>
#include <stdlib.h>
typedef struct stacknode
{int data;
    struct stacknode *next;
}stacknode,* linkstack;
linkstack top;
void push(linkstack top,int x)
{stacknode *p=new stacknode;
    p->data=x;
    p->next=top;
    top=p;
}
int pop(linkstack top)
{int x;
    stacknode *p=top;
    x=p->data;
    top=p->next;
    free (p);
    return x;
}
void showstack(linkstack top)
{stacknode *p=top;
    if(p==NULL)
    printf("it is kong");
    else
    {printf("it is:");
        while(p!=NULL)
        {printf("%6d",p->data);
            p=p->next;
        }
        printf("\n");
}}
main()
{linkstack top=NULL;
push(top,1);
push(top,2);
printf("%5d",pop(top));
请顺便看看这个吧!!!!!!!!!!!!!!也运行不了啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!为什么????????????????????????????????????
5 回复
#2
寒风中的细雨2010-04-21 21:42
#include <stdio.h>
#include <stdlib.h>

typedef struct stacknode
{
    int data;
    struct stacknode *next;
}stacknode,* linkstack;

//linkstack top;

void push(linkstack &top,int x)
{
    stacknode *p=new stacknode;
    p->data = x;
    p->next = top;
    top=p;
    printf("%d\n", top->data);
}

int pop(linkstack &top)
{
    int x;
    stacknode *p=top;
    x=p->data;
    top=p->next;
    //free (p);//
    delete p;
    return x;
}

void showstack(linkstack top)
{
    stacknode *p=top;
    if(p==NULL)
    printf("it is kong");
    else
    {
        printf("it is:");
        while(p)
        {
            printf("%6d",p->data);
            p=p->next;
        }
        printf("\n");
    }
}

int main()
{
    linkstack top=NULL;

    push(top,1);
    push(top,2);
    printf("%5d\n",pop(top));

    return 0;
}
#3
lufeng17202010-04-21 23:07
不胜感激啊!!!!!!!!!!!!!!!!!!!!!!!
#4
lufeng17202010-04-21 23:09
谢谢啊 !!!!!!!!高手啊!!!!以后多指教小弟啊!能不能加我qq啊?以后有不懂的可以问你啊!我的qq是495273822!!!!
#5
寒风中的细雨2010-04-22 06:32
差远啦  
  正在学呢还
#6
2010-04-28 09:33
#include <stdio.h>
#include <stdlib.h>

typedef struct stacknode
{
    int data;
    struct stacknode *next;
}stacknode,* linkstack;

//linkstack top;

void push(linkstack &top,int x)
{
    stacknode *p=new stacknode;
    p->data = x;
    p->next = top;
    top=p;
    printf("%d\n", top->data);
}

int pop(linkstack &top)
{
    int x;
    stacknode *p=top;
    x=p->data;
    top=p->next;
    //free (p);//
    delete p;
    return x;
}

void showstack(linkstack top)
{
    stacknode *p=top;
    if(p==NULL)
    printf("it is kong");
    else
    {
        printf("it is:");
        while(p)
        {
            printf("%6d",p->data);
            p=p->next;
        }
        printf("\n");
    }
}

int main()
{
    linkstack top=NULL;

    push(top,1);
    push(top,2);
    printf("%5d\n",pop(top));

    return 0;

能分析一下这个程序吗?
谢谢!!!!
1