1带头结点的的单链表
typedef int ElemType;
struct Node
{
ElemType data;
struct Node * pNext;
};
//带有头结点的单链表的基本操作(12个)
//1构造一个空链表
struct Node * init_list()
{
struct Node * pHead;
pHead = (struct Node *)malloc(sizeof(struct Node));
if (!pHead)
exit(-1);
pHead->pNext = NULL;
return pHead;
}
//2链表已存在,销毁链表(从前往后)
void destroy_list(struct Node * pHead)
{
struct Node * q;
while (pHead)
{
q = pHead->pNext;
free(pHead);
pHead = q;
}
}
//3将链表置为空
void clear_list(struct Node * pHead)
{
struct Node * q;
q = pHead->pNext;
pHead->pNext = NULL;
destroy_list(q);
}
//4判断是否为空
bool is_empty(struct Node * pHead)
{
if (pHead->pNext == NULL)
return true;
else
return false;
}
//5返回链表中元素个数
int length_list(struct Node * pHead)
{
if (is_empty(pHead))
return 0;
else
{
int cnt = 0;
struct Node * p;
p = pHead->pNext;
while (NULL != p)
{
cnt++;
p = p->pNext;
}
return cnt;
}
}
//6取得第pos个位置上元素的值
bool get_list(struct Node * pHead, int pos, ElemType * value)
{
int i = 0;
struct Node * p = pHead->pNext;
while (p && i < pos - 1)
{
i++;
p = p->pNext;
}
if (!p || i > pos - 1)
return false;
*value = p->data;
return true;
}
//7给定一个数,得到线性表中与此数相等的最小的位序
bool compare(ElemType e1, ElemType e2)
{
if (e1 == e2)
return true;
else
return false;
}
int locate_list(struct Node * pHead, ElemType val)
{
int i = 0;
struct Node * p = pHead->pNext;
while (p)
{
i++;
if(compare(p->data, val))
return i;
p = p->pNext;
}
return 0;
}
//8给定一个数,判断是否在链表里,如果在,从第二个元素开始得到此数的前一个数
bool prior_list(struct Node * pHead, ElemType cur_e, ElemType * pre_e)
{
struct Node * q;
struct Node * p = pHead->pNext;
while (p && p->pNext)
{
q = p->pNext;
if (q->data == cur_e)
{
*pre_e = p->data;
return true;
}
p = q;
}
return false;
}
//9返回cur的后一个
bool next_list(struct Node * pHead, ElemType cur_e, ElemType * next_e)
{
struct Node * p = pHead->pNext;
while (p && p->pNext)
{
if (p->data == cur_e)
{
*next_e = p->pNext->data;
return true;
}
p = p->pNext;
}
return false;
}
//10插入数据
bool insert_list(struct Node * pHead, int pos, int value)
{
int i = 0;
struct Node * p = pHead;
while (NULL != p && i < pos - 1)
{
p = p->pNext;
i++;
}
if (i > pos - 1 || NULL == p)
return false;
struct Node * pNew = (struct Node *) malloc(sizeof (struct Node));
if (NULL == pNew)
{
exit(-1);
}
struct Node * q = p->pNext;
pNew->data = value;
p->pNext = pNew;
pNew->pNext = q;
return true;
}
//11删除数据
bool delete_list(struct Node * pHead, int pos, int * val)
{
int i = 0;
struct Node * p = pHead;
while (NULL != p->pNext && i < pos - 1)
{
p = p->pNext;
i++;
}
if (i > pos - 1 || NULL == p->pNext)
return false;
struct Node * q = p->pNext;
*val = q->data;
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
return true;
}
//12遍历链表
void traverse_list(struct Node * pHead)
{
struct Node * p;
p = pHead->pNext;
while (NULL != p)
{
printf("%d ", p->data);
p = p->pNext;
}
printf("\n");
}
//正位序输入n个元素的值,建立带表头结点的单链线性表
struct Node * create_list_zheng(len)
{
ElemType value;
struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));
if (pHead == NULL)
{
printf("分配失败,程序终止!");
exit(-1);
}
struct Node * pTail = pHead;
pTail->pNext = NULL;
for (int i = 0; i < len; i++)
{
printf("第 %d 个节点的值为:", i+1);
fflush(stdout);
scanf("%d", &value);
struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));
if (pNew == NULL)
{
printf("分配失败,程序终止!");
exit(-1);
}
pNew->data = value;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
//逆位序输入n个元素的值,建立带表头结点的单链线性表
struct Node * create_list_ni(len)
{
ElemType value;
struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));
if (pHead == NULL)
{
printf("分配失败,程序终止!");
exit(-1);
}
pHead->pNext = NULL;
for (int i = len; i > 0; i--)
{
printf("第 %d 个节点的值为:", i);
fflush(stdout);
scanf("%d", &value);
struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));
if (pNew == NULL)
{
printf("分配失败,程序终止!");
exit(-1);
}
pNew->data = value;
pNew->pNext = pHead->pNext;
pHead->pNext = pNew;
}
return pHead;
}
void mergeList(struct Node * La, struct Node * Lb, struct Node * Lc)
{
struct Node * pa = La->pNext;
struct Node * pb = Lb->pNext;
struct Node * pc;
pc = Lc;
while (pa && pb)
{
if (pa->data <= pb->data)
{
pc->pNext = pa;
pc = pa; //移动pc
pa = pa->pNext;
}
else
{
pc->pNext = pb;
pc = pb;
pb = pb->pNext;
}
}
pc->pNext = pa ? pa : pb;
free(Lb);
Lb = NULL;
}
测试
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
struct Node * pHead;
pHead = init_list();
for (int i = 0; i < 6; i++)
{
insert_list(pHead, i+1, 2 * i);
}
int len = length_list(pHead);
printf("len = %d\n", len);
ElemType pre_e;
prior_list(pHead, 4, &pre_e);
printf("pre_e = %d\n", pre_e);
ElemType next_e;
next_list(pHead, 4, &next_e);
printf("next_e = %d\n", next_e);
int pos = locate_list(pHead, 6);
printf("pos = %d\n", pos);
ElemType val;
get_list(pHead, 5, &val);
printf("val = %d\n", val);
traverse_list(pHead);
ElemType del;
delete_list(pHead, 2, &del);
printf("del = %d\n", del);
traverse_list(pHead);
struct Node * La;
La = create_list_zheng(5);
traverse_list(La);
struct Node * Lb;
Lb = create_list_zheng(7);
traverse_list(Lb);
struct Node * Lc;
Lc = init_list();
mergeList(La, Lb, Lc);
traverse_list(Lc);
return 0;
}