链式存储的每个节点包括两个域,其中存储数据元素信息的域称为数据域;存储直接后继存储位置称为指针域。指针域中存储的信息交指针或链。
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR -1
#define MAX_SIZE 20
typedef int ElemType;
节点的结构体
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode;
链表初始化
ElemType Init_LNode(LNode *L)
{
int a = -1;
L->data = 0;
L->next=NULL;
if(L->next == NULL)
{
a = OK;
}
return a;
}
在第二i个位置插入e
ElemType Insert_LNode(LNode *L, int i, ElemType e)
{
int count = 0;
LNode *p;
LNode *q;
int a = -1;
p = L;
while(p->next != NULL && count < i - 1)
{
count++;
p = p->next;
}
if(i > count + 1)//插入的位置i,比链表的长度加一还要大, 溢出
{
printf("overflow\n");
}
else//插入一个节点
{
q= (LNode*) malloc( sizeof(LNode));
q->data = e;
q->next = p->next;
p->next = q;
a= OK;
}
return a;
}
根据元素位置i 查询元素
ElemType Query(LNode *L, int i, ElemType count)//count是元素个数
{
LNode *p = L;
if(i <= 0 || i > count)//溢出
{
printf("Irregular position or this element does not exist!\n");
}
else
{
int j;//向后走i次,找到第i个元素
for(j = 0; j < i; j++)
{
p = p->next;
}
printf("The NO.%d number is :%d\n", i, p->data);
}
return OK;
}
删除链表第i个元素
ElemType Delete_LNode(LNode *L, int i, int count)//count是元素个数
{
LNode *p, *q;
p = L;
if(i <= 0 || i > count)
{
printf("This element does not exist!\n");
return ERROR;
}
else
{
int j;
for(j = 0; j < i; j++)
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
}
return OK;
}
遍历链表 求元素个数
int Travel(LNode *p)
{
int count = 0;
while(p->next != NULL)
{
count++;
p = p->next;
}
return count;
}
输出链表元素
void OutPut_LNode(LNode *L, ElemType count)
{
LNode *p;
int i = count - 1;
p = L->next;
while(p != NULL)
{
printf("The No.%d number is :%d\n", count-i ,p->data);
i--;
p = p->next;
}
}
连接两个有序的链表 连接后仍然有序
ElemType ListCombine(LNode *L1, LNode *L2, LNode *L3)
{
int i;
int a = 0;//元素个数
int pos = 0;//插入的位置
int value = 0;//插入的值
int count = 0;//L3的长度
LNode *p;
LNode *q;
//L1插入元素
printf("L1:");
scanf("%d", &a);
for(i = 0; i < a; i++)
{
printf("position:");
scanf("%d", &pos);
printf("value:");
scanf("%d", &value);
Insert_LNode(L1, pos, value);
}
//L2插入元素
printf("L2:");
scanf("%d", &a);
for(i = 0; i < a; i++)
{
printf("position:");
scanf("%d", &pos);
printf("value:");
scanf("%d", &value);
Insert_LNode(L2, pos, value);
}
//打印L1和L2
printf("----L1----\n");
count = Travel(L1);
OutPut_LNode(L1, count);
printf("----L2----\n");
count = Travel(L2);
OutPut_LNode(L2, count);
//连接L1和L2
p = L1->next;
q = L2->next;
i = 1;
while(p != NULL && q != NULL)
{
if(p->data < q->data)
{
Insert_LNode(L3, i, p->data);
p = p->next;
i++;
}
else if(p->data > q->data)
{
Insert_LNode(L3, i, q->data);
q = q->next;
i++;
}
else if(p->data == q->data)
{
Insert_LNode(L3, i, p->data);
p = p->next;
i++;
Insert_LNode(L3, i, q->data);
q = q->next;
i++;
}
}
把剩下的元素直接连到L3后面
while(1)
{
if(p != NULL && q == NULL)
{
Insert_LNode(L3, i, p->data);
p = p->next;
i++;
}
else if(q != NULL && p == NULL)
{
Insert_LNode(L3, i, q->data);
q = q->next;
i++;
}
else
{
break;
}
}
printf("----L3----\n");
count = Travel(L3);
printf("%d----\n",count);
OutPut_LNode(L3, count);
return 0;
}
main方法
int main()
{
int i = 0;
int a;//插入的数据
int b;//操作编号
int j;//插入的位置
int count = 0;
int res = -1;
LNode L;
printf("1.Initialize\n\n2.Insert Data\n\n3.Travel\n\n4.OutPut LNode\n\n5.Delete\n\n6.Query\n\n7.combine\n\n");
while(1)
{
printf("Please enter the operation code :");
scanf("%d", &b);
printf("\n");
switch(b)
{
case 1:
//初始化
res = Init_LNode(&L);
if(res == -1)
{
printf("error\n\n");
}
else
{
printf("success\n");
}
i++;
break;
case 2:
if(i == 0)
{
printf("Please initialize a LinkNode first!\n");
}
else
{
printf("Please enter number:");
scanf("%d", &a);
printf("\n");
printf("Please enter place:");
scanf("%d", &j);
printf("\n");
res = -1;
res = Insert_LNode(&L, j, a);
printf("\n");
if(res == -1)
{
printf("Error\n");
}
else
{
printf("Success\n");
}
printf("--------------\n");
}
break;
case 3:
if(i == 0)
{
printf("Please initialize a LinkNode first!\n");
}
else
{
//求链表元素个数
count = Travel(&L);
printf("The count is %d\n",count);
}
break;
case 4:
if(i == 0)
{
printf("Please initialize a LinkNode first!\n");
}
else
{
//输出链表
count = Travel(&L);
OutPut_LNode(&L, count);
}
break;
case 5:
if(i == 0)
{
printf("Please initialize a LinkNode first!\n");
}
else
{
count = Travel(&L);
printf("***********Delete*********\n");
printf("Please enter the place:");
scanf("%d", &a);
Delete_LNode(&L, a, count);
}
break;
case 6:
if(i == 0)
{
printf("Please initialize a LinkNode first!\n");
}
else
{
printf("***********Query************\n");
count = Travel(&L);
printf("Please enter the place:\n");
scanf("%d", &a);
Query(&L, a, count);
}
break;
case 7:
if(i == -1)
{
printf("Please initialize a LinkNode first!\n");
}
else
{
LNode L1;
LNode L2;
LNode L3;
//初始化
Init_LNode(&L1);
Init_LNode(&L2);
Init_LNode(&L3);
ListCombine(&L1, &L2, &L3);
}
break;
}
if(b > 7)
{
printf("Operation coding error!\nPlease enter again!\n");
}
}
return 0;
}
以上就是链表的一些列操作