• SDUT_2122 数据结构实验之链表七:单链表中重复元素的删除


    提交代码

    数据结构实验之链表七:单链表中重复元素的删除

    Time Limit: 1000MS Memory Limit: 65536KB

    Problem Description

    按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。

    Input

    第一行输入元素个数 n (1 <= n <= 15);
    第二行输入 n 个整数,保证在 int 范围内。

    Output

    第一行输出初始链表元素个数;
    第二行输出按照逆位序所建立的初始链表;
    第三行输出删除重复元素后的单链表元素个数;
    第四行输出删除重复元素后的单链表。

    Example Input

    10
    21 30 14 55 32 63 11 30 55 30

    Example Output

    10
    30 55 30 11 63 32 55 14 30 21
    7
    30 55 11 63 32 14 21

    Hint

    Author

    不得使用数组!
    #include <iostream>
    using namespace std;
    struct node
    {
    	int num;
    	node *next;
    };
    void display(struct node *head)
    {
    		while(head!=NULL)
    		{
    			if(head->next!=NULL)
    				cout<<head->num<<' ';
    			else
    				cout<<head->num<<endl;
    			head=head->next;
    		}
    }
    void Delete(struct node *head,int n)
    {
    	node *p,*p1,*p2;
    	p=head;
    	while(p->next!=NULL)//刚遍历到最后一个节点时就结束,不对最后一个节点进行判断(因为在前面判断出 前面的节点值和最后一个节点值都不想等)
    	{
    		p1=p;//p1作为索引,在遇到与p相等的值的时候,删除相等的节点
    		p2=p->next;//由p后面的一个值开始进行遍历
    		while(p2!=NULL)//后面的值不为空节点时进行循环
    		{
    			if(p->num==p2->num)//在while(p2!=NULL)循环中p->num的保持不变
    			{
    				p1->next=p2->next;//遇到相等的值的时候索引p1的值开始发生作用
    				p2=p2->next;//删除节点
    				n--;
    			}
    			else
    			{
    				p1=p1->next;//遇到不想等的时候,P1也要跟随变化,用于在遇到相等的时候发生作用
    				p2=p2->next;
    			}
    		}
    			p=p->next;//对下一个值进行判断
    	}
    		cout<<n<<endl;
    		display(head);
    }
    void create_list(struct node *head,int n)
    {
    	node *p,*q;
    	//	head=new node;
    	head->next=NULL;
    	for(int i=0;i<n;i++)
    	{
    		p=new node;
    		cin>>p->num;
    		p->next=head->next;
    		head->next=p;//从头节点入手,进行中间插入,head->num并没有值
    	}
    	cout<<n<<endl;
    	display(head->next);
    }
    int main()
    {
    	node *head;
    	head=new node;
    	int n;
    	cin>>n;
    	create_list(head,n);
    		Delete(head->next,n);
    		return 0;
    }
    
    下面的段代码一直Runtime Error
    #include <iostream>
    using namespace std;
    struct node
    {
    	int num;
    	node *next;
    };
    void display(struct node *head)
    {
    		while(head!=NULL)
    		{
    			if(head->next!=NULL)
    				cout<<head->num<<' ';
    			else
    				cout<<head->num<<endl;
    			head=head->next;
    		}
    }
    struct node *Delete(struct node *head,int n)
    {
    	node *p,*p1,*p2;
    	p=head;
    	while(p->next!=NULL)//刚遍历到最后一个节点时就结束,不对最后一个节点进行判断(因为在前面判断出 前面的节点值和最后一个节点值都不想等)
    	{
    		p1=p;//p1作为索引,在遇到与p相等的值的时候,删除相等的节点
    		p2=p->next;//由p后面的一个值开始进行遍历
    		while(p2!=NULL)//后面的值不为空节点时进行循环
    		{
    			if(p->num==p2->num)//在while(p2!=NULL)循环中p->num的保持不变
    			{
    				p1->next=p2->next;//遇到相等的值的时候索引p1的值开始发生作用
    				p2=p2->next;//删除节点
    				n--;
    			}
    			else
    			{
    				p1=p1->next;//遇到不想等的时候,P1也要跟随变化,用于在遇到相等的时候发生作用
    				p2=p2->next;
    			}
    		}
    			p=p->next;//对下一个值进行判断
    	}
    		cout<<n<<endl;
    		return head;
    }
    struct node *create_list(struct node *head,int n)
    {
    	node *p,*q;
    	head->next=NULL;
    	for(int i=0;i<n;i++)
    	{
    		p=new node;
    		cin>>p->num;
    		p->next=head->next;
    		head->next=p;
    	}
    	return head;
    }
    int main()
    {
    	node *head;
    	head=new node;
    	int n;
    	cin>>n;
    	create_list(head,n);
    	cout<<n<<endl;
    		display(head->next);
    		Delete(head->next,n);
    		display(head->next);
    		return 0;
    }
    


  • 相关阅读:
    Python strip()方法
    C#操作计划任务
    C# Task的用法
    C#异步编程之浅谈Task
    [深入学习C#]C#实现多线程的方法:线程(Thread类)和线程池(ThreadPool)
    [深入学习C#]C#实现多线程的方式:使用Parallel类
    详细的.Net并行编程高级教程--Parallel
    5天玩转C#并行和多线程编程 —— 第一天 认识Parallel
    C# 并行任务——Parallel类
    C#多线程--线程池(ThreadPool)
  • 原文地址:https://www.cnblogs.com/sxy201658506207/p/7586265.html
Copyright © 2020-2023  润新知