标题:C语言 链表问题啊 这个插最后一个 插不进去 求高手改改?
只看楼主
tianxiayehan
Rank: 1
等 级:禁止发言
帖 子:47
专家分:4
注 册:2011-8-31
结帖率:50%
已结贴  问题点数:20 回复次数:5 
C语言 链表问题啊 这个插最后一个 插不进去 求高手改改?
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: name include create record 
2012-01-21 17:11
唯佳
Rank: 2
等 级:论坛游民
帖 子:42
专家分:44
注 册:2012-1-11
得分:7 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define Max 10

struct list
{
    int number;
    int total;
    struct list *Next;
};
typedef struct list Node;
typedef Node *link;

int data[2][Max] =
{
    1,3,5,7,2,4,6,8,9,0,15,35,10,67,25,65,38,70,30,20
};//输入数据


link Delete_list(link head,int key)

 {
     link pointer;
     link back;

     pointer = head;//pointer设为首节点

     while(1)
     {
         if( pointer->Next == NULL)
         {
             printf("Not Found!!\n");
             break;
         }
         if( head->number == key)//删除节点
         {
             head = pointer->Next;//指向
             free(pointer);
             break;
         }
         back = pointer;//??
         pointer = pointer->Next;

         if( pointer->number == key)//插入链表的中部或尾端
         {
             back->Next = pointer->Next;//改接
             free(pointer);
             break;
         }
     }
     return head;

 }
//插入节点至链表内
link Insert_list(link head, link new, link key)
{
    link pointer;//节点的声明
    pointer = head;//pointer设为首节点

    while(1)
    {
        if( pointer == NULL)//插入在首节点前
        {
            new -> Next = head;
            head = new;//将new->Next尾端指向head
            break;
        }
        if( pointer ->number == key )//插入链表中部或尾端
        {
            new -> Next = pointer -> Next;
            pointer -> Next = new;
            break;
        }
            pointer = pointer->Next;//前往下一节点
    }
    return head;

}

//输出链表数据
void Print_list(link head)
{
    link pointer;

    pointer = head;//pointer设为首节点

    while(pointer != NULL)
    {
        printf("[%d,%d]",pointer->number,pointer->total);
        pointer = pointer->Next;
    }
    printf(" \n ");

}
//释放链表
void Free_list(link head)
{
    link pointer;
   while(head != NULL)
    {
        pointer = head;
        head = head->Next;
        free(pointer);
    }
}

//建立链表
link Create_list(link head)
{
    link pointer;
    link new;
    int i;

    head = (link)malloc(sizeof(Node));

    if( head == NULL)
    {
        printf("Memory allocate failure!! \n");
    }
    else
    {
        head ->number = data[0][0];
        head ->total = data[1][0];
        head ->Next = NULL;//数据的初始化

        pointer = head;//pointer节点设为首节点

        for (i=0; i <= Max; i++)
        {
            new = (link)malloc(sizeof(Node));

            new -> number = data[0][i];
            new -> total = data[1][i];
            new -> Next = NULL;

            pointer -> Next = new;//将新链表窜连在原链表尾部
            pointer = new;//列表尾端节点为新节点
        }
    }
    return head;
}

//主程序
void main()
{
    link head;
    link new;
    int key;
    int n;

    head = Create_list(head);//调用建立链表

    if( head != NULL)
    {
        Print_list(head);
        printf("选择\n1 delete  2 insert \n");
        scanf("%d",&n);
        switch(n)
        {
            case 1:
            printf("delete");
            while(1)
        {
            printf("Input 0 to EXIT\n");
                new = (link)malloc(sizeof(Node));
                printf("Please input the data number : ");
                scanf("%d",&new->number);
            if(new->number == 0)
                break;
                printf("Please input the data total :");
                scanf("%d",&new -> total);

                printf("Please input the data number for insert : ");
                scanf("%d",&key);

                head = Insert_list( head,new, key);
                Print_list(head);
        }
        break;

            case 2:
            printf("insert");
            while(1)
        {
            printf("Input 0 to EXIT!\n");
        printf("Please input the data to delete : ");
            scanf("%d",&key);

            if( key == 0)
            break;

            head = Delete_list(head,key);//调用插入节点
            Print_list(head);//调出链表数据
        }
        break;

            default :
            printf("error!\n");
            break;
        }
        Free_list(head);
    }
}


宁静致远
2012-01-21 22:44
唯佳
Rank: 2
等 级:论坛游民
帖 子:42
专家分:44
注 册:2012-1-11
得分:0 
貌似 见过
前几天 想删除与插入
今天 加了一switch语句
将两者合二为一了

宁静致远
2012-01-21 22:46
唯佳
Rank: 2
等 级:论坛游民
帖 子:42
专家分:44
注 册:2012-1-11
得分:0 
你那么多头文件 啥用
我的警告信息 特多
你也给我改改 互相学习

