删除相同元素(线性表)

Home Web Board ProblemSet Standing Status Statistics
OJ系统新功能测试中,如有问题请联系 17865569030 17865569180 17865571035 尽量不要在上课时间打电话

Problem G: 删除相同元素(线性表)

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 921   Solved: 631
[ Submit][ Status][ Web Board]

Description

(线性表)在一个递增有序的线性表中,有数值相同的元素存在。若存储方式为单链表,设计算法去掉数值相同的元素,使表中不再有重复的元素。

Input

输入长度:6

输入数据:2 3 4 5 5 7

Output

2 3 4 5 7

Sample Input

6
8 9 10 11 22 22

Sample Output

8 9 10 11 22 

HINT

[ Submit][ Status][ Web Board]
#include <stdio.h>
#include <stdlib.h>


typedef struct node{
	
	int date ;
	struct node *next ;
	
}Node;
Node *creat_list(int n)
{
	Node *head =NULL ;
	Node *last ;
	Node *p;
	int i ,m ;
	for(i=0;i<n ;i++)
	{
		scanf("%d",&m);
		p=(Node* )malloc(sizeof(Node));
		p->date =m ;
		p->next =NULL ;
		if(head==NULL)
		{
			head=p;
		}
		else
		{
			last->next =p ;
		}
		last=p;
		
	}
	return head ;
	
	
 } 
 void show(Node *head)
 {
 	Node *p = head ;
 	while(p)
 	{
 		printf("%d ",p->date);
 		p=p->next ;
 		
	 }
 	printf("\n");
 	
 }
Node *quchong(Node *head)
{
	Node *p1 ;
	Node *p2=head;
	do{
		p1=p2;
		p2=p2->next ;
		if(p2->date==p1->date)
		{
			p1->next =p2->next ;
		}
		
	}while(p2->next!=NULL);
	return head;
	
}
int main(int argc, char *argv[]) {
	
	Node *head =NULL;
	
	int n ,b;
	scanf("%d",&n);
	head =creat_list(n);
quchong(head);
	show(head);

	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/80821839
今日推荐