删除相同的结点

题目:有两个链表a和b,设结点中包含学号和姓名。从a链表中删去与b链表中有相同学号的那些结点。

// 删除相同的结点.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#define NULL 0

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

int main(int argc, char* argv[])
{
	struct student a[3],b[3],*head,*tp,*temp;
	a[0].num=1;	a[0].name="曾小贤";	  a[0].next=&a[1];
	a[1].num=2;	a[1].name="黄渤";	  a[1].next=&a[2];
	a[2].num=3; a[2].name="罗大佑";   a[2].next=NULL;
	b[0].num=2; b[0].name="黄渤";	  b[0].next=&b[1];
	b[1].num=3; b[1].name="罗大佑";   b[1].next=&b[2];
	b[2].num=4; b[2].name="张飞";     b[2].next=NULL;	
	head=&a[0];		
	temp=head;
	do
	{
		for(int L=0;L<=2;L++)
		{
			if(temp->num==b[L].num)
			{	
				if(temp==head)
				{
					head=temp->next;
				}
				else if(temp->next==NULL) 
				{
					tp->next=NULL;
				}
				else 
				{
					tp->next=temp->next;
				}
			}		
		}
		tp=temp;
		temp=temp->next;
	}while(temp!=NULL);		
	do
	{
		printf("%d,%s\n",head->num,head->name);
		head=head->next;
	}while(head!=NULL);
	return 0;
}

结果有误,希望广大读者能帮我分析一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值