编写一算法,将单链表(带头结点)中,数值重复的节点删除。 使所得到的结果中各节点的值均不相等。
测试数据1212121
# include <iostream>
# include
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
}LinkList;
//新建后数据顺序不变
void CreateListF(LinkList *&L, ElemType a[], int n);
//新建后数据顺序倒置
void CreateListR(LinkList *&L, ElemType a[], int n);
void InitList(LinkList *&L);
void DestoryList(LinkList *&L);
int ListEmpty(LinkList * L);
int ListLength(LinkList *L);
void DisplayList(LinkList * L);
bool GetElem(LinkList *L, int i, ElemType &e);
int LocateElem(LinkList *L, const ElemType & e);
bool ListInsert(LinkList *&L, int i, const ElemType & e);
//去除链表中相同的元素
void delrepeat(LinkList *&L);
# include <iostream>
# include "LinkList.h"
void CreateListF(LinkList *&L, ElemType a[], int n)
{
LinkList * s;
int i;
L = (LinkList *) malloc(sizeof(LinkList));
L -> next = NULL;
for(i = 0; i < n; i++)
{
s = (LinkList *) malloc(sizeof(LinkList));
s -> data = a[i];
s -> next = L -> next;
L -> next = s;
}
}
void CreateListR(LinkList *&L, ElemType a[], int n)
{
LinkList * s, *r;
int i;
L = (LinkList *) malloc(sizeof(LinkList));
r = L;
for(i = 0; i < n; i++)
{
s = (LinkList *) malloc(sizeof(LinkList));
s -> data = a[i];
r -> next = s;
r = s;
}
}
void InitList(LinkList *&L)
{
L = (LinkList *) malloc(sizeof(LinkList));
L -> next = NULL;
}
void DestoryList(LinkList *&L)
{
LinkList * pre = L, *p = L -> next;
while(p != NULL)
{
free(pre);
pre = p;
p = pre -> next;
}
free(pre);
}
int ListEmpty(LinkList * L)
{
return (L -> next == NULL);
}
int ListLength(LinkList *L)
{
int n = 0;
LinkList * p = L;
while(p -> next != NULL)
{
n++;
p = p -> next;
}
return n;
}
void DisplayList(LinkList * L)
{
LinkList * p = L -> next;
while(p != NULL)
{
std::cout << p -> data;
p = p -> next;
}
std::cout << std::endl;
}
bool GetElem(LinkList *L, int i, ElemType &e)
{
int j = 0;
LinkList *p = L;
while(j < i && p != NULL)
{
j++;
p = p -> next;
}
if( p == NULL)
return false;
e = p -> data;
return true;
}
int LocateElem(LinkList *L, const ElemType & e)
{
int i = 1;
LinkList *p = L -> next;
while(p != NULL && p -> data != e)
{
p = p -> next;
i++;
}
if( p == NULL)
return 0;
return i;
}
bool ListInsert(LinkList *&L, int i, const ElemType & e)
{
int j = 0;
LinkList *p = L, *s;
while(j < i - 1 && p != NULL)
{
j++;
p = p -> next;
}
if( p == NULL)
return false;
else
{
s = (LinkList *) malloc (sizeof(LinkList));
s -> data = e;
s -> next = p -> next;
p -> next = s;
return true;
}
}
void delrepeat(LinkList *&L)
{
LinkList *p = L -> next, *q, *t;
while(p != NULL)
{
q = p;
while( q -> next != NULL)
{
if(q -> next -> data == p -> data)
{
t = q -> next;
q -> next = t -> next;
free(t);
}
else
q = q -> next;
}
p = p -> next;
}
}
/*
* 编写一算法,将单链表(带表头节点)中值重复的节点删除,使
* 所得到结果中各节点值各不相同。
* 实验数据121212121
*/
# include
# include
# include"LinkList.h"
using namespace std;
int main(void)
{
int a[100];
int i = 0;
cout << "The Number: ";
while((a[i] = getchar()) != '\n')
{
a[i] -= 48;
i++;
}
LinkList * List;
CreateListF(List, a, i);
DisplayList(List);
delrepeat(List);
DisplayList(List);
return 0;
}