⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 删除重复.txt

📁 数值分析中不动点迭代的算法
💻 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 + -