| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 375 人关注过本帖
标题:往链表中插入新元素的bug
只看楼主 加入收藏
wangwjt
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-11-29
结帖率:100%
收藏
 问题点数:0 回复次数:3 
往链表中插入新元素的bug
#include<stdio.h>
#include <stdlib.h>
typedef struct node{
    char name[20];
    struct node *next;
}NODE;

void creat(NODE *head, int n){
    NODE *p;
    for (; n > 0; n--){
        p = (NODE *)malloc(sizeof(NODE));
        gets(p->name);
        p->next = head->next;
        head->next = p;
    }
}
void  output(NODE *head){
    NODE *p;
    p = head->next;
    while (p != NULL){
        puts(p->name);
        p = p->next;
    }
}

void insert_note(NODE *head, NODE *p, int i){
    NODE *q;
    int n = 0;
    for (q = head; n < i && p->next != NULL; ++n)
        q = q->next;
    p->next = q->next;
    q->next = p;
}

void main(){
    NODE *head;
    int n;
    head = (NODE *)malloc(sizeof(NODE));
    head->next = NULL;

    printf("结点个数为:");
    scanf("%d", &n);                       
    getchar();                           
    creat(head, n);
    printf("\n\n");
    printf("输出;\n");
    output(head);

    NODE *p;
    p =(NODE *)malloc(sizeof(NODE));
    int i;
    printf("请输入要插入的位置");
    scanf("%d", &i);
   
    printf("请输入要插入的数据");
    gets(p->name);
    getchar();
    insert_note(head, p, i);
    printf("输出新链表;\n");
    output(head);
}


程序哪里出错了????相当于只插入了空结点,而没存入数据呀。,,,,求解决,,,,
搜索更多相关主题的帖子: include 元素 
2015-01-16 15:24
wangwjt
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-11-29
收藏
得分:0 
解决了。。。。。



#include<stdio.h>
#include <stdlib.h>
typedef struct node{
    char name[20];
    struct node *next;
}NODE;

void creat(NODE *head, int n){
    NODE *p;
    for (; n > 0; n--){
        p = (NODE *)malloc(sizeof(NODE));
        gets(p->name);
        p->next = head->next;
        head->next = p;
    }
}
void  output(NODE *head){
    NODE *p;
    p = head->next;
    while (p != NULL){
        puts(p->name);
        p = p->next;
    }
}

void insert_note(NODE *head, NODE *p, int i){
    NODE *q;
    int n = 0;
    for (q = head; n < i && p->next != NULL; ++n)
        q = q->next;
    p->next = q->next;
    q->next = p;
}

void main(){
    NODE *head;
    int n;
    head = (NODE *)malloc(sizeof(NODE));
    head->next = NULL;

    printf("结点个数为:");
    scanf("%d", &n);                       
    getchar();                           
    creat(head, n);
    printf("\n\n");
    printf("输出;\n");
    output(head);

    NODE *p;
    p =(NODE *)malloc(sizeof(NODE));
    int i;
    printf("请输入要插入的位置");
    scanf("%d", &i);
    printf("请输入要插入的数据");
    getchar();
    gets(p->name);
    insert_note(head, p, i);
    printf("输出新链表;\n");
    output(head);
}
2015-01-16 16:25
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:0 
程序代码:
        printf("请输入要插入的位置: ");
    scanf("%d", &i);
    getchar();
    printf("请输入要插入的数据: ");
    gets(p->name);

图片附件: 游客没有浏览图片的权限,请 登录注册

Only the Code Tells the Truth             K.I.S.S
2015-01-16 16:40
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:0 
看到楼主又新发了个帖 https://bbs.bccn.net/thread-441181-1-1.html
向该帖二楼的同学学习
汇总一下留个记号

程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    char name[81];
    struct node *next;
} NODE;


//新增结点在右边
void creatRight(NODE *head, int n) {
    NODE *p;

    for(; n > 0; n--) {
        p = (NODE *)malloc(sizeof(NODE));
        gets(p->name);
        head->next = p;
        head = p;
    }

    printf("\n");
}

//新增结点在左边
void creatLeft(NODE *head, int n) {
    NODE *p;

    for(; n > 0; n--) {
        p = (NODE *)malloc(sizeof(NODE));
        gets(p->name);
        p->next = head->next;
        head->next = p;
    }

    printf("\n");
}

void  output(NODE *head) {
    NODE *p;
    p = head->next;
    printf("输出:\n");

    while(p != NULL) {
        puts(p->name);
        p = p->next;
    }

    printf("\n");
}

void insert_node(NODE *head, NODE *p, int i) {
    NODE *q;
    int n = 0;

    for(q = head; n < i && p->next != NULL; ++n)
        q = q->next;

    p->next = q->next;
    q->next = p;
}

int main() {
    NODE *headR, *headL, *p;
    int i, n;

    p = (NODE *)malloc(sizeof(NODE));
    headR = (NODE *)malloc(sizeof(NODE));
    headL = (NODE *)malloc(sizeof(NODE));

    if(!p || !headR || !headL) exit(0);

    headR->next = NULL;
    headL->next = NULL;

    printf("结点个数为: ");
    scanf("%d", &n);
    fflush(stdin);
    creatRight(headR, n);
    creatLeft(headL, n);
    output(headR);
    output(headL);

    printf("请输入要插入的位置: ");
    scanf("%d", &i);
    fflush(stdin);

    printf("请输入要插入的数据: ");
    gets(p->name);
    insert_node(headR, p, i);
    insert_node(headL, p, i);

    printf("输出新链表:\n");
    output(headR);
    output(headL);

    free(p);
    free(headR);
    free(headL);
    return 0;
}

Only the Code Tells the Truth             K.I.S.S
2015-01-16 22:28
快速回复:往链表中插入新元素的bug
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027438 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved