/*
时间:2019年10月13日11:16:02
说明:链表的一些常用操作
*/
#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);
bool insert_list(PNODE,int,int);
bool delete_list(PNODE,int,int*);
void sort_list(PNODE);
int main(void)
{
PNODE pHead = NULL;//等价于struct Node * pHead = NULL;
pHead = create_list(); //create_list()功能:创建一个非循环单链表,并将该链表的头结点的地址付给pHead
traverse_list(pHead);
if(is_empty(pHead))
{
printf("链表为空!\n");
}
else
{
printf("链表不空!\n");
}
int length = length_list(pHead);
printf("链表的长度为:%d\n",length);
printf("排序后,");
sort_list(pHead);
traverse_list(pHead);
if(insert_list(pHead,4,26))
{
printf("插入节点后,");
traverse_list(pHead);
}
else
{
printf("插入节点失败!\n");
}
int val;
if(delete_list(pHead,4,&val))
{
printf("删除的元素为:%d,删除节点后,",val);
traverse_list(pHead);
}
else
{
printf("删除节点失败!\n");
}
return 0;
}
PNODE create_list(void)
{
int len; //用来存放有效节点的个数
printf("请输入您需要生成的链表节点的个数:len = ");
scanf("%d",&len);
int val;//用来临时存放用户输入的节点的值
//分配了一个不分配有效数据的头结点,pHead是头指针,pHead指向的是一个头结点
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if(NULL == pHead)
{
printf("分配失败,程序终止!\n");
exit(-1);
}
PNODE pTail = pHead;//pTail表示永远指向尾节点
pTail->pNext = NULL;
for(int i = 0;i < len;i++)
{
printf("请输入第%d个节点的值:",i+1);
scanf("%d",&val);
//每循环一次,pNew都会创造出一个新的节点
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("分配失败,程序终止!\n");
exit(-1);
}
pNew->data = val;
pTail->pNext = pNew; //将pNew挂到pHead后面
pNew->pNext = NULL;
pTail = pNew;
}
return pHead;
}
void traverse_list(PNODE pHead)
{
PNODE p = pHead;
printf("该链表的值为:");
while(p->pNext != NULL)
{
printf("%d ",p->pNext->data);
p = p->pNext;
}
printf("\n");
}
bool is_empty(PNODE pHead)
{
if(NULL == pHead->pNext)
{
return true;
}
else
{
return false;
}
}
int length_list(PNODE pHead)
{
int len = 0;
PNODE p = pHead->pNext;
while(p != NULL)
{
p = p->pNext;
len++;
}
return len;
}
void sort_list(PNODE pHead)
{
PNODE p,q;
int t;
for(p = pHead->pNext;p != NULL;p = p->pNext)
{
for(q = p->pNext;q != NULL;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 && pos-1 > i)
{
p = p->pNext;
i++;
}
if(i > pos-1 || p == NULL)
{
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(p->pNext != NULL && 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;
}
数据结构之链表的操作
原文:https://www.cnblogs.com/baoyingying/p/11665503.html