分别以头插法和尾插法建立两个数据域定义为整型的升序单链表,再将这两个有序链表合并成一个新的无重复元素的有序链表,最后可以根据输入的数据,先找到相应的结点,后删除之。
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
node * qbuild(node *first) //头插法
{
int i,n,a[100];
node *s;
printf("输入n:");
scanf("%d",&n);
first=(node*)malloc(sizeof(node));
first->next=NULL;
for(i=0;i<n;i++)
{
s=(node*)malloc(sizeof(node));
printf("输入a[%d]:",i);
scanf("%d",&a[i]);
s->data=a[i];
s->next=first->next;
first->next=s;
}
return first;
}
node * hbuild(node *first) //尾插发
{
int i,n,a[100];
struct node *s,*r;
printf("输入n:");
scanf("%d",&n);
first=(node*)malloc(sizeof(node));
r=first;
for(i=0;i<n;i++)
{
s=(node*)malloc(sizeof(node));
printf("输入a[%d]:",i);