求:单链表删除 和 顺序表删除的算法。删除表中值相同的多余结点 C++的

来源:百度知道 编辑:UC知道 时间:2024/09/27 17:28:03

#include <stdio.h>
#include <malloc.h>
typedef struct LinkList
{
int elem;
struct LinkList *next;
}LList;

void CreatList( LList *L)
{
int ch;
LList *s;
printf("please enter the date:");
scanf("%d",&ch);

while(ch!=0)/*当输入为0时结束输入 */
{
s=malloc(sizeof( LList));
s -> elem = ch;
s ->next =L ->next;
L -> next = s;
printf("please enter the date:");
scanf("%d",&ch);
}
}

void reverse( LList *L)
{
LList *p,*r,*q;
p=L;
while(p)
{
q=p->next;
r=p;
while(q)
{
if(q->elem==p->elem)
{
r->next=q->next;
free(q);
q=r->next;
}
else
{
q=q->next;
r=r->next;
}
}
p=p->next;
}