数据结构各章实验源代码; 数据结构实验源代码

源代码在线查看: 习题07-有序链表删除重复元素.c

软件大小: 46 K
上传用户: foresnake
关键词: 数据结构 实验 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				#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);
				}
				
							

相关资源