| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1662 人关注过本帖
标题:数据结构链表中删除重复元素,为什么运行出错呢?帮我看看吧 谢谢
只看楼主 加入收藏
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <malloc.h>
struct list{
    int data;
    struct list *next;
};
struct list *create(){
    int n;
    struct list *head,*p,*q;
    head=(struct list *)malloc(sizeof(struct list));
    head->next=NULL;
    q=head;
    printf("输入链表(0为结束):\n");
    scanf("%d",&n);
    while(n<=0){
        printf("值不正确\n");
        scanf("%d",&n);
    }
    while(n>0){
        p=(struct list *)malloc(sizeof(struct list));
        p->data=n;
        p->next=NULL;
        q->next=p;
        q=p;
        printf("下一个");
        scanf("%d",&n);
    }
    return head;
}
void shan(struct list *head){//删除链表中相等的元素
    struct list *p,*q,*s;
    q=head->next;
    while(q){//printf("a");  // q->next 改成q
        p=q->next;
        s=q;
        while(p){//printf("b");
            if(q->data==p->data){
                s->next=p->next;
                free(p);
                p=s->next;//printf("c");
            }else{//printf("d");
                p=p->next;
                s=s->next;
            }
        }//printf("e");
        q=q->next;//printf("f");
    }//printf("d");
}
void print(struct list *head){
    while(head->next){
        printf("%3d",head->next->data);
        head=head->next;
    }
}
void main(){
    struct list *head;
    head=create();
    print(head);
    printf("\n");
//    zhuanzhi1(head);
    shan(head);
    print(head);
}

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

梅尚程荀
马谭杨奚







                                                       
2012-03-05 19:32
adgvcxz
Rank: 2
等 级:论坛游民
帖 子:23
专家分:52
注 册:2009-6-26
收藏
得分:0 
回复 11楼 有容就大
  谢谢了   我再去看看为什么改了才正确   想了半天  谢谢啊。。。
2012-03-05 20:03
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:0 
楼主先说下你是用什么编译器
2012-03-06 09:21
快速回复:数据结构链表中删除重复元素,为什么运行出错呢?帮我看看吧 谢谢
数据加载中...
 
   



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

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