更新了文件名。开始了数据结构
This commit is contained in:
156
Chapter11/ds/line/list/linklist/simple/head/list.c
Normal file
156
Chapter11/ds/line/list/linklist/simple/head/list.c
Normal file
@@ -0,0 +1,156 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
list *list_create()
|
||||
{
|
||||
list *me;
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
|
||||
me->next = NULL;
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
// 从第一个有效节点作为第0个
|
||||
int list_insert_at(list *me, int i, datatype *data)
|
||||
{
|
||||
int k = 0;
|
||||
list *node = me, *newnode;
|
||||
|
||||
if (i < 0)
|
||||
return -1;
|
||||
// return -EINVAL;
|
||||
|
||||
while (k < i && node != NULL)
|
||||
{
|
||||
node = node->next;
|
||||
k++;
|
||||
}
|
||||
|
||||
if (NULL != node)
|
||||
{
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return -2;
|
||||
|
||||
newnode->data = *data;
|
||||
newnode->next = NULL;
|
||||
|
||||
newnode->next = node->next;
|
||||
node->next = newnode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return -3;
|
||||
}
|
||||
|
||||
// 有序插入
|
||||
int list_order_insert(list *me, datatype *data)
|
||||
{
|
||||
list *p = me, *q;
|
||||
|
||||
while (p->next && p->next->data < *data)
|
||||
p = p->next;
|
||||
|
||||
q = malloc(sizeof(*q));
|
||||
if (NULL == q)
|
||||
return -1;
|
||||
|
||||
q->data = *data;
|
||||
q->next = p->next;
|
||||
p->next = q;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int list_delete_at(list *me, int i, datatype *data)
|
||||
{
|
||||
int k = 0;
|
||||
list *p = me, *q;
|
||||
|
||||
*data = -1;
|
||||
|
||||
if (i < 0)
|
||||
return -1;
|
||||
|
||||
while (k < i)
|
||||
{
|
||||
p = p->next;
|
||||
k++;
|
||||
}
|
||||
|
||||
if (p)
|
||||
{
|
||||
q = p->next;
|
||||
p->next = q->next;
|
||||
*data = q->data;
|
||||
free(q);
|
||||
q = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return -2;
|
||||
}
|
||||
|
||||
int list_delete(list *me, datatype *data)
|
||||
{
|
||||
list *p = me, *q;
|
||||
while (p->next && p->next->data != *data)
|
||||
p = p->next;
|
||||
|
||||
if (NULL == p->next)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
q = p->next;
|
||||
p->next = q->next;
|
||||
free(q);
|
||||
q = NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int list_isempty(list *me)
|
||||
{
|
||||
if (NULL == me->next)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void list_display(list *me)
|
||||
{
|
||||
list *node = me->next;
|
||||
if (0 == list_isempty(me))
|
||||
return;
|
||||
|
||||
while (NULL != node)
|
||||
{
|
||||
printf("%d ", node->data);
|
||||
node = node->next;
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void list_destroy(list *me)
|
||||
{
|
||||
list *node, *next;
|
||||
|
||||
for (node = me->next; node != NULL; node = next)
|
||||
{
|
||||
next = node->next;
|
||||
free(node);
|
||||
}
|
||||
|
||||
free(me);
|
||||
|
||||
return;
|
||||
}
|
||||
26
Chapter11/ds/line/list/linklist/simple/head/list.h
Normal file
26
Chapter11/ds/line/list/linklist/simple/head/list.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef LIST_H__
|
||||
#define LIST_H__
|
||||
|
||||
typedef int datatype;
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
datatype data;
|
||||
struct node_st *next;
|
||||
}list;
|
||||
|
||||
list *list_create();
|
||||
|
||||
int list_insert_at(list *me, int i, datatype *data);
|
||||
int list_order_insert(list *me, datatype *data);
|
||||
|
||||
int list_delete_at(list *me, int i, datatype *data);
|
||||
int list_delete(list *me, datatype *data);
|
||||
|
||||
int list_isempty(list *me);
|
||||
|
||||
void list_display(list *me);
|
||||
void list_destroy(list *me);
|
||||
|
||||
|
||||
#endif
|
||||
44
Chapter11/ds/line/list/linklist/simple/head/main.c
Normal file
44
Chapter11/ds/line/list/linklist/simple/head/main.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "list.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
list *l;
|
||||
datatype arr[] = {12, 9, 23, 2, 34, 6, 45};
|
||||
|
||||
l = list_create();
|
||||
if (NULL == l)
|
||||
exit(1);
|
||||
|
||||
for (int i = 0; i < sizeof(arr) / sizeof(*arr); i++)
|
||||
{
|
||||
// if (list_insert_at(l, 0, &arr[i]))
|
||||
if (list_order_insert(l, &arr[i]))
|
||||
exit(1);
|
||||
}
|
||||
|
||||
list_display(l);
|
||||
|
||||
datatype value;
|
||||
int err;
|
||||
|
||||
err = list_delete_at(l, 2, &value);
|
||||
if (err)
|
||||
exit(1);
|
||||
list_display(l);
|
||||
printf("delete:%d\n", value);
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
int value = 12;
|
||||
list_delete(l, &value);
|
||||
list_display(l);
|
||||
#endif
|
||||
|
||||
list_destroy(l);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
7
Chapter11/ds/line/list/linklist/simple/head/makefile
Normal file
7
Chapter11/ds/line/list/linklist/simple/head/makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
all:main
|
||||
|
||||
main:main.o list.o
|
||||
$(CC) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm *.o main -rf
|
||||
Reference in New Issue
Block a user