华为机试题(10)--删除单向链表中的某些节点

本文介绍了一个单向链表的处理方法,包括删除重复节点并逆序排列。通过C语言实现,展示了链表创建、显示、销毁及逆序的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入一个不带头节点的单向链表(链表的节点数小于100),删除链表中内容重复的节点(重复的节点全部删除),剩余的节点逆序倒排。

【输入】 pstrIn:输入一个不带头节点的单向链表

【输出】 pstrOut:删除内容重复的节点后,逆序排列的链表(不带头节点,链表第一个节点的内存已经申请)

    

示例:

输入链表的内容依次为 6,7,8,8,9,10,6

则输出链表的内容依次应该是 10,9,7

#include<stdio.h> 
#include <stdlib.h>

typedef struct node//节点存放一个数据和指向下一个节点的指针
{
	int data;
	struct node* pnext;
} Node;

Node *link_create()//链表创建
{
	int item;
	Node *head = NULL;
	do
	{
		Node *p;
		scanf("%d",&item);
		p = (Node *)malloc(sizeof(Node));
		if(p == NULL)
		{
			printf("memory applied failed\n");
			break;
		}
		p->data = item;
		p->pnext = head;
		head = p;
	}while(getchar() != '\n');
	return head;
}

void link_show(Node *head)
{
	Node* p;
	p=head;
	while(p != NULL)
	{
		printf("%d ",p->data);
		p = p->pnext;
	}
	printf("\n");
}

void link_destroy(Node *head)
{
	Node* p;
	Node* tmp;
	p=head;
	while(p != NULL)
	{
		tmp = p->pnext;
		free(p);
		p = tmp;
	}
}

Node *link_reverse(Node *head)
{
	Node *pre,*cur,*next;
	/*head->pnext =NULL;*/

	pre = head;
	cur = pre->pnext;
	next = cur->pnext;
	head->pnext =NULL;//第一次的pre,cur,next

	if(next == NULL)//链表只有两个节点,如果没有此语句,当链表确实只有两个节点时,就会发生错误。
	{
		cur->pnext = pre;
		head = cur;
		return head;
	}

	while(next->pnext != NULL)
	{
		cur->pnext = pre;//修改指针,每次循环修改一次

		pre = cur;
		cur = next;
		next = next->pnext;
	}//循环终止时,next->pnext == NULL
	cur->pnext = pre;
	next->pnext = cur;
	head = next;
	return head;

}


Node * link_vChanProcess(Node * pstrIn/*,Node * pstrOut*/)
{
	Node *p = pstrIn;
	Node *pre,*cur,*next,*pstrOut,*tmp;
	int temp[100];
	int m=0,i,cnt;
	pre = pstrIn;
	while(p != NULL)
	{
		temp[m]= p->data;
		p = p->pnext;
		m++;
	}
	while(pre != NULL)
	{
		cnt = 0;
		for(i=0 ;i<m;i++)//判断pre 有没有重复,该不该删?
			if(pre->data == temp[i])
				cnt++;
		printf("cnt = %d\n",cnt);
		if(cnt > 1)
		{
			tmp = pre;
			pre = pre->pnext;
			free(tmp);

		}
		else
		{
			cur = pre->pnext;
			next = cur->pnext;
			break;
		}
	}
	pstrOut = pre;

	while(next != NULL)
	{
		cnt = 0;
		for(i=0 ;i<m;i++)//判断cur 有没有重复,该不该删?
			if(cur->data == temp[i])
				cnt++;
		if(cnt > 1 )
		{
			free(cur);
			pre->pnext = next;
			cur = next;
			next = cur->pnext;
		}
		else
		{
			pre = cur;
			cur = next;
			next = cur->pnext;
		}
	}

	//循环结束后,next == NULL
	cnt = 0;
	for(i=0 ;i<m;i++)//判断最后一个节点 有没有重复,该不该删?
		if(cur->data == temp[i])
			cnt++;
	if(cnt > 1 )
	{
		pre->pnext = NULL;
		free(cur);
	}

	return pstrOut;
}


int main()
{
	//Node *new_head;
	Node *pstrOut;
	Node *head = link_create();
	link_show(head);
	pstrOut = link_vChanProcess(head/*,pstrOut*/);
	//new_head = link_reverse(head);
	link_show(pstrOut);
	link_destroy(pstrOut);

	system("pause");
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值