建立两个单向链表a和b,然后从a中删除那些在b中存在的节点。求如上题用C语言写的程序!要用链表

如题所述

#include <stdio.h>  
#include <string.h>  
#define LA 5  
#define LB 4  
struct student   
{  
    int num;  
    char name[10];  
    struct student *next;  
}a[LA],b[LB];  
void main()  
{  
    struct student a[LA]={{101,"zhang"},{102,"li"},{103,"wang"},{104,"niu"},{105,"liu"}};  
    struct student b[LB]={{103,"zhu"},{105,"huang"},{106,"zhao"},{107,"qian"}};  
    struct student *heada,*headb,*pa1,*pa2,*pb1;  
    int i;  
    heada=a;//指向a链表  
    headb=b;//指向b链表  
    //对链表a赋地址并且输出  
    printf("a链表:\n");  
    for (pa1=heada,i=1;i<=LA;i++)  
    {       
        if(i<LA)  
          pa1->next=a+i;  
        else  
            pa1->next=NULL;  
        printf("%4d%8s\n",pa1->num,pa1->name);  
        if(i<LA)  
        pa1=pa1->next;  
    }  
  
    //对链表b赋地址并且输出  
    printf("b链表:\n");  
    for (pb1=headb,i=1;i<=LB;i++)  
    {  
        if(i==LB)  
            pb1->next=NULL;  
        else   
            pb1->next=b+i;  
            printf("%4d%8s\n",pb1->num,pb1->name);  
        if(i<LB)  
            pb1=pb1->next;  
    }  
    //从a中找出与b相同的节点并且删除  
    pa1=heada;//初始化指针  
    while (pa1!=NULL)//只要链表a没有到结尾就执行  
    {   pb1=headb;  
        while((pa1->num != pb1->num) && (pb1->next!=NULL))//没有找到相等的值且b表没有到结尾  
        {pb1=pb1->next;}//b表向后移动  
        if (pa1->num==pb1->num)//找到了  
        {  
            if(pa1==heada)  
            heada=pa1->next;//头结点直接指向下一结点  
            else  
            {  
                pa2->next=pa1->next;//乍一看觉得pa2没有指向,但能进入这个else说明绝对有一次没有找到过,  
                pa1=pa1->next;      //即pa1向后移动过,所以pa2是有所指的  
            }  
        }  
        else  
        {pa2=pa1;  
         pa1=pa1->next;  
        }   
    }  
    //输出新表  
    printf("现在a链表如下:\n");  
    pa1=heada;  
    while(pa1!=NULL)  
    {  
        printf("%4d%8s\n",pa1->num,pa1->name);  
        pa1=pa1->next;  
    }  
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-03-26
#include <stdio.h>
typedef struct node{
int value;
struct node *pointer;
}link;

link *create(int *array,int num){//(数组,数组元素个数)使用数组创建单链表
link *head=NULL,*temp1,*temp2;
int i=0;
if(num>1){
head=(link *)malloc(sizeof(link));
temp1=head;
temp1->value=array[0];
temp2=temp1->pointer;
}
for(i=1;i<num;i++){
temp2=(link *)malloc(sizeof(link));
temp2->value=array[i];
temp1->pointer=temp2;
temp1=temp2;
}
temp1->pointer=NULL;
return head;
}

void show(link *temp){//显示链表
while(temp!=NULL){
printf("%d,",temp->value);
temp=temp->pointer;
}
printf("\n");
}

link *del(link *a,link *b){//删除链表a中与链表b相同的元素
link *tempa,*tempb,*prev,*del;
tempa=a;
prev=NULL;
while(tempa!=NULL){//遍历链表a
tempb=b;
while(tempb!=NULL){//遍历链表b
if(tempb->value==tempa->value){//找到a,b相同值的节点
del=tempa;
if(tempa==a){//找到的节点为头节点
a=a->pointer;
tempa=a;
free(del);
}else{
prev->pointer=tempa->pointer;
tempa=prev;
    free(del);
}
}
tempb=tempb->pointer;
}
prev=tempa;
tempa=tempa->pointer;
}
return a;
}

int main(){
int array1[6]={0,1,3,4,6,9},array2[2]={1,4};
link *a,*b;
a=create(array1,6);//创建链表a,1-2-3-4-6-9
printf("create link a:");
show(a);
b=create(array2,2);//创建链表a,1-4
printf("create link b:");
show(b);
a=del(a,b);
printf("del later link a:");
show(a);
return 0;
}

很久没写C语言了,这个是我完全按照你的要求写的,不是copy的。

追问

嗯嗯,我看看

本回答被提问者和网友采纳
第2个回答  2016-01-11

1、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();

}

2、测试环境c-free5的标准C。

相似回答