| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 567 人关注过本帖
标题:求求各位大虾帮忙看看这个问题啊。
只看楼主 加入收藏
hjmHhyp
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-10-9
收藏
 问题点数:0 回复次数:2 
求求各位大虾帮忙看看这个问题啊。
题目:有2个链表a,b,设结点中包含学号、姓名。从a链表中
删去和b链表中相同的学号的那些结点。

自己写的代码:
#include "stdio.h"
#include "malloc.h"

void print(struct student *head);
struct student *creat();
struct student *del(struct student *head,long num);


struct student
{
    int num;
    char name;
    struct student *next;
};
int n;

void main()
{
    struct student *p1,*p2,*p3;
    p1=creat();
    p2=creat();
    for(p2;p2->next!=NULL;p2++)
    p3=del(p1,p2->next->num);
    print(p3);
}


struct student *creat()
{
    struct student *head,*p1,*p2;
    int n;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    cin>>p1->num>>p1->name;
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(sizeof(struct student));
        cin>>p1->num>>p1->name;

    }
    p2->next=NULL;
    return head;
}

void print(struct student *head)
{
    struct student *p;
    p=head;
    cout<<"Now,These "<<n<<"records are:";
    if(head!=NULL)
    {

        do
        {
            cout<<p->num<<p->name;
            p=p->next;
        }while(p!=NULL);
    }
}

struct student *del(struct student *head,long num)
{
    struct student *p1,*p2;
    if(head==NULL){cout<<"List null";}
    p1=head;
    while(num!=p1->num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
            head=p1->next;
        else p2->next=p1->next;
        cout<<"delete "<<num;
        n=n-1;
    }
    else cout<<num<<"not been found!";
    return head;

}
运行和编译都通过了,但是自己逐步调试的时候发现运行到for(p2;p2->next!=NULL;p2++);的时候出现错误。自己主要是想通过这一句p2=creat();将num调出来,以便删除函数p3=del(p1,p2->next->num);能够删除p1=creat();中和p2=creat();相同num的结点,而达到题目的要求。
2008-10-12 11:57
hjmHhyp
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-10-9
收藏
得分:0 
希望各位大虾各抒己见,小弟谢谢了!
2008-10-12 11:59
jyycom
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2006-5-18
收藏
得分:0 
// 我也是菜鸟,抛砖引玉//

#include "stdafx.h"
#include <iostream.h>

#include "stdio.h"
#include "malloc.h"

void print(struct student *head);
struct student *creat();
void del(struct student *head,int num);


struct student
{
    int num;
    char name[5];
    struct student *next;
};
 
void main()
{
    struct student *p1,*p2,*p3;
    p1=creat();
    p2=creat();
    p3=p2;
    cout<<"p1 is "<<endl;
    print(p1);
    cout<<"p2 is "<<endl;
    print(p2);
   cout<<"end"<<endl;
    while (p3)
    {
        del(p1,p3->num);
        p3=p3->next;
        
    }
    cout<<"p1 after del"<<endl;
        print(p1);
   
}


struct student *creat()
{
    struct student *head,*p1,*p2;
 
    p1=(struct student*)malloc(sizeof(struct student));
    cin>>p1->num>>p1->name;
    p1->next=NULL;
    head=p1;
    
    while(1)
    {   
        int temp;
        cin>>temp;
        if (temp)
        {
            p2=(struct student *)malloc(sizeof(struct student));
            p2->num=temp;
            cin>>p2->name;
            p2->next=NULL;
            p1->next=p2;
            p1=p2;
        }
        else
        {
            cout<<"builded!"<<endl;
            return head;
        }    
        
        
    }
   
}

void print(struct student *head)
{
    while (head!=NULL)
    {
        cout<<"num is "<<head->num<<endl;
        cout<<"name is "<<head->name<<endl;
        head=head->next;

    }
}

void del(struct student *head,int num)
{
    struct student *p,*q;
    p=q=head;
    
    while (p)
    {
        if(p->num==num)
            break;
        q=p;
        p=p->next;
    }
    if (p!=NULL)
    { q->next=p->next;
    free(p);
    }
   

}

重新学习C语言!
2008-10-12 20:08
快速回复:求求各位大虾帮忙看看这个问题啊。
数据加载中...
 
   



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

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