/ *
顺序表(链表实现)
2018.12.07
* /
#include <stdio.h>
#include <stdlib.h> //需要使用malloc,所以加入stdlib头文件
#define MAXSIZE 10000
typedef struct LNode
{
int data; //结点数据
struct LNode * next; //结构体指针写法要记牢
} LNode;
//头插法建立单链表
void createListF (LNode ** L,int * arr,int arrLength)
{
(* L)=(LNode *)malloc(sizeof(LNode)); //注意此处的写法,天晴的为C ++代码,是传引用
(* L) - > next = NULL; // C语言只能传地址所以对L的引用使用* L来实现
LNode * newNode;
LNode *head;
head =(* L); //这个操作既直观又方便
for(int i = 0; i <arrLength; i ++)
{
newNode =(LNode *)malloc(sizeof(LNode)); //循环申请新结点的空间
newNode-> data = arr [i]; //注意申请空间的写法
newNode-> next = head-> next; //这段是头插法的核心代码
head-> next = newNode; //做的时候要多画图
}
}
//尾插法建立单链表
void createListR (LNode ** L,int * arr,int arrLength)//涉及到对链表的增加,所以使用**
{
(* L)=(LNode *)malloc(sizeof(LNode)); //注意此处的写法,天晴的为C ++代码,是传引用
(* L) - > next = NULL; // C语言只能传地址所以对L的引用使用* L来实现
LNode * rear;
LNode * newNode;
rear= * L;
for(int i = 0; i <arrLength; i ++)
{
newNode =(LNode *)malloc(sizeof(LNode));
newNode-> data = arr [i];
rear-> next = newNode; //第一次执行此操作产生了“头结点”
rear = rear-> next;
}
rear-> next = NULL;
}
//单链表插入元素(头插法)
void insertElementF(LNode ** L,int element)
{
LNode * head;
head = * L;
LNode * newNode =(LNode *)malloc(sizeof(LNode));
newNode-> data = element;
newNode-> next = head-> next; //头插法操作
head-> next = newNode;
}
//单链表插入元素(尾插法)
void insertElementR(LNode ** L,int element)
{
LNode * head;
head = * L;
LNode * newNode =(LNode *)malloc(sizeof(LNode));
newNode-> data = element;
while(head-> next!= NULL)head = head-> next;
newNode-> next = head-> next;
head-> next = newNode;
}
//单链表删除元素
void deleteElement(LNode **L, int element)
{
LNode *temp;
temp = *L;//temp初始为头指针
while (temp->next != NULL)
{
if (temp->next->data == element)//要删除temp的下一个结点
{
temp->next = temp->next->next;
break;
}
temp = temp->next;
}
if (temp->data == element)temp = NULL;//最后一个结点的元素也是要删除的元素
}
//返回单链表长度
int getLength(LNode *L)
{
int length=0;
L = L->next;
if (L == NULL)return 0;
while (L != NULL)
{
length++;
L = L->next;
}
return length;
}
//归并两个递增的单链表
void merge(LNode *L1, LNode *L2, LNode **L)
{
(*L) = (LNode*)malloc(sizeof(LNode));
(*L)->next = NULL;
LNode *rear;
rear = *L;
LNode *newNode;
L1 = L1->next; //首元素结点
L2 = L2->next; //首元素结点
while (L1 != NULL && L2 != NULL)
{
newNode = (LNode*)malloc(sizeof(LNode));
if (L1->data < L2->data)
{
newNode->data = L1->data;
L1 = L1->next;
}
else
{
newNode->data = L2->data;
L2 = L2->next;
}
rear->next = newNode;
rear = rear->next;
}
if (L1 == NULL)
{
rear->next = L2;
}
if (L2 == NULL)
{
rear->next = L1;
}
}
//打印单链表
void printList(LNode *L)
{
printf("建立的链表为:");
while (L != NULL)
{
L = L->next;
if (L->next != NULL)printf("%d -> ", L->data);
if (L->next == NULL) { printf("%d\n", L->data); return; }
}
}
int main()
{
{
int arr[MAXSIZE];
int arrLength;
printf("请输入线性表的长度:");
scanf("%d", &arrLength);
printf("请输入线性表的元素:");
for (int i = 0; i < arrLength; i++)
{
scanf("%d", &arr[i]);
}
LNode *L1;
createListR(&L1, arr, arrLength);
printf("采用尾插法");
printList(L1);
LNode *L2;
createListF(&L2, arr, arrLength);
printf("采用头插法");
printList(L2);
printf("头插法:请输入将插入链表的元素:");
int insertNumber;
scanf("%d", &insertNumber);
insertElementF(&L1, insertNumber);
insertElementF(&L2, insertNumber);
printList(L1);
printList(L2);
printf("采用尾插法建立的链表的长度:%d\n", getLength(L1));
printf("采用头插法建立的链表的长度:%d\n", getLength(L2));
printf("尾插法:请输入将插入链表的元素:");
//int insertNumber;
scanf("%d", &insertNumber);
insertElementR(&L1, insertNumber);
insertElementR(&L2, insertNumber);
printList(L1);
printList(L2);
printf("采用尾插法建立的链表的长度:%d\n", getLength(L1));
printf("采用头插法建立的链表的长度:%d\n", getLength(L2));
printf("请输入将删除链表的元素:");
int deleteNumber;
scanf("%d", &deleteNumber);
deleteElement(&L1, deleteNumber);
deleteElement(&L2, deleteNumber);
printList(L1);
printList(L2);
}
printf("请按照递增的序列输入线性表的元素\n");
int arr1[MAXSIZE];
int arr1Length;
printf("请输入线性表1的长度:");
scanf("%d", &arr1Length);
printf("请输入线性表1的元素:");
for (int i = 0; i < arr1Length; i++)
{
scanf("%d", &arr1[i]);
}
int arr2[MAXSIZE];
int arr2Length;
printf("请输入线性表2的长度:");
scanf("%d", &arr2Length);
printf("请输入线性表2的元素:");
for (int i = 0; i < arr2Length; i++)
{
scanf("%d", &arr2[i]);
}
LNode *L1;
createListR(&L1, arr1, arr1Length);
printf("L1:采用尾插法");
printList(L1);
LNode *L2;
createListR(&L2, arr2, arr2Length);
printf("L2:采用尾插法");
printList(L2);
LNode *L;
merge(L1, L2, &L);
printf("L:归并后");
printList(L);
return 0;
}