7.带表头节点的单向链表
(1)创建带表头节点的单向链表
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
int main()
{
int a[N] = {10,20,30,40,50};
ElemSN *head;
//创建带表头单向链表
head = Createlink(a,5);
//打印原链表
printf("原链表为:");
Printlink(head);
printf("\n");
return 0;
}
(2)创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
(2)删除关键字
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
void Delkeynode(ElemSN * h,int key);//删除关键字
int main()
{
int a[N] = {10,20,30,40,50};
ElemSN *head;
//创建带表头单向链表
head = Createlink(a,5);
//打印原链表
printf("原链表为:");
Printlink(head);
printf("\n");
//删掉关键字
printf("输入关键字:");
int key;
scanf("%d",&key);
printf("删除关键字结果为:");
Delkeynode(head,key);
Printlink(head);
printf("\n");
return 0;
}
//创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
//删除关键字
void Delkeynode(ElemSN * h,int key)
{
ElemSN *p,*Delp;
for(p = h;p->next && p->next->data != key;p=p->next);
if(!p->next)
{
printf("没找着");
}
else
{
Delp = p->next;
p->next = Delp->next;
free(Delp);
}
}
(3)删掉无序重复关键字
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
void Delkeynode(ElemSN * h,int key);//删除无序且重复关键字
int main()
{
int a[N] = {1,1,2,1,3,5,1,1};
ElemSN *head;
//创建带表头单向链表
head = Createlink(a,8);
//打印原链表
printf("原链表为:");
Printlink(head);
printf("\n");
//删掉无序重复关键字
printf("输入关键字:");
int key;
scanf("%d",&key);
Delkeynode(head,key);
Printlink(head);
printf("\n");
return 0;
}
//创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
//删除无序重复关键字
void Delkeynode(ElemSN * h,int key)
{
ElemSN *p,*Delp;
for(p =h;p->next;)
{
if(p->next->data == key)
{
Delp = p->next;
p->next = Delp->next;
free(Delp);
}
else
{
p = p->next;
}
}
}
(4)
就地逆置
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
ElemSN * Renode(ElemSN *h);//就地逆置
int main()
{
int a[N] = {10,20,30,40,50};
ElemSN *head;
//创建带表头单向链表
head = Createlink(a,5);
//打印原链表
printf("原链表为:");
Printlink(head);
printf("\n");
//就地逆置
printf("逆置结果为:");
Renode(head);
Printlink(head);
printf("\n");
return 0;
}
//创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
//就地逆置
ElemSN * Renode(ElemSN *h)
{
ElemSN *p,*q,*h1;
p = h;
q = h->next;
h1 = NULL;
while(p->next)
{
p->next = q->next;
q->next = h1;
h1 = q;
q = p->next;
}
h->next = h1;
return h;
}
//就地逆置2
ElemSN * Renode(ElemSN *h)
{
ElemSN *p,*h1;
h1 = NULL;
while(h->next)
{
p = h->next;
h->next = p->next;
p->next = h1;
h1 = p;
}
h->next = h1;
return h;
}
(5)输出带表头节点的升序链表
#include <stdio.h>
#include <stdlib.h>
#define N 6
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
ElemSN * LTBnode(ElemSN *h);//带表头节点的升序链表
int main()
{
int a[N] = {3,2,5,8,4,7};
ElemSN *head;
//创建带表头单向链表
head = Createlink(a,6);
//打印原链表
printf("原链表为:");
Printlink(head);
printf("\n");
//单向链表升序
printf("升序结果为:");
head = LTBnode(head);
Printlink(head);
printf("\n");
return 0;
}
//创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
//带表头节点的升序链表
ElemSN * LTBnode(ElemSN *h)
{
ElemSN *p,*q,*h1,*pmax;
q = h->next;
p = h;
h1 = NULL;
while(q->next)
{
for(;q->next;q = q->next)
{
if(p->next->data < q->next->data)
{
p = q;
}
}
pmax = p->next;
p->next = pmax->next;
pmax->next = h1;
h1 = pmax;
p = h,q = h->next;
}
h->next->next = h1;
return h;
}
(6)合并
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
ElemSN * Mergelink(ElemSN *h1,ElemSN *h2);//合并
int main()
{
int a[N] = {2,2,2,5,6};
int b[10] = {1,3,3,5,6,8,8,8,9,10};
ElemSN *head,*head1,*head2;
//创建带表头单向链表
head1 = Createlink(a,5);
head2 = Createlink(b,10);
//打印原链表
printf("原链表1为:");
Printlink(head1);
printf("\n");
printf("原链表2为:");
Printlink(head2);
printf("\n");
//合并
printf("合并结果为:");
head = Mergelink(head1,head2);
Printlink(head);
printf("\n");
return 0;
}
//创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
//合并
ElemSN * Mergelink(ElemSN *h1,ElemSN *h2)
{
ElemSN *head3,*p,*q;
head3 = q =(ElemSN *)malloc(sizeof(ElemSN));
head3->next = NULL;
while(h1->next && h2->next)
{
if(h1->next->data < h2->next->data)
{
p = h1->next;
h1->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}
else
{
p = h2->next;
h2->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}
}
if(h1)
{
q->next = h1->next;
/* while(h1->next)
{
p = h1->next;
h1->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}*/
}
if(h2)
{
q->next = h2->next;
/* while(h2->next)
{
p = h2->next;
h2->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}*/
}
return head3;
}
(7)删除合并后带重复结点的值
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node * next;
}ElemSN;
ElemSN * Createlink(int a[],int n);//创建带表头单向链表
void Printlink(ElemSN *h);//打印带表头单向链表
ElemSN * Mergelink(ElemSN *h1,ElemSN *h2);//合并
ElemSN * Delsamenode(ElemSN *h);//删除重复的结点
int main()
{
int a[N] = {2,2,2,5,6};
int b[10] = {1,3,3,5,6,8,8,8,9,10};
ElemSN *head,*head1,*head2;
//创建带表头单向链表
head1 = Createlink(a,5);
head2 = Createlink(b,10);
//打印原链表
printf("原链表1为:");
Printlink(head1);
printf("\n");
printf("原链表2为:");
Printlink(head2);
printf("\n");
//合并
printf("合并结果为:");
head = Mergelink(head1,head2);
Printlink(head);
printf("\n");
//将合并后重复的删掉
printf("处理后的结果为:");
head = Delsamenode(head);
Printlink(head);
printf("\n");
return 0;
}
//创建带表头单向链表
ElemSN * Createlink(int a[],int n)
{
ElemSN *h,*t,*p;
h = (ElemSN *)malloc(sizeof(ElemSN));
t = h;
for(int i = 0;i < n;i++)
{
p = (ElemSN *)malloc(sizeof(ElemSN));
p->data = a[i];
p->next = NULL;
t->next = p;
t = p;
}
return h;
}
//打印链表
void Printlink(ElemSN *h)
{
ElemSN *p;
for(p = h;p->next;p = p->next)
{
printf("%d ",p->next->data);
}
}
//合并
ElemSN * Mergelink(ElemSN *h1,ElemSN *h2)
{
ElemSN *head3,*p,*q;
head3 = q =(ElemSN *)malloc(sizeof(ElemSN));
head3->next = NULL;
while(h1->next && h2->next)
{
if(h1->next->data < h2->next->data)
{
p = h1->next;
h1->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}
else
{
p = h2->next;
h2->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}
}
if(h1)
{
q->next = h1->next;
/* while(h1->next)
{
p = h1->next;
h1->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}*/
}
if(h2)
{
q->next = h2->next;
/* while(h2->next)
{
p = h2->next;
h2->next = p->next;
p->next = NULL;
q->next = p;
q = p;
}*/
}
return head3;
}
ElemSN * Delsamenode(ElemSN *h)
{
ElemSN *p,*q;
p = h;
q = h->next;
while(q->next)
{
if(p->next->data != q->next->data)
{
p = p->next;
q = q->next;
}
else
{
p->next = q->next;
free(q);
q = p->next;
}
}
return h;
}