用C语言编写实现删除单链表重复节点的算法 a -> b -> a -> c -> c 变成

-> b -> c

给你个参考例子,自己去改吧。

#include <stdio.h>
#define Len sizeof(struct student)
struct student
{
long num;
float score;
struct student * next;
};
int n;

int main()
{
struct student *a=0,*b=0;
struct student *p1,*p2,*p3;

n=0;
while(n<5)/*建立链表a*/ 
{
n+=1;
p1=(struct student *)malloc(Len);
if(n==1) 

  a=p1;
  p2=p1;
}
else 
{
  p2->next=p1;
  p2=p1;
}
p1->num=n;
p1->score=80+n;
}
p1->next=0;

n=0;
while(n<3)/*建立b链表*/ 
{
n+=1;
p1=(struct student *)malloc(Len);
if(n==1) 

  b=p1;
  p2=p1;
}
else 
{
  p2->next=p1;
  p2=p1;
}
p1->num=n+1;
p1->score=80+n+1;
}
p1->next=0;

p1=a;
while(p1->next!=0)/*显示a链表*/
{
  printf("%d %0.2f   ",p1->num,p1->score);
  p1=p1->next;
}
printf("\n");

p1=b;
while(p1->next!=0)/*显示b链表*/
{
  printf("%d %0.2f   ",p1->num,p1->score);
  p1=p1->next;
}
printf("\n\n");

p3=b;
while (p3->next!=0)/*查找删除a中的b*/ 
{
   p1=a;
   while((p3->num!=p1->num)&&(p1->next!=0))
   {p2=p1;p1=p1->next;}
   if(p3->num==p1->num)/*找到一个*/
   {
    if(p1==a)
      a=p1->next;
    else
      p2->next=p1->next;
      printf("delete:%ld\n",p3->num);
    }
    else printf("%ld not been found!\n",p3->num);
    p3=p3->next;   
}

printf("\n");

p1=a;
while(p1->next!=0)/*显示删除b后的a链表*/
{
  printf("%d %0.2f   ",p1->num,p1->score);
  p1=p1->next;
}
printf("\n\npress enter end...");
getchar();

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-01-16
C语言版数据结构书上有一大把。
相似回答