数据结构各章实验源代码; 数据结构实验源代码
源代码在线查看: 习题07-有序链表删除重复元素.c
#include "datastru.h"
#include
#include
void delete(LINKLIST *a){
/*在有序链表中删除重复元素,保留一个*/
LINKLIST *la;
la = a->next;
while(la != NULL && la->next != NULL)
if (la->data == la->next->data)
la->next = la->next->next;
else la = la->next;
}
int count_head(LINKLIST *head){
/*带头结点的单链表:输出单链表元素值并计数*/
int i = 0;
LINKLIST *p;
p = head->next;
printf("输出单链表元素值 : ");
while(p != NULL)
{i++;
printf(" %c",p->data);
p = p->next;}
printf("\n\n");
return i;
}
LINKLIST *creatlink_order_head(LINKLIST *head)
/*建立带头结点的有序单链表*/
{ LINKLIST *t, *p, *q;
char ch;
t = (LINKLIST *)malloc(sizeof(LINKLIST));
head = t; t->next = NULL;
printf("单链表元素值为单个字符, 连续输入,$为结束字符 : ");
while ((ch = getchar()) != '$')
{t = (LINKLIST *)malloc(sizeof(LINKLIST));
t->data = ch;
q = head; p = head->next;
while( p != NULL && p->data q = p; p = p->next;}
q->next = t; t->next = p;
}
return(head);
}
main()
{
LINKLIST *head = NULL;
int num;
printf("\n 建立单链表\n\n");
head = creatlink_order_head(head);
fflush(stdin);
num = count_head(head);
printf("\n 删除重复元素后\n\n");
delete(head);
num = count_head(head);
}