注册 登录
编程论坛 数据结构与算法

求助-C数据结构

q495321651 发布于 2012-10-01 21:07, 426 次点击
/* Note:Your choice is C IDE */
#include "stdio.h"
#include<stdlib.h>
typedef struct node
{
    int date;
    struct node *next;
}node;

void sclist (node *h)
{
    node *p,*r,*q,*s;
    p=h->next;
    r=h->next;
    q=h->next;
    p=p->next;
    while(p!=NULL)
    {   
        while(p!=NULL)
        {
            
            if(q->date==p->date)
            {
                r->next=p->next;
                s=p;
                p=p->next;
                free(s);
                r=p;
                p=r->next;
            }
            else
            {
                r=r->next;
                p=p->next;
            }
        }
        q=q->next;
        r=q;
        p=r->next;
    }
}

void input(node *h)
{
    node *p,*r;
    int x;
    r=h;
    printf("请输入你的数据:");
    scanf("%d",&x);
    while(x!=-999)
    {
        p=(node *)malloc(sizeof(node));
        p->date=x;
        r->next=p;
        p->next=NULL;
        r=p;
        scanf("%d",&x);
    }
}

void putlist(node *h)
{
    node *p;
    p=h->next;
    while(p!=NULL)
    {
        printf("%d ",p->date);
        p=p->next;
    }
   
}
void main()
{
    node *h;
    h=(node *)malloc(sizeof(node));
    h->next=NULL;
    input(h);
    putlist(h);
    sclist(h);
    putlist(h);
}
想实现; 删除一组数中重复的数
 例如:
      输入:1 2 1 2 2 2 2 5
输出;       1 2 5
1 回复
#2
寒风中的细雨2012-10-01 21:32
单链表  前插入 有问题
程序代码:
    while(x!=-999)
    {
        p=(node *)malloc(sizeof(node));
        p->date=x;
        r->next=p;
        p->next=NULL;
        r=p;
        scanf("%d",&x);
    }

这个没有注释比较痛苦读起来  特别是if else 分支
void sclist (node *h){}

1