#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include<stddef.h>
#include<stdbool.h>
typedef struct Node
{
int data;/*数据域*/
struct Node * pNext;/*指针域*/
}NODE,* PNODE;
PNODE create_list();/*创建链表*/
void traverse_list(PNODE pHEad);/*遍历*/
bool delete_list(PNODE pHead,int,int*);/*删除*/
void sort_list(PNODE);/*排序*/
bool is_empty_list(PNODE pHead);/*判断是否为空*/
int len_list(PNODE pHead);/*链表长度*/
bool inser_list(PNODE pHead,int pos,int val);/*插入元素*/
int main()
{
int a,b;/*方便insert_list函数插入数据*/
int val;/*方便后面删除元素实验*/
PNODE pHead = NULL; /*等价于 struct Node* pHead = NULL*/
pHead = create_list();/*创建一个非循环单链表,并将该链表的头节点的地址赋给pHead*/
traverse_list(pHead);/*遍历*/
if(is_empty_list(pHead))
{
printf("The list is empty!\n");
exit(-1);/*用户输入0,终止程序*/
}
else
{
printf("The list is existent!\n");
}
/*输出长度*/
int len = len_list(pHead);
printf("The list'length is %d\n",len);
/*遍历链表*/
sort_list(pHead);
traverse_list(pHead);
/*插入元素*/
printf("Input the num:");/*val*/
scanf("%d",&a);
printf("选择位置:");/*pos*/
scanf("%d",&b);
inser_list(pHead,a,b);
printf("插入新元素后:");
traverse_list(pHead);
/*删除元素*/
printf("Input the num which you what to delete:");
scanf("%d",&val);
if(delete_list(pHead,2,&val))
{
printf("删除成功\n");
}
else
{
printf("删除失败!你查找的元素不存在\n");
}
traverse_list(pHead);
return 0;
}
/*构造链表*/
PNODE create_list()
{
int len,val;
PNODE pHead = (PNODE)malloc(sizeof(NODE));/*分配一个不存放数据的头节点*/
if(NULL == pHead)
{
printf("Failed!\n");
exit(-1);
}
PNODE pNULL = pHead;
pNULL->pNext = NULL;
printf("Input the num of linked list nodes:");
scanf("%d",&len);
printf("\n");
for(int i=0;i<len;i++)
{
printf("input the num of %d node:",i+1);
scanf("%d",&val);
PNODE pNew = (PNODE)malloc(sizeof(NODE));/*动态分配内存*/
if(pNew == NULL)
{
printf("Failed!\n");
exit(-1);/*终止程序*/
}
else
{
pNew->data = val;
pNULL->pNext = pNew;
pNew->pNext = NULL;
pNULL = 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_list(PNODE pHead)
{
if(NULL == pHead->pNext)
{
return true;
}
else
{
return false;
}
}
/*链表长度*/
int len_list(PNODE pHead)
{
PNODE p = pHead->pNext;
int len = 0;
while(NULL != p)
{
++len;
p = p->pNext;
}
printf("\n");
return len;
}
/*冒泡排序*/
void sort_list(PNODE pHead)
{
int r;/*交换数据*/
int i,j;/*控制循环,避免指针越界问题*/
int len = len_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)
{
r = p->data;
p->data = q->data;
q->data = r;
}
}
}
return;
}
/*插入元素*/
/*在pHead所指向的链表的第pos个节点出插入一个新节点,该节点的值为val,并且pos的值是从1开始的*/
bool inser_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;
}
/*此时完成p指向pos-1个节点*/
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("Error:动态分配内存失败!\n");
exit(-1);
}
else
{
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;
}
|