宁静致远
2012-01-21 22:50
墨清扬
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:1
帖 子:294
专家分:817
注 册:2011-10-4
得分:7 
楼主能否说清楚点,在哪里插不进去?测试了一下这个程序有其他一些问题,比如提示输入却不读取,还有插入的时候有时会非法访问

酱油实习生
2012-01-23 23:33
xs47968224
Rank: 2
等 级:论坛游民
帖 子:18
专家分:52
注 册:2010-3-11
得分:7 
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# include <conio.h>

typedef struct student
{
    int id;
    char name[15];
    float age;
    int record;
    struct student * next;
}ST, *PST;

PST create(void)
{
    PST p=NULL, phead=NULL, tail=NULL;
    char n;
    int sun=1;
    float t;
    do
    {
        p=(PST)malloc(sizeof(ST));
        printf("请输入学生的学号:");
        scanf("%d", &p->id);
        printf("请输入学生的名字:");
        scanf("%s", p->name);
        printf("请输入学生的成绩:");
        scanf("%f", &t);
        p->age=t;
        p->record=sun;
        p->next=NULL;
        ++sun;

        if(p==NULL)
        {
            printf("分配失败\n");
            exit(1);
        }
        if(phead==NULL)
        {
            phead=tail=p;
        }
        else
        {
            tail->next=p;
            tail=tail->next;
        }
        

        fflush(stdin);
        printf("是否继续输入学生信息:(y/n?)");
        scanf("%c", &n);
    }while(n=='y');
    printf("按任意键返回...");
    getche();

    return phead;
}


void print(PST phead)
{
    PST p=phead;
    int n=1;
    while(p!=NULL)
    {
        printf("第%d个人的学号:%d 名字:%s 成绩:%f \n", n, p->id, p->name, p->age);
        p=p->next;
        ++n;
    }
    printf("按任意键返回...");
    getche();
}


PST insert(PST phead)
{

        PST pre, p, cur,s,f;
        int n, c=1, d=0;
        cur=phead;
        

        p=(PST)malloc(sizeof(ST));
        printf("请输入你要插入学生的学号:");
        scanf("%d", &p->id);
        printf("请输入你要插入学生的名字:");
        scanf("%s", p->name);
        printf("请输入你要插入学生的成绩:");
        scanf("%f", &p->age);
        p->next=NULL;


        if(phead==NULL)
        {
            phead=p;
            phead->record=1;
            printf("没有学生,此为第一个学生的信息\n");
            printf("按任意键返回...");
            getche();
            return phead;
        }

        printf("****请输入你要插入学生的位置****");
        scanf("%d", &n);

        if(cur->record==n)
        {
            phead=p;
            phead->next=cur;  //  是否等于p->next = cur;
            s=phead;
            while(s->next!=NULL)
            {
                s->record=c;
                ++c;
                s=s->next;
            }
            printf("已经往第1名学生前放了.\n按任意键继续");
            getche();
            return phead;
        }

        while(cur->next!=NULL)
        {
            pre=cur;
            cur=cur->next;
            if(cur->record==n)
                break;
        }

        if(cur->record==n)
        {
            pre->next=p;
            p->next=cur;

            d=pre->record+1;
            f=p;
            while(f!=NULL)
            {
                f->record=d;
                ++d;
                f=f->next;
            }
            f->record=d;
            printf("在中间插入了学生信息...\n");
        }
        else
        {

            cur->next=p;
            p->record=cur->record+1;
            printf("在最后插入了学生信息...\n");
        }
        printf("按任意键继续...");
        getche();
        return phead;
}



int main(void)
{
    int n;
    PST phead=NULL;
    do
    {
        printf("请选择:\n1.新建  2.打印  3.插入  4.删除  5.退出\n请输入您的选择:\n");
        scanf("%d", &n);
        switch(n)
        {
            case 1: phead=create(); break;
            case 2: print(phead); break; printf("按任意键继续..."); getche(); break;
            case 3: phead=insert(phead); break;
        //    case 4: phead=del(phead); printf("按任意键继续..."); getche(); break;
            case 5: printf("bye bye !!!!!!\n"); exit(0); break;
            default:printf("输入非法,请重新输入\n");
        }
        system("cls");
    }while(1);


    return 0;
}

稍稍小小改动了一下基本书写顺序,可以运行了,尽量把定义写在函数的开始,当然,也有说是写在须要用变量的时候,在接近的地方写上定义,但是你PST开头也写了定义,后面又定义PST s ,PST f的。。。我改到前面去了,别的好像没什么问题啊,插入没问题吧?难道我的编译器能通过?

欢迎来访-> 小Z博客
2012-01-27 12:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-359929-1-1.html




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

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