| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 388 人关注过本帖
标题:动态链表的创建部分貌似哪里的指针有问题,请大家帮忙看看
只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:20 回复次数:1 
动态链表的创建部分貌似哪里的指针有问题,请大家帮忙看看
程序代码:
/*
*.编程实现一个单向项链表,能实现链表的创建、插入、删除、显示功能。  
*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
struct stu
{
    char name[20];
    long  num;
    float score;
    struct stu *next;
};
int n;

 stu *create()
{
    stu *head,*pNext,*p;
    short i,N;
    head=(stu *)malloc(sizeof(stu));
    p=head;
    printf("input the number of student:\n");
    scanf("%d",&N);
    printf("input name and number for  nodes:\n");
    scanf("%c,%ld,%f",head->name,&head->num,&head->score);//为头结点赋值
    for(i=0;i<N;i++)
    {
        pNext=(stu *)malloc(sizeof(stu));//连续开辟结构体空间
        scanf("%c,%ld,%f",pNext->name,&pNext->num,&pNext->score);
        p->next=pNext;
        p=pNext;
    }
    //printf("please input the students' information:\n");
    /*for(i=1;i<N;i++)
    {
       
        p=p->next;
    }*/
    cout<<head<<endl;
    pNext->next=NULL;
    return head;
}
void print(stu *head)//查询链表
{
    stu *p;
    printf("Output the infomation in the chain list:\n");
    p=head;
    if(head!=NULL)
    do
    {
        printf("%c,%ld,%f",p->name,p->num,p->score);
        p=p->next;
    }while(p!=NULL);
}

stu *insert(stu * head,stu *pInsert)
{

    stu *temp,*p,*pIns;
    p=head;
    pIns=pInsert;//指向要插入的结点
    if(head==NULL)
    {
        head=pIns;
        pIns->next=NULL;
    }
    else
    {
        while((pIns->num>p->num)&&(p->next!=NULL))
        {
           
            temp=p;//temp指向刚才p所指的结点
            p=p->next;//p后移一个结点
   
        }
           
        if(pIns->num<=p->num)
        {
            if(head==p)
                head=pIns;//插到原来第一个结点之前
            else
            {
                temp->next=pIns;
                pIns->next=p;
            }
        }
        else//一定是为结点
        {
            p->next=pIns;
            pIns->next=NULL;
        }
    }
    n=n+1;
    printf("insert success!");
    return head;
}
stu *dele(stu *head,long num)
{

    stu *p1,*p2;
    if(head==NULL)
        printf("empty list!\n");
    p1=head;
    while(p1->num!=num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->num==num)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
        n=n-1;
        free(p1);
    }
    else
        printf("the node not be found!\n");
    return head;
}

int main()
{
    stu *cl,*stud;
    int i;
    long num;
    printf("*************************************************\n");
    printf(            "select the item                    \n");
    printf(            "1:set the students' information       \n");
    printf(            "2:query the students' information  \n");
    printf(            "3:insert the students'information  \n");
    printf(            "4:delete the students'information  \n");
    printf(            "0:ESC the program!                    \n");
    printf("*************************************************\n");
    do
    {
        printf("select the number to next");
        scanf("%d",&i);
        switch(i)
        {
            case 1:
                {
                    //printf("Please input the students' information:\n");
                    cl=create();
                    cout<<cl<<endl;
                }break;
            case 2:
                {
                    print(cl);
                }break;
            case 3:
                {
                    printf("Please input the students' information:\n");
                    scanf("%c,%ld,%f",stud->name,&stud->num,&stud->score);
                    cl=insert(cl,stud);
                }break;
                case 4:
                {
                    printf("Please input the delete number:\n");
                    scanf("%ld",&stud->num);
                    cl=dele(cl,num);
                }break;
             case 5:
                    exit(0);       
            default:break;
        }
    }while(i!=0);
        return 0;
}
搜索更多相关主题的帖子: 编程 动态 
2011-06-24 10:50
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:20 
if (head == p) {
    head = pIns;        // 插到原来第一个结点之前
    head->next = p;   // 要跟后面的链表接上啊,不然全都丢了
} else ...


而且楼主这么写多麻烦,搞个头结点,比这方便多了
2011-06-24 11:13
快速回复:动态链表的创建部分貌似哪里的指针有问题,请大家帮忙看看
数据加载中...
 
   



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

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