7-6 数据结构实验之链表七:单链表中重复元素的删除 (20 分)
生活随笔
收集整理的這篇文章主要介紹了
7-6 数据结构实验之链表七:单链表中重复元素的删除 (20 分)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
按照數(shù)據(jù)輸入的相反順序(逆位序)建立一個單鏈表,并將單鏈表中重復的元素刪除(值相同的元素只保留最后輸入的一個)。
輸入格式:
第一行輸入元素個數(shù) n (1 <= n <= 15);
第二行輸入 n 個整數(shù),保證在 int 范圍內。
輸出格式:
第一行輸出初始鏈表元素個數(shù);
第二行輸出按照逆位序所建立的初始鏈表;
第三行輸出刪除重復元素后的單鏈表元素個數(shù);
第四行輸出刪除重復元素后的單鏈表。
樣例">輸入樣例:
10 21 30 14 55 32 63 11 30 55 30輸出樣例:
10 30 55 30 11 63 32 55 14 30 21 7 30 55 11 63 32 14 21?
#include<stdio.h> #include<stdlib.h> struct node {int data;struct node *next; }; struct node *creat(int n) {int i;struct node *head,*p;head=(struct node*)malloc(sizeof(struct node));head->next=NULL;for(i=0;i<n;i++){p=(struct node*)malloc(sizeof(struct node));scanf("%d",&p->data);p->next=head->next;head->next=p;}return (head); } void pri(struct node *p) {while(p){if(p->next==NULL)printf("%d\n",p->data);elseprintf("%d ",p->data);p=p->next;} } int main() {int n;struct node *head,*p,*q,*k;scanf("%d",&n);head=creat(n);printf("%d\n",n);pri(head->next);p=head->next;q=head;while(p){int flag=1;k=head->next;while(k!=p){if(k->data==p->data){flag=0;n--;q->next=p->next;p=p->next;break;}k=k->next;}if(flag==1){p=p->next;q=q->next;}}printf("%d\n",n);pri(head->next);return 0; }總結
以上是生活随笔為你收集整理的7-6 数据结构实验之链表七:单链表中重复元素的删除 (20 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目记录 / 基于AT89C51的环境检
- 下一篇: SQL Server 数据库之角色、管理