新手!!链桟问题请大神求解!!

陆小茂 2017-11-02 09:45:00
//原始代码

#include<stdio.h>
#include<stdlib.h>

typedef struct car
{
int id;
char color[20];
}CAR;

typedef struct node
{
CAR data;
struct node *next;
}NODE;

main()
{

NODE *top; //定义栈顶指针
//初始化
top=NULL;
//加入节点,入栈
NODE *p; //定义指针
p=(NODE *)malloc(sizeof(NODE)); //分配空间
p->data.id=12345; //赋值
p->next=NULL;

NODE *q;
q=(NODE *)malloc(sizeof(NODE));
q->data.id=23456;
q->next=NULL;

NODE *s;
s=(NODE *)malloc(sizeof(NODE));
s->data.id=34567;
s->next=NULL;

p->next=top;
top=p;

q->next=top;
top=q;

s->next=top;
top=s;
//输出
NODE *t;
t=top;
while(t!=NULL)
{
printf("%d\n",t->data.id);
t=t->next;
}

//删除
NODE *tmp;
tmp=top;
top=top->next;
free(tmp); //释放内存

//输出
NODE *t1;
t1=top;
printf("删除\n");
while(t1!=NULL)
{
printf("%d\n",t1->data.id);
t1=t1->next;
}

}


//现要求对上面的代码改为在函数中实现

//下面的是我修改的,出现的语法错误,本人刚入门,请大神指点一二,万分感激

#include<stdio.h>
#include<stdlib.h>

typedef struct car
{
int id;
char color[20];
}CAR;

typedef struct node
{
CAR data;
struct node *next;
}NODE;

void initLinkStack(NODE *top)
{
top=NULL;
}

void insertLinkStack(NODE *top,CAR newCar)
{
NODE *node;
node=(NODE *)malloc(sizeof(NODE)); //分配空间
node->data=newCar; //赋值

node->next=top; //新节点指向堆栈的顶端
top=node; //新节点成为桟的顶端
}

void outPutLinkStack(NODE *top)
{

NODE *t;
t=top;
while(t!=NULL)
{
printf("车牌:%d ,颜色:%s\n",t->data.id,t->data.color);
t=t->next;
}
}

void deleteLinkStack(NODE *top)
{
NODE *tmp;
tmp=top;
top=top->next;
free(tmp); //释放内存

}

main()
{
CAR C1={123456,"black"};
CAR C2={234567,"blue"};
CAR C3={345678,"red"};

NODE top; //定义栈顶指针
initLinkStack(&top);

//增加元素
printf("依次增加桟元素:\n");
insertLinkStack(&top,C1);
insertLinkStack(&top,C2);
insertLinkStack(&top,C3);
//输出元素
outPutLinkStack(&top);

//删除元素
printf("删除栈顶元素:\n");
deleteLinkStack(&top);
//输出元素
outPutLinkStack(&top);

}

...全文
133 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
陆小茂 2017-11-04
引用 2 楼 cfjtaishan 的回复:
#if 0
#include<stdio.h>
#include<stdlib.h>

typedef struct car
{
    int id;
    char color[20];
}CAR;

typedef struct node
{
    CAR data;
    struct node *next;
}NODE;

int main(void)
{

    NODE *top;
    top = NULL;

    //加入节点,入栈
    NODE *p; //定义指针
    p = (NODE *)malloc(sizeof(NODE)); //分配空间
    p->data.id=12345; //赋值
    p->next = NULL;

    NODE *q;
    q=(NODE *)malloc(sizeof(NODE));
    q->data.id=23456;
    q->next = NULL;

    NODE *s;
    s=(NODE *)malloc(sizeof(NODE));
    s->data.id = 34567;
    s->next = NULL;

    p->next = top;
    top = p;

    q->next = top;
    top = q;

    s->next = top;
    top = s;

    //输出
    NODE *t;
    t = top;
    while(t)
    {
        printf("%d\n",t->data.id);
        t = t->next;
    }

    //删除
    NODE *tmp;
    tmp = top;
    top = top->next;
    if (tmp)
        free(tmp); //释放内存

    //输出
    NODE *t1;
    t1 = top;
    printf("删除\n");
    while(t1)
    {
        printf("%d\n",t1->data.id);
        t1 = t1->next;
    }

}

#else

//现要求对上面的代码改为在函数中实现

//下面的是我修改的,出现的语法错误,本人刚入门,请大神指点一二,万分感激

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

typedef struct car
{
    int id;
    char color[20];
}CAR;

typedef struct node
{
    CAR data;
    struct node *next;
}NODE;

void initLinkStack(NODE **top)
{
    *top = NULL;
}

void insertLinkStack(NODE **top,CAR newCar)
{
    NODE *node;

    node = (NODE *)malloc(sizeof(NODE));
    if (!node)
        exit(0);

    node->data = newCar;
    node->next = *top;
    *top = node;
}

void outPutLinkStack(NODE *top)
{
    NODE *t;

    t = top;
    while (t) {
        printf("车牌:%d ,颜色:%s\n",t->data.id,t->data.color);
        t = t->next;
    }
}

void deleteLinkStack(NODE **top)
{
    NODE *tmp;

    if (!top)
        return;
    tmp = *top;
    *top = (*top)->next;
    free(tmp); //释放内存

}

int main()
{
    CAR C1 = {123456,"black"};
    CAR C2 = {234567,"blue"};
    CAR C3 = {345678,"red"};

    NODE *top; //定义栈顶指针
    initLinkStack(&top);

    //增加元素
    printf("依次增加桟元素:\n");
    insertLinkStack(&top,C1);
    insertLinkStack(&top,C2);
    insertLinkStack(&top,C3);
    //输出元素
    outPutLinkStack(top);

    //删除元素
    printf("删除栈顶元素:\n");
    deleteLinkStack(&top);
    //输出元素
    outPutLinkStack(top);

}
#endif
修改你的代码之后,可以试一下;
NODE top; //定义栈顶指针
initLinkStack(&top);
注释和你的实际不相符,比如上面的两句代码,注释是定义栈顶指针,但是,却是定义了一个结构体变量,而非指针。
谢谢啦
  • 打赏
  • 举报
回复
陆小茂 2017-11-04
引用 1 楼 jiht594 的回复:
并没有语法错误。 main函数需要返回int
我用无返回类型的呢,我直接用指针传值的
  • 打赏
  • 举报
回复
陆小茂 2017-11-04
谢谢大家!!!
  • 打赏
  • 举报
回复
自信男孩 2017-11-03
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
 
typedef struct car
{
    int id;
    char color[20];
}CAR;
 
typedef struct node
{
    CAR data;
    struct node *next;
}NODE;
 
void initLinkStack(NODE **top)
{
    *top = NULL;
}
 
void insertLinkStack(NODE **top,CAR newCar)
{
    NODE *node;
 
    node = (NODE *)malloc(sizeof(NODE));
    if (!node)
        exit(0);
 
    node->data = newCar;
    node->next = *top;
    *top = node;
}
 
void outPutLinkStack(NODE *top)
{
    NODE *t;
 
    t = top;
    while (t) {
        printf("车牌:%d ,颜色:%s\n",t->data.id,t->data.color);
        t = t->next;
    }
}
 
void deleteLinkStack(NODE **top)
{
    NODE *tmp;
 
    if (!top)
        return;
    tmp = *top;
    *top = (*top)->next;
    free(tmp); //释放内存
 
}
 
