?? 刪除重復.txt
字號:
#include<stdio.h>
#include<malloc.h>
typedef int elemtype;
typedef struct node
{ elemtype data;
struct node *next;
}linklist;
#define NULL 0
// 以下是主程序
#include"linklist.h"
// creatlist函數用來創建單鏈表
linklist *creatlist(int n)
{
int x,k;
linklist *head, *r, *p;
p=(linklist *)malloc(sizeof(linklist));
head=p;
p->next=NULL;
r=p;
for(k=1;k<=n;k++)
{ printf("input value:\n");
scanf("%d",&x);
p=(linklist *)malloc(sizeof(linklist));
p->data=x;
p->next=NULL;
r->next=p;
r=r->next;
}
return(head);
}
// deletesamenode函數用來刪除鏈表中重復的結點
linklist *deletesamenode(linklist *h)
{ linklist *p,*q,*s;
p=h->next;
s=p;
while(p!=NULL)
{ q=p->next;
while(q!=NULL)
{ if(q->data!=p->data)
{s=q; // s為q的直接前趨指針,即s緊跟著q向右移動。
q=q->next;
}
else
{ s->next=q->next; //此時q所指向的結點為待刪除結點
free(q);
q=s->next; //q指向后繼結點,繼續尋找與p所指結點值相同的結點。
}
} //內while循環結束
p=p->next;
} //外層while循環結束
return(h);
}
// output函數用來輸出單鏈表的內容
void output(linklist *h)
{ linklist *p;
p=h->next;
while(p)
{ printf("%d ",p->data);
p=p->next;
}
}
void main()
{ linklist *head;
int n;
printf("input the length of the list:\n");
scanf("%d",&n);
head=creatlist(n);
printf("output the list:\n");
output(head);
printf("刪除鏈表中結點的重復值!\n");
head=deletesamenode(head);
output(head); // 輸出經過處理后的單鏈表,此時單鏈表中的值應該唯一。
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -