赞
踩
- #include<stdio.h>
- #include<stdlib.h>
- struct Node{
- int Data;
- struct Node *Next;
- };
- void insert_tail(struct Node *L,int num){/*尾插法*/
- struct Node *p,*q;
- p=L;
- while(p->Next!=NULL){
- p=p->Next;
- }
- q=(struct Node *)malloc(sizeof(struct Node));
- q->Data=num;
- q->Next=p->Next;
- p->Next=q;
- }
- void show(struct Node *L){/*打印链表*/
- struct Node *p;
- p=L;
- while(p->Next!=NULL){
- p=p->Next;
- printf("%d\t",p->Data);
- }
- printf("\n");
- }
- void slim(struct Node *L){/*去除链表中重复元素*/
- struct Node *p,*q;
- p=L->Next;
- while(p!=NULL){
- while(p->Next!=NULL&&p->Data==p->Next->Data){
- q=p->Next;
- p->Next=q->Next;
- free(q);/*回收多余空间*/
- }
- p=p->Next;
- }
- }
- int main(){
- struct Node *L,*Neg;
- L=(struct Node *)malloc(sizeof(struct Node));
- L->Next=NULL;
- int i;
- for(i=1;i<=4;i++){/*输入构造含重复元素的链表*/
- insert_tail(L,i);
- insert_tail(L,i);
- insert_tail(L,i);
- }
- show(L);/*打印原有链表*/
- slim(L);
- show(L);/*打印去除重复元素后的链表*/
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。