更新了文件名。开始了数据结构
This commit is contained in:
48
Chapter11/ds/line/list/linklist/simple/nohead/main.c
Normal file
48
Chapter11/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
Chapter11/ds/line/list/linklist/simple/nohead/makefile
Normal file
7
Chapter11/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
Chapter11/ds/line/list/linklist/simple/nohead/nohead.c
Normal file
106
Chapter11/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
Chapter11/ds/line/list/linklist/simple/nohead/nohead.h
Normal file
26
Chapter11/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);
|
||||
Reference in New Issue
Block a user