#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *pNext;
}Node;
Node *create_list()
{
Node *phead = (Node *)malloc(sizeof(Node));
if(phead == NULL)
{
printf("分配内存失败,程序终止!\n");
exit(-1);
}
phead->pNext = NULL;
return phead;
}
Node *add_tail(Node *phead)
{
int num = 0;
int i = 0;
int val = 0;
Node *ptail = phead;
ptail->pNext = NULL;
printf("请输入想插入的节点数:\t");
scanf("%d",&num);
for(i = 0;i < num;i++)
{
printf("请输入第%d个插入的值:\t",i+1);
scanf("%d",&val);
Node *pnew = (Node *)malloc(sizeof(Node));
if(pnew == NULL)
{
printf("分配内存失败,程序终止!\n");
exit(-1);
}
pnew->data = val;
ptail->pNext = pnew;
pnew->pNext = NULL;
ptail = pnew;
}
return phead;
}
Node *add_head(Node *phead)
{
int num = 0;
int i = 0;
int val = 0;
printf("请输入想插入的节点数:\t");
scanf("%d",&num);
for(i = 0;i < num;i++)
{
printf("请输入第%d个插入的值:\t",i+1);
scanf("%d",&val);
Node *pnew = (Node *)malloc(sizeof(Node));
if(pnew == NULL)
{
printf("分配内存失败,程序终止!\n");
exit(-1);
}
pnew->data = val;
pnew->pNext = phead->pNext;
phead->pNext = pnew;
}
}
void traverse_list(Node *phead)
{
printf("--------遍历链表--------\n");
Node *ptr = phead;
if(ptr->pNext == NULL)
{
printf("该链表为空!\n");
return ;
}
while (ptr->pNext)
{
printf("%d\t",ptr->pNext->data);
ptr = ptr->pNext;
}
printf("\n");
}
int is_Empty(Node *phead)
{
if(phead->pNext == NULL)
{
return 1;
}
else
{
return 0;
}
}
int length_list(Node *phead)
{
int len = 0;
Node *ptr = phead;
while (ptr->pNext)
{
++len;
ptr->pNext = ptr->pNext->pNext;
}
return len;
}
void insert_list(Node *phead)
{
int pos = 0;
int val = 0;
Node *ptr = phead;
if(pos < 0)
{
printf("pos error!\n");
return ;
}
printf("请输入想要插入的位置:\t");
scanf("%d",&pos);
int count = 0;
while(count < (pos - 1) && ptr != NULL)
{
ptr = ptr->pNext;
++count;
}
if(count > pos - 1 || ptr == NULL)
{
printf("pos error!\n");
return ;
}
Node *pnew = (Node *)malloc(sizeof(Node));
if(pnew == NULL)
{
printf("分配内存失败,程序终止!\n");
return ;
}
printf("请输入想要插入的数据:\t");
scanf("%d",&val);
pnew->data = val;
pnew->pNext = ptr->pNext;
ptr->pNext = pnew;
}
void delete_list(Node *phead)
{
int pos = 0;
int val = 0;
Node *ptr = phead;
if(pos < 0)
{
printf("pos error!\n");
return ;
}
printf("请输入想要删除的位置:\t");
scanf("%d",&pos);
int count = 0;
while(count < (pos- 1) && ptr != NULL)
{
ptr = ptr->pNext;
++count;
}
if(count > pos - 1 || ptr->pNext == NULL)
{
printf("超出范围,无法删除!\n");
return ;
}
printf("要删除的数据是:%d\n",ptr->pNext->data);
ptr->pNext = ptr->pNext->pNext;
}
void sort_list(Node *phead)
{
int temp = 0;
Node *p,*q;
for(p = phead->pNext ; p != NULL ; p = p->pNext)
{
for(q = p->pNext ; q != NULL ;q = q->pNext)
{
if(p->data < q->data)
{
temp = p->data;
p->data = q->data;
q->data = temp;
}
}
}
}
void reverse_list(Node *phead)
{
Node *ptr = phead->pNext;
if (ptr != NULL)
{
reverse_list(ptr);
printf("%d\t",ptr->data);
}
}
int main()
{
Node *phead = create_list();
add_tail(phead);
traverse_list(phead);
// delete_list(phead);
// traverse_list(phead);
// int ret = is_Empty(phead);
// printf("ret = %d\n",ret);
insert_list(phead);
traverse_list(phead);
// delete_list(phead);
sort_list(phead);
traverse_list(phead);
printf("---------逆序输出---------\n");
reverse_list(phead);
printf("\n");
int ret_len = length_list(phead);
printf("链表的长度:%d\n",ret_len);
return 0;
}