优化了命名
This commit is contained in:
156
C11-数据结构/ds/line/list/linklist/simple/head/list.c
Normal file
156
C11-数据结构/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
C11-数据结构/ds/line/list/linklist/simple/head/list.h
Normal file
26
C11-数据结构/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
C11-数据结构/ds/line/list/linklist/simple/head/main.c
Normal file
44
C11-数据结构/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
C11-数据结构/ds/line/list/linklist/simple/head/makefile
Normal file
7
C11-数据结构/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
|
||||
117
C11-数据结构/ds/line/list/linklist/simple/josephu/jose.c
Normal file
117
C11-数据结构/ds/line/list/linklist/simple/josephu/jose.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define JOSE_NUM 8
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
int data;
|
||||
struct node_st *next;
|
||||
} list;
|
||||
|
||||
// try something
|
||||
#if 0
|
||||
list *jose_create(int a[], int n)
|
||||
{
|
||||
list *me, *p;
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
me->data = me;
|
||||
|
||||
p = malloc(sizeof(*p));
|
||||
p = me;
|
||||
p->data = a[i];
|
||||
me->next = p;
|
||||
}
|
||||
|
||||
return me;
|
||||
}
|
||||
#endif
|
||||
|
||||
list *jose_create(int n)
|
||||
{
|
||||
list *me, *newnode, *cur;
|
||||
int i = 1;
|
||||
|
||||
me = malloc(sizeof(*me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
me->data = i;
|
||||
me->next = me;
|
||||
i++;
|
||||
|
||||
cur = me;
|
||||
|
||||
for (; i <= n; i++)
|
||||
{
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return NULL;
|
||||
newnode->data = i;
|
||||
newnode->next = me;
|
||||
|
||||
cur->next = newnode;
|
||||
cur = newnode;
|
||||
}
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
void jose_show(list *me)
|
||||
{
|
||||
list *list;
|
||||
|
||||
for (list = me; list->next != me; list = list->next)
|
||||
{
|
||||
// sleep(1);
|
||||
printf("%d ", list->data);
|
||||
// fflush(NULL);
|
||||
}
|
||||
printf("%d\n", list->data);
|
||||
}
|
||||
|
||||
void jose_kill(list **me, int n)
|
||||
{
|
||||
list *cur = *me, *node;
|
||||
int i = 1;
|
||||
|
||||
while (cur != cur->next)
|
||||
{
|
||||
while (i < n)
|
||||
{
|
||||
node = cur;
|
||||
cur = cur->next;
|
||||
i++;
|
||||
}
|
||||
|
||||
printf("%d ", cur->data);
|
||||
node->next = cur->next;
|
||||
free(cur);
|
||||
|
||||
cur = node->next;
|
||||
i = 1;
|
||||
}
|
||||
|
||||
*me = cur;
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
list *list;
|
||||
|
||||
list = jose_create(JOSE_NUM);
|
||||
|
||||
jose_show(list);
|
||||
|
||||
jose_kill(&list, 3);
|
||||
|
||||
jose_show(list);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
48
C11-数据结构/ds/line/list/linklist/simple/nohead/main.c
Normal file
48
C11-数据结构/ds/line/list/linklist/simple/nohead/main.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nohead.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, ret;
|
||||
struct node_st *list = NULL;
|
||||
struct score_st tmp;
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
tmp.id = i;
|
||||
snprintf(tmp.name, NAMESIZE, "stu%d", i);
|
||||
tmp.math = rand() % 100;
|
||||
tmp.chinese = rand() % 100;
|
||||
|
||||
// list = list_insert(list, &tmp);
|
||||
ret = list_insert(&list, &tmp);
|
||||
if (0 != ret)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
list_show(list);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
int id = 13;
|
||||
struct score_st *ptr;
|
||||
ptr = list_find(&list, id);
|
||||
if (NULL == ptr)
|
||||
printf("Can not find!\n");
|
||||
else
|
||||
printf("%d %s %d %d", ptr->id, ptr->name, ptr->math, ptr->chinese);
|
||||
|
||||
|
||||
#if 0
|
||||
list_delete(&list);
|
||||
|
||||
list_show(list);
|
||||
#endif
|
||||
|
||||
list_destroy(&list);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
7
C11-数据结构/ds/line/list/linklist/simple/nohead/makefile
Normal file
7
C11-数据结构/ds/line/list/linklist/simple/nohead/makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
all:main
|
||||
|
||||
main:main.o nohead.o
|
||||
$(CC) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm *.o main -rf
|
||||
106
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.c
Normal file
106
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nohead.h"
|
||||
|
||||
|
||||
#if 0
|
||||
struct node_st *list_insert(struct node_st *list, struct score_st *data)
|
||||
{
|
||||
struct node_st *new;
|
||||
|
||||
new = malloc(sizeof(*new));
|
||||
if (NULL == new)
|
||||
return NULL;
|
||||
|
||||
new->data = *data;
|
||||
new->next = list;
|
||||
list = new;
|
||||
|
||||
return list;
|
||||
}
|
||||
#endif
|
||||
|
||||
int list_insert(struct node_st **list, struct score_st *data)
|
||||
{
|
||||
struct node_st *new;
|
||||
|
||||
new = malloc(sizeof(*new));
|
||||
if (NULL == new)
|
||||
return -1;
|
||||
|
||||
new->data = *data;
|
||||
new->next = *list;
|
||||
*list = new;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void list_show(struct node_st *list)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
for (cur = list; NULL != cur; cur = cur->next)
|
||||
{
|
||||
printf("%d %s %d %d\n",
|
||||
cur->data.id,
|
||||
cur->data.name,
|
||||
cur->data.math,
|
||||
cur->data.chinese);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除首位
|
||||
int list_delete(struct node_st **list)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
if (NULL == *list)
|
||||
return -1;
|
||||
|
||||
cur = *list;
|
||||
*list = (*list)->next;
|
||||
|
||||
free(cur);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct score_st *list_find(struct node_st **list, int id)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
if (NULL == *list)
|
||||
return NULL;
|
||||
|
||||
// for (cur = *list; NULL != cur->next; cur = cur->next)
|
||||
// !!! 是cur不为空
|
||||
for (cur = *list; NULL != cur; cur = cur->next)
|
||||
{
|
||||
if (id == cur->data.id)
|
||||
{
|
||||
// printf("%d %s %d %d\n",
|
||||
// cur->data.id,
|
||||
// cur->data.name,
|
||||
// cur->data.math,
|
||||
// cur->data.chinese);
|
||||
return &(cur->data);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int list_destroy(struct node_st **list)
|
||||
{
|
||||
struct node_st *cur;
|
||||
|
||||
if (NULL == list)
|
||||
return -1;
|
||||
|
||||
for (cur = *list; NULL != cur; cur = *list)
|
||||
{
|
||||
*list = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
}
|
||||
26
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.h
Normal file
26
C11-数据结构/ds/line/list/linklist/simple/nohead/nohead.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct score_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
struct node_st
|
||||
{
|
||||
struct score_st data;
|
||||
struct node_st *next;
|
||||
};
|
||||
|
||||
// struct node_st *list_insert(struct node_st *list, struct score_st *data);
|
||||
int list_insert(struct node_st **list, struct score_st *data);
|
||||
|
||||
void list_show(struct node_st *list);
|
||||
|
||||
int list_delete(struct node_st **list);
|
||||
|
||||
struct score_st *list_find(struct node_st **list, int id);
|
||||
|
||||
int list_destroy(struct node_st **list);
|
||||
114
C11-数据结构/ds/line/list/linklist/simple/polynomial/poly.c
Normal file
114
C11-数据结构/ds/line/list/linklist/simple/polynomial/poly.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct node_st
|
||||
{
|
||||
int coef;
|
||||
int exp;
|
||||
struct node_st *next;
|
||||
} list;
|
||||
|
||||
list *poly_create(int a[][2], int n)
|
||||
{
|
||||
list *me, *newnode, *cur;
|
||||
|
||||
me = malloc(sizeof(me));
|
||||
if (NULL == me)
|
||||
return NULL;
|
||||
me->next = NULL;
|
||||
|
||||
cur = me;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
newnode = malloc(sizeof(*newnode));
|
||||
if (NULL == newnode)
|
||||
return NULL;
|
||||
|
||||
newnode->coef = a[i][0];
|
||||
newnode->exp = a[i][1];
|
||||
newnode->next = NULL;
|
||||
|
||||
cur->next = newnode;
|
||||
cur = newnode;
|
||||
}
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
void poly_show(list *me)
|
||||
{
|
||||
list *cur;
|
||||
|
||||
for (cur = me->next; NULL != cur; cur = cur->next)
|
||||
{
|
||||
printf("(%d %d) ", cur->coef, cur->exp);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void poly_union(list *p1, list *p2)
|
||||
{
|
||||
list *p, *q, *r;
|
||||
|
||||
p = p1->next;
|
||||
q = p2->next;
|
||||
r = p1;
|
||||
|
||||
while (p && q)
|
||||
{
|
||||
if (p->exp < q->exp)
|
||||
{
|
||||
r->next = p;
|
||||
r = p;
|
||||
p = p->next;
|
||||
}
|
||||
else if (p->exp > q->exp)
|
||||
{
|
||||
r->next = q;
|
||||
r = q;
|
||||
q = q->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->coef += q->coef;
|
||||
if (p->coef)
|
||||
{
|
||||
r->next = p;
|
||||
r = p;
|
||||
}
|
||||
|
||||
p = p->next;
|
||||
q = q->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (NULL == p)
|
||||
r->next = q;
|
||||
else
|
||||
r->next = p;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[][2] = {{5, 0}, {2, 1}, {8, 8}, {3, 16}};
|
||||
int b[][2] = {{6, 1}, {16, 6}, {-8, 8}};
|
||||
|
||||
list *p1, *p2;
|
||||
|
||||
p1 = poly_create(a, 4);
|
||||
if (NULL == p1)
|
||||
exit(1);
|
||||
|
||||
p2 = poly_create(b, 3);
|
||||
if (NULL == p2)
|
||||
exit(1);
|
||||
|
||||
poly_show(p1);
|
||||
poly_show(p2);
|
||||
|
||||
poly_union(p1, p2);
|
||||
poly_show(p1);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user