int main()
{
    CAR C1 = {123456,"black"};
    CAR C2 = {234567,"blue"};
    CAR C3 = {345678,"red"};
 
    NODE *top; //定义栈顶指针
    initLinkStack(&top);
 
    //增加元素
    printf("依次增加桟元素:\n");
    insertLinkStack(&top,C1);
    insertLinkStack(&top,C2);
    insertLinkStack(&top,C3);
    //输出元素
    outPutLinkStack(top);
 
    //删除元素
    printf("删除栈顶元素:\n");
    deleteLinkStack(&top);
    //输出元素
    outPutLinkStack(top);
 
}
  • 打赏
  • 举报
回复
自信男孩 2017-11-03
#if 0
#include<stdio.h>
#include<stdlib.h>

typedef struct car
{
    int id;
    char color[20];
}CAR;

typedef struct node
{
    CAR data;
    struct node *next;
}NODE;

int main(void)
{

    NODE *top;
    top = NULL;

    //加入节点,入栈
    NODE *p; //定义指针
    p = (NODE *)malloc(sizeof(NODE)); //分配空间
    p->data.id=12345; //赋值
    p->next = NULL;

    NODE *q;
    q=(NODE *)malloc(sizeof(NODE));
    q->data.id=23456;
    q->next = NULL;

    NODE *s;
    s=(NODE *)malloc(sizeof(NODE));
    s->data.id = 34567;
    s->next = NULL;

    p->next = top;
    top = p;

    q->next = top;
    top = q;

    s->next = top;
    top = s;

    //输出
    NODE *t;
    t = top;
    while(t)
    {
        printf("%d\n",t->data.id);
        t = t->next;
    }

    //删除
    NODE *tmp;
    tmp = top;
    top = top->next;
    if (tmp)
        free(tmp); //释放内存

    //输出
    NODE *t1;
    t1 = top;
    printf("删除\n");
    while(t1)
    {
        printf("%d\n",t1->data.id);
        t1 = t1->next;
    }

}

#else

//现要求对上面的代码改为在函数中实现

//下面的是我修改的,出现的语法错误,本人刚入门,请大神指点一二,万分感激

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

typedef struct car
{
    int id;
    char color[20];
}CAR;

typedef struct node
{
    CAR data;
    struct node *next;
}NODE;

void initLinkStack(NODE **top)
{
    *top = NULL;
}

void insertLinkStack(NODE **top,CAR newCar)
{
    NODE *node;

    node = (NODE *)malloc(sizeof(NODE));
    if (!node)
        exit(0);

    node->data = newCar;
    node->next = *top;
    *top = node;
}

void outPutLinkStack(NODE *top)
{
    NODE *t;

    t = top;
    while (t) {
        printf("车牌:%d ,颜色:%s\n",t->data.id,t->data.color);
        t = t->next;
    }
}

void deleteLinkStack(NODE **top)
{
    NODE *tmp;

    if (!top)
        return;
    tmp = *top;
    *top = (*top)->next;
    free(tmp); //释放内存

}

int main()
{
    CAR C1 = {123456,"black"};
    CAR C2 = {234567,"blue"};
    CAR C3 = {345678,"red"};

    NODE *top; //定义栈顶指针
    initLinkStack(&top);

    //增加元素
    printf("依次增加桟元素:\n");
    insertLinkStack(&top,C1);
    insertLinkStack(&top,C2);
    insertLinkStack(&top,C3);
    //输出元素
    outPutLinkStack(top);

    //删除元素
    printf("删除栈顶元素:\n");
    deleteLinkStack(&top);
    //输出元素
    outPutLinkStack(top);

}
#endif
修改你的代码之后,可以试一下;
NODE top; //定义栈顶指针
initLinkStack(&top);
注释和你的实际不相符,比如上面的两句代码,注释是定义栈顶指针,但是,却是定义了一个结构体变量,而非指针。
  • 打赏
  • 举报
回复
jiht594 2017-11-03
并没有语法错误。
main函数需要返回int
  • 打赏
  • 举报
回复
发帖
C语言

6.6w+

社区成员

C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
帖子事件
创建了帖子
2017-11-02 09:45
社区公告
暂无公告