本文主要回顾数据结构中的链表的操作,包括链表的构造和基本操作函数首先是在头文件中申明所要用的结构体以及链表基本操作函数
#ifndef _list_H
#define _list_H
//声明一个结构
struct Node;
typedef struct Node *pnode;
typedef pnode List;
typedef pnode Position;
List makeempty(List L);
int isempty(List L);
int islast(Position p,List L);
Position Find(int X,List L);
void Delete(int X,List L);
Position findprevious(int X,List L);
void Insert(int X,List L,Position P);
#endif // _list_H
接下来包括Node结构体的定义以及链表基本操作的定义
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
struct Node
{
int element;
Position next;
};
int isempty(List L)
{
if(L->next==NULL)
return 1;
else
return 0;
}
//Find函数,用来查找元素所在的节点的地址
Position Find(int X,List L)
{
Position P=L->next;
while(P!=NULL&&P->element!=X)
P=P->next;
return P;
}
//Findpre函数,用来查找元素所在节点的前一个节点的地址
Position Findpre(int X,List L)
{
Position P=L;
while(P->next!=NULL&&P->next->element!=X)
P=P->next;
return P;
}
//delete函数,用于节点的删除
void Delete(int X,List L)
{
Position P=L;
Position tem;
P=Findpre(X,L);
if(P->next==NULL)
{
printf("error not found the elem");
exit(1);
}
tem=P->next;
P->next=tem->next;
free(tem);
}
//遍历表
void traverse(List L)
{
Position P=L->next;
if(P==NULL)
printf("this is a empty list");
else
while(P!=NULL)
{
printf("%d\n",P->element);
P=P->next;
}
}
然后定义两个链表root和orig
int main()
{
List orig;
Position node1=malloc(sizeof(struct Node));
Position node2=malloc(sizeof(struct Node));
orig=malloc(sizeof(struct Node));
node2->element=2;
node2->next=NULL;
node1->element=1;
node1->next=node2;
orig->element=0;
orig->next=node1;
Insert(3,orig,node2);
Position node3=node2->next;
Insert(4,orig,node3);
Position node4=node3->next;
Insert(5,orig,node4);
List root=malloc(sizeof(struct Node));
root->next=NULL;
Position node1_root=malloc(sizeof(struct Node));
Position node2_root=malloc(sizeof(struct Node));
node2_root->element=4;
node2_root->next=NULL;
node1_root->element=2;
node1_root->next=node2_root;
root->next=node1_root;
}
练习1:给定一个链表L1和链表L2,它们包含以升序排列的整数。操作PrintLots(L1,L2)将打印L中那些P所指定的位置上的元素。例如p=1,3,4,7,那么将L中的第1,第3,第4,第7个元素打印出来。
PrintLots函数如下
void PrintLots(List L1,List L2)
{
int tem;
int index=0;
Position P2=L2->next;
Position P1=L1->next;
if(P2==NULL)
printf(" this is a empty list");
else
while(P2!=NULL)
{
tem=P2->element;
while(P1!=NULL)
{
index=index+1;
if(index==tem)
{
printf("%d\n",P1->element);
P1=P1->next;
break;
}
P1=P1->next;
}
if(P1==NULL)
{
printf("index is full");
break;
}
P2=P2->next;
}
}
主函数程序如下:
printf("root\n");
traverse(root);
printf("orig\n");
traverse(orig);
printf("result\n");
PrintLots(orig,root);
输出结果如下
练习2:交换链表中相邻的两个元素之间的位置
void exchange(List L,int X)
{
int index=0;
Position P=L;
Position P_ex1,P_ex2,P_ex3;
while(P->next!=NULL)
{
index=index+1;
if(index==X)
{
P_ex1=P->next;
P_ex2=P->next->next;
P_ex3=P->next->next->next;
P->next=P_ex2;
P_ex2->next=P_ex1;
P_ex1->next=P_ex3;
printf("success exchange\n");
break;
}
P=P->next;
}
}
该函数交换位置X和位置X+1上的元素。如果交换成功,返回success。
然后调用exchange
exchange(orig,2);
traverse(orig);
结果为
练习三,将两个排好序的链表进行求交运算,函数为intersection
void intersection(List L1,List L2)
{
if(L1->next==NULL||L2->next==NULL)
{
printf("intersection of L1 and L2 is empty");
exit(1);
}
Position P1;
Position P2;
if(L1->next->element<L2->next->element)
{
P1=L2->next;
P2=L1->next;
}
else
{
P1=L1->next;
P2=L2->next;
}
int tem;
int suc=0;
printf("intersection result is\n");
while(P1!=NULL)
{
tem=P1->element;
while(P2!=NULL)
{
if(P2->element>=tem)
{
if(P2->element==tem)
{
printf("%d\n",tem);
P2=P2->next;
suc=1;
break;
}
if(P2->element>tem)
{
break;
}
}
P2=P2->next;
}
P1=P1->next;
}
if(suc==0)
printf("intersection is empty\n");
}
练习四.将两个已经排好序的链表求并运算
void Union(List L1,List L2)
{
if(L1->next==NULL&&L2->next!=NULL)
{
traverse(L2);
exit(1);
}
if(L1->next!=NULL&&L2->next==NULL)
{
traverse(L1);
exit(1);
}
if(L1->next==NULL&&L2->next==NULL)
{
printf("the result is empty");
exit(1);
}
List L3;
Position P1;
Position P2;
int tem;
if(L1->next->element<L2->next->element)
{
P1=L2->next;
P2=L1->next;
L3=L1;
}
else
{
P1=L1->next;
P2=L2->next;
L3=L2;
}
printf("union result\n");
while(P1!=NULL)
{
tem=P1->element;
while(P2!=NULL)
{
if(tem<=P2->element)
{
if(tem==P2->element)
break;
else
{
printf("%d\n",tem);
break;
}
}
P2=P2->next;
}
if(P2==NULL)
printf("%d\n",tem);
P1=P1->next;
}
traverse(L3);
}