环境:gcc
目的:链式存储单向表练习
功能如下:
1. 创建一个空顺序存储表
2. 表中插入数据分3种情况
1)表头插入
2)表尾插入
3)在指定位置插入
4 在指定位置删除一个元素
5 将表中的数据元素倒序
6 遍历顺序存储表
7. 释放链表内存
链式存储单向表结构:
/*************************************************************************
@Author: wanghao
@Created Time : Tue 08 May 2018 10:12:00 PM PDT
@File Name: linklist.c
@Description:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
typedef struct node
{
data_t data;
struct node *pnext;
}NODE;
/*Create empty list*/
NODE *crete_empty_list(void);
/*Insert list data in head way*/
int insert_list_data_head(NODE *list, data_t data);
/*Insert list data in tail way*/
int insert_list_data_tail(NODE *list, data_t data);
/*Insert list data at a certain location*/
int insert_list_data(NODE *list, int location, data_t data);
/*Delete list data at a certain location*/
int delete_list_data(NODE *list, int location);
/*Reverse list data*/
int reverse_list_data(NODE *list);
/*Seek the sum max with two location*/
NODE *sum_max_location(NODE *list);
/*Free list memory*/
int free_list(NODE *list);
/*Print list data*/
int print_list(NODE *list);
int main(int argc, const char *argv[])
{
int i = 0;
NODE *phead = NULL;
phead = crete_empty_list();
if(!phead)
{
return 1;
}
for(i = 10; i < 30; i += 3)
{
insert_list_data_head(phead, i);
}
for(i = 40; i < 50; i += 3)
{
insert_list_data_tail(phead, i);
}
insert_list_data(phead, 1, 111);
insert_list_data(phead, 2, 222);
insert_list_data(phead, 3, 333);
insert_list_data(phead, 4, 444);
print_list(phead);
delete_list_data(phead, 1);
delete_list_data(phead, 1);
delete_list_data(phead, 1);
delete_list_data(phead, 1);
print_list(phead);
reverse_list_data(phead);
print_list(phead);
NODE *result = sum_max_location(phead);
printf("max data is %d\n",result->data);
free_list(phead);
print_list(phead);
return 0;
}
NODE *crete_empty_list(void)
{
NODE *pnode = NULL;
pnode = (NODE *)malloc(sizeof(NODE));
if(!pnode)
{
printf("Malloc list node mem fail!\n");
return NULL;
}
pnode->pnext = NULL;
return pnode;
}
int insert_list_data_head(NODE *list, data_t data)
{
NODE *newnode;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
newnode = crete_empty_list();
if(!newnode)
{
printf("Insert list data fail!\n");
return 1;
}
newnode->data = data;
newnode->pnext = list->pnext;
list->pnext = newnode;
return 0;
}
int insert_list_data_tail(NODE *list, data_t data)
{
NODE *ptail;
NODE *newnode;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
ptail = list;
while(ptail->pnext)
{
ptail = ptail->pnext;
}
newnode = crete_empty_list();
if(!newnode)
{
printf("Insert list data fail!\n");
return 1;
}
newnode->data = data;
newnode->pnext = ptail->pnext;
ptail->pnext = newnode;
return 0;
}
int insert_list_data(NODE *list, int location, data_t data)
{
int i;
NODE *p;
NODE *newnode;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
i = 1;
p = list;
while(i < location && p)
{
i++;
p = p->pnext;
}
if(!p)
{
printf("The location is wrong!\n");
return 1;
}
newnode = crete_empty_list();
if(!newnode)
{
printf("Insert list data fail!\n");
return 2;
}
newnode->data = data;
newnode->pnext = p->pnext;
p->pnext = newnode;
return 0;
}
int delete_list_data(NODE *list, int location)
{
int i;
NODE *p = NULL;
NODE *ptemp = NULL;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
i = 1;
p = list;
while((i < location) && (p->pnext))
{
i++;
p = p->pnext;
}
if(!(p->pnext))
{
printf("The location is wrong!\n");
return 1;
}
ptemp = p->pnext;
p->pnext = ptemp->pnext;
free(ptemp);
return 0;
}
int reverse_list_data(NODE *list)
{
NODE *s;
NODE *ptemp = NULL;
s = list->pnext;
list->pnext = NULL;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
while(s)
{
ptemp = s;
s = s->pnext;
ptemp->pnext = list->pnext;
list->pnext = ptemp;
}
return 0;
}
NODE *sum_max_location(NODE *list)
{
int i, j;
int max;
int sum;
NODE *s = NULL;
NODE *pmax;
if(!list)
{
printf("The list is not exist!\n");
return NULL;
}
s = list->pnext;
if(!s)
{
printf("The list is empty!\n");
return NULL;
}
if(!(s->pnext))
{
printf("The list only has one element!\n");
return NULL;
}
i = 0;
pmax = s;
max = s->data + s->pnext->data;
while(s->pnext)
{
sum = s->data + s->pnext->data;
if(sum > max)
{
pmax = s;
}
s = s->pnext;
}
return pmax;
}
int free_list(NODE *list)
{
NODE *s = NULL;
NODE *ptemp = NULL;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
s = list->pnext;
list->pnext = NULL;
while(s)
{
printf("hello world!\n");
ptemp = s->pnext;
free(ptemp);
ptemp = NULL;
s = s->pnext;
}
return 0;
}
/*Print list data*/
int print_list(NODE *list)
{
NODE *p = NULL;
if(!list)
{
printf("The list is not exist!\n");
return -1;
}
p = list->pnext;
while(p)
{
printf("%d\t",p->data);
p = p->pnext;
}
printf("\n");
return 0;
}