前言
本篇文章是在上一篇文章link.的基础上对非循环单链表的其他操作(判断链表是否为空、求链表的长度、对链表排序以及对链表的插入和删除操作)
判断链表是否为空
代码实现:
bool is_empty(PNODE pHead)
{
PNODE p = pHead->pNext;
if(NULL == p)
return true;
else
return false;
}
求链表的长度
代码实现:
int length_list(PNODE pHead)
{
int len = 0;
PNODE p = pHead;
while(NULL != p)
{
++len;
p = p->pNext;
}
return len;
}
对链表进行排序
首先了解数组的排序
数组排序(选择排序)代码如下:
void sort_array(int *a, int len)
{
int i, j, t;
for(i = 0; i < len-1; ++i)
{
for(j = i+1; j < len; ++j)
{
if(a[i] > a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
对比得出链表的排序,代码如下:
void sort_list(PNODE pHead)
{
int i, j, t;
int len = length_list(pHead);
PNODE p, q;
for(i = 0, p = pHead->pNext; i < len-1; ++i, p = p->pNext)
{
for(j = i+1, q = p->pNext; j < len; ++j, q = q->pNext)
{
if(p->data > q->data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
}
}
}
对链表进行插入操作
插入操作伪算法
插入操作代码
//在pHead所指链表的第pos个结点前插入插入一个新的结点,该结点的值是val,并且pos的值是从1开始
bool insert_list(PNODE pHead, int pos, int val)
{
int i = 0;
PNODE p = pHead;
while(NULL != p && i < pos-1)
{
p = p->pNext;
++i;
}
if(NULL == p || i > pos-1)
return false;
PNODE pNew = (PNODE)malloc(sizeof(PNODE)); //3
if(NULL == pNew)
{
printf("内存分配失败\n");
exit(-1);
}
pNew->data = val;
PNODE q = p->pNext; //4
p->pNext = pNew; //5
pNew->pNext = q; //6
return true;
}
对链表进行删除操作
删除操作伪算法
删除操作代码
bool delete_list(PNODE pHead; int pos; int *pval)
{
int i = 0;
PNODE p = pHead;
while(NULL != p->pNext && i < pos-1) //此处为p->pNext,不同于插入操作,因为头结点后面必须要有有效结点才能进行删除操作
{
p = p->pNext;
++i;
}
if(NULL == p || i > pos-1)
return false;
PNODE q = p->pNext; //3
*pval = q->data; //*pval用来存放被删除结点的值
p->pNext = p->pNext->pNext; //4 删除了p后面的结点
free(q);
q = NULL;
return true;
}
整体代码实现
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node
{
int data; //数据域
struct Node *pNext; //指针域
}NODE, *PNODE; //NODE等价于struct Node PNODE等价于struct Node *
//函数声明
PNODE create_List(void);
void traverse_List(PNODE pHead);
bool is_empty(PNODE pHead);
int length_List(PNODE pHead);
void sort_List(PNODE pHead);
bool insert_List(PNODE pHead, int pos, int val);
bool delete_List(PNODE pHead, int pos, int *pval);
int main(void)
{
PNODE pHead = NULL; //等价于struct Node *pHead = NULL;
int val;
pHead = create_List(); //create_List()功能:创建一个非循环单链表,并将该链表的头结点的地址赋给pHead
traverse_List(pHead); //遍历
int len = length_List(pHead);
printf("链表长度是%d\n", len);
sort_List(pHead);
traverse_List(pHead);
if(insert_List(pHead, 3, 100))
printf("插入成功\n");
else
printf("插入失败\n");
traverse_List(pHead);
if(delete_List(pHead, 3, &val))
printf("删除成功, 删除的值为%d\n", val);
else
printf("删除失败\n");
traverse_List(pHead);
return 0;
}
//创建一个链表
PNODE create_List(void)
{
int len; //用来存放有效节点的个数
int i;
int val; //用来临时存放用户输入的结点的值
//分配了一个不存放有效数据的头结点
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if(NULL == pHead)
{
printf("分配失败,程序终止!\n");
exit(-1);
}
PNODE pTail = pHead;
pTail->pNext = NULL;
printf("请输入您需要生成的链表节点的个数:len = ");
scanf("%d", &len);
for(i = 0; i < len; ++i)
{
printf("请输入第%d个节点的值:", i+1);
scanf("%d", &val);
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pHead)
{
printf("分配失败,程序终止!\n");
exit(-1);
}
pNew->data = val;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
//遍历链表
void traverse_List(PNODE pHead)
{
PNODE p = pHead->pNext;
while(NULL != p)
{
printf("%d ", p->data);
p = p->pNext;
}
printf("\n");
return;
}
//判断链表是否为空
bool is_empty(PNODE pHead)
{
if(NULL == pHead->pNext)
return true;
else
return false;
}
//求链表长度
int length_List(PNODE pHead)
{
PNODE p = pHead->pNext;
int len = 0;
while(NULL != p)
{
++len;
p = p->pNext;
}
return len;
}
//排序 与数组的选择排序进行比较
void sort_List(PNODE pHead)
{
int i, j ,t;
int len = length_List(pHead);
PNODE p, q;
for(i = 0, p = pHead->pNext; i < len - 1; ++i, p = p->pNext)
{
for(j = i+1, q = p->pNext; j < len; ++j, q = q->pNext)
{
if(p->data > q->data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
}
}
return;
}
//在pHead所指向的链表第pos个节点的前面插入一个新的节点,该节点的值是val,并且pos的值是从1开始的
bool insert_List(PNODE pHead, int pos, int val)
{
int i = 0;
PNODE p = pHead;
while(NULL != p && i < pos-1)
{
p = p->pNext;
++i;
}
if(i > pos-1 || NULL == p)
return false;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("动态内存分配失败\n");
exit(-1);
}
pNew->data = val;
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
return true;
}
//删除
bool delete_List(PNODE pHead, int pos, int *pval)
{
int i = 0;
PNODE p = pHead;
while(NULL != p->pNext && i < pos-1)
{
p = p->pNext;
++i;
}
if(i > pos-1 || NULL == p->pNext)
return false;
PNODE q = p->pNext;
*pval = q->data;
//删除p节点后面的结点
p->pNext = p->pNext->pNext;
free(q);
q = NULL;
return true;
}
参考资料
[1]数据结构–郝斌link.