| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1821 人关注过本帖
标题:单链表删除重复元素
只看楼主 加入收藏
倾城一笑
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2016-3-16
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
单链表删除重复元素
1212121 删除完之后变为12  
思路:先拿第一个元素与之后所有做对比,相同的都删除,然后在那第二个元素与后面元素做对比,相同的删除,以此类推
Node *CompareList(Node *pHead)//删除重复元素
{
    Node *pNext,*tmp,*p1;
    if(pHead == NULL)
    {
        printf("链表为空,无法操作\n");
        exit(0);
    }
    else
    {
        p1 = pHead;
        while(p1 !=NULL)
        {
            pNext = p1->next;
            while(pNext != NULL)
        {
        
            if(p1->Elem == pNext->Elem)
            {
                tmp = pNext->next;
                free(pNext);
                pNext = tmp;
            }
            else pNext = pNext->next;
        }
            p1 = p1->next;
        }
    }
    return pHead;
}
搜索更多相关主题的帖子: 元素 
2017-04-23 15:51
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:20 
差不多就是这样了。

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include "List.h"

void
Del( Node **RootPP );

int
main( void )
{
    Node *Root;
    int ix;

    Root = NULL;

    for( ix = 0; 20 > ix; ++ix )
        InsertList( &Root, ix % 3 );
    PrintList( Root, ' ' );
    printf( "\n" );
    Del( &Root );
    PrintList( Root, ' ' );
    return 0;
}

void
Del( Node **RootPP )//删除链表中重复的值
{
    Node *Current;
    Node *This;
    int t;

    while( NULL != ( Current = *RootPP ) )
    {
        RootPP = &Current->Next;
        t = Current->Value;
        while( NULL != ( This = *RootPP ) )
        {
            if( t == This->Value )
            {
                *RootPP = This->Next;
                free( This );
                continue;
            }
            RootPP = &This->Next;
        }
        RootPP = &Current->Next;
    }
}

程序代码:
//List.h
#ifndef List_H
#define List_H
typedef struct NODE {
    int Value;
    struct NODE *Next;
}Node; 

void 
PrintList( Node *RootP,char Spacech );
int 
InsertList( Node **RootP, int value );
void 
DeleteList( Node **RootP );
void 
DeleteNode( Node **RootP, int value );
Node *
FindNode( Node *Root, int value );
Node *
ReverseList( Node *first );
void
MergeList( Node **L1, Node **L2 );
/* MergeList() 将L2链表合并到L1。L1,L2是两个递增有序的链表。

 * 合并之后的链表同样是递增有序的链表。

 */
#endif


程序代码:
//List.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"

int 
InsertList( Node **RootP, int value )
{
    Node *NewCell;
    Node *Current;

    while( NULL != ( Current = *RootP ) /*&& value > Current->Value */)
        RootP = &Current->Next;
    
    NewCell = (Node *)malloc( sizeof(Node) );
    if( NULL == NewCell )
        return 0;
    NewCell->Value = value;
    NewCell->Next = Current;
    *RootP = NewCell;

    return 1;

}

void 
DeleteNode( Node **RootP , int value )
{
    Node *Current;

    while( NULL != ( Current = *RootP ) && value != Current->Value )
        RootP = &Current->Next;

    if( NULL == Current )
        return;
    *RootP = Current->Next;
    free( Current );
}

void 
PrintList( Node *RootP, char Spacech )
{
    Node *Current;

    while( NULL != ( Current = RootP ) )
    {
        printf("%d%c",Current->Value,Spacech);
        RootP = Current->Next;
    }
}


void 
DeleteList( Node **RootP )
{
    Node *Current;
    Node *Temp;

    Current = *RootP;
    *RootP = NULL;

    while( NULL != Current )
    {
        Temp = Current->Next;
        free( Current );
        Current = Temp;
    }
}


Node *
FindNode( Node *Root, int value )
{
    Node *Next;

    while( NULL != ( Next = Root ) && Next->Value != value )
        Root = Next->Next;

    return Next;
}

Node *
ReverseList( Node *first )
{
    Node *current;
    Node *next;

    for( current = NULL; NULL != first; first = next )
    {
        next = first->Next;
        first->Next = current;
        current = first;
    }

    return current;
}

void
MergeList( Node **L1, Node **L2 )
{
    Node *First, *Secondary;
    Node *Temp;

    for( First = *L1, Secondary = *L2; NULL != First; First = *L1 )
    {
        for( ; NULL != Secondary && First->Value >= Secondary->Value; Secondary = Temp )
        {
            Temp = Secondary->Next;
            *L1 = Secondary;
            Secondary->Next = First;
            L1 = &Secondary->Next;
        }
        L1 = &First->Next;
    }

    *L1 = Secondary;
}






[此贴子已经被作者于2017-4-23 19:16编辑过]


09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-04-23 19:10
快速回复:单链表删除重复元素
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.073953 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved