===========================
2022-8-3
===========================
note
一.不用写调用,只写声明和实现
实现有头单向不循环链表的
遍历和销毁
任务(最基本的要求)
1.大家可以在纸上写出简单的(有头双向循环链表)增删改查
2.大家可以在电脑上写出通用性强的链表
3.大家可以使用内核链表(会用)
自己重新写一遍有头单向循环链表的功能
1.创建头结点 llist_create();
2.插入数据结点 llist_insert();
(头插法 尾插法)
3.遍历链表 llist_display();
4.销毁链表 llist_destroy();
-----------------------------------
5.查找结点 llist_find();
6.删除结点 llist_delete();
7.拿出结点 llist_fetch();
==============================
作业
1.把今天讲过的内容完完整整的复习一遍
2.重新写一遍有头单向循环链表的各种功能
3.写一个无头单向不循环的链表
4.完成joseph杀人游戏
==============================
================================================================================
无头单向不循环的链表
/*
实现 无头单向不循环的链表
无头 : 代表没有头结点
在无头的链表中所有的结点都是数据结点
1.插入数据结点 llist_insert();
由于没有头结点所以直接把新的数据结点插入到链表最后
2.遍历链表 llist_display();
3.销毁链表 llist_destroy();
------------------------------------
4.查找结点 llist_find();
5.删除结点 llist_delete();
6.拿出结点 llist_fetch();
*/
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <stdio.h> #include "llist.h"
int main(void) { LLIST *l = NULL; LLIST *find = NULL; LLIST save; int ret = 0; int find_id = 100; int num = 0; char *find_name = "stu9";
printf("Please Insert Node Number : "); scanf("%d", &num);
l = llist_insert(num); if(l == NULL) return -1; llist_display(l);
printf("==================\n");
#if 0 ret = llist_delete(&l, find_name); if(ret != 0) printf("Delete It Is Failed!\n"); else printf("Delete It!\n"); #else ret = llist_fetch(&l, &find_id, &save); if(ret != 0) printf("Fetch It Is Failed!\n"); else printf("Fetch It! %d %s %d\n", save.id, save.name,save.math); #endif printf("==================\n");
llist_display(l);
llist_destroy(l);
return 0; }
|
llist.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #ifndef __LLIST_H #define __LLIST_H
#define NAMESIZE 20
typedef struct llist_node { int id; char name[NAMESIZE]; int math; struct llist_node *next; }LLIST;
LLIST *llist_insert(int);
void llist_display(LLIST *);
void llist_destroy(LLIST *);
LLIST *llist_find(LLIST *, const void *);
int llist_delete(LLIST **, const void *);
int llist_fetch(LLIST **, const void *, void *);
#endif
|
llist.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include "llist.h"
LLIST *llist_insert(int num) { LLIST *l = NULL; LLIST *p = NULL; LLIST *newnode = NULL; LLIST data; int i = 0;
l = malloc(sizeof(LLIST)); if(l == NULL) return NULL;
data.id = 100 + i; sprintf(data.name, "stu%d", i); data.math = 100 - i; data.next = NULL; memcpy(l, &data, sizeof(LLIST)); p = l; for(i = 1; i < num; i++) { newnode = malloc(sizeof(LLIST)); if(newnode == NULL) return l; data.id = 100 + i; sprintf(data.name, "stu%d", i); data.math = 100 - i; data.next = NULL; memcpy(newnode, &data, sizeof(LLIST)); p->next = newnode; p = p->next; }
return l; }
void llist_display(LLIST *l) { LLIST *cur = NULL;
for(cur = l; cur != NULL; cur = cur->next) printf("%d %s %d\n",cur->id,cur->name,cur->math); }
void llist_destroy(LLIST *l) { LLIST *back = l; LLIST *cur = l->next;
while(cur != NULL) { back->next = cur->next; free(cur); cur = back->next; } free(back); }
LLIST *llist_find(LLIST *l, const void *find_data) { LLIST *cur = NULL; for(cur = l; cur != NULL; cur = cur->next) { if(cur->id==*(int*)find_data || !(strcmp(cur->name, find_data))) return cur; } return NULL; }
int llist_delete(LLIST **l, const void *find_data) { LLIST *back = *l; LLIST *cur = NULL;
cur = llist_find(*l, find_data); if(cur == NULL) return -1; if(cur == *l) (*l) = (*l)->next; else { while(back->next != cur) back = back->next; back->next = cur->next; } free(cur); return 0; }
int llist_fetch(LLIST **l, const void *find_data, void *save) { LLIST *back = *l; LLIST *cur = NULL;
cur = llist_find(*l, find_data); if(cur == NULL) return -1; if(cur == *l) (*l) = (*l)->next; else { while(back->next != cur) back = back->next; back->next = cur->next; } memcpy(save, cur, sizeof(LLIST)); free(cur); return 0; }
|
makefile
1 2 3 4
| llist : main.o llist.o gcc -o $@ $^ clean : rm -rf *.o llist
|
================================================================================
需要实现有头单向循环链表
/*
需要实现有头单向循环链表
1.创建头结点 llist_create();
2.插入数据结点 llist_insert();
头插法 尾插法
3.遍历链表 llist_display();
4.销毁链表 llist_destroy();
===================================
5.查找结点 llist_find();
6.删除结点 llist_delete();
7.拿出数据 llist_fetch();
*/
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <stdio.h> #include "llist.h"
int main(void) { LLIST *handler = NULL; LLIST *find = NULL; LLIST data; LLIST save; int i = 0; int ret = 0; int find_id = 100; char *find_name = "stu4";
handler = llist_create(); if(handler == NULL) return -1; for(i = 0; i < 5; i++) { data.id = 100 + i; sprintf(data.name, "stu%d", i); data.math = 100 - i; data.next = NULL; llist_insert(handler,&data,TAILINSERT); } llist_display(handler);
printf("=========================\n");
#if 0 ret = llist_delete(handler, "stu9"); if(ret != 0) printf("Delete It Is Failed!\n"); else printf("Delete It!\n"); #else ret = llist_fetch(handler,find_name, &save); if(ret != 0) printf("Fetch It Is Failed!\n"); else printf("Fetch It! %d %s %d\n",save.id, save.name, save.math); #endif printf("=========================\n");
llist_display(handler);
llist_destroy(handler);
return 0; }
|
llist.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #ifndef __LLIST_H #define __LLIST_H
#define HEADINSERT 0 #define TAILINSERT 1 #define NAMESIZE 20
typedef struct llist_node//定义了每个结点的结构体类型 { int id; char name[NAMESIZE]; int math; struct llist_node *next; }LLIST;
LLIST *llist_create();
int llist_insert(LLIST *, const void *, int );
void llist_display(LLIST *);
void llist_destroy(LLIST *);
LLIST *llist_find(LLIST *, const void *);
int llist_delete(LLIST *, const void *);
int llist_fetch(LLIST *, const void *, void *);
#endif
|
llist.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include "llist.h"
LLIST *llist_create() { LLIST *handler = NULL;
handler = malloc(sizeof(LLIST)); if(handler == NULL) return NULL; handler->next = handler;
return handler; }
int llist_insert(LLIST *handler,const void *data,int mode) { LLIST *p = handler; LLIST *newnode = NULL;
newnode = malloc(sizeof(LLIST)); if(newnode == NULL) return -1; memcpy(newnode, data, sizeof(LLIST)); switch(mode) { case HEADINSERT : break; case TAILINSERT : while(p->next != handler) p = p->next; break; default : free(newnode); return -2; } newnode->next = p->next; p->next = newnode; return 0; }
void llist_display(LLIST *handler) { LLIST *cur = NULL;
for(cur=handler->next; cur!=handler; cur=cur->next) printf("%d %s %d\n",cur->id,cur->name,cur->math); }
void llist_destroy(LLIST *handler) { LLIST *back = handler; LLIST *cur = handler->next;
while(cur != handler) { back->next = cur->next; free(cur); cur = back->next; } free(cur); }
LLIST *llist_find(LLIST *handler, const void *find_data) { LLIST *cur = NULL;
for(cur=handler->next;cur!=handler;cur=cur->next) { if(cur->id==*(int*)find_data || !(strcmp(cur->name, find_data))) return cur; } return NULL; }
int llist_delete(LLIST *handler, const void *find_data) { LLIST *back = handler; LLIST *cur = NULL;
cur = llist_find(handler, find_data); if(cur == NULL) return -1; while(back->next != cur) back = back->next;
back->next = cur->next; free(cur); return 0; }
int llist_fetch(LLIST *handler, const void *find_data, void *save) { LLIST *back = handler; LLIST *cur = NULL;
cur = llist_find(handler, find_data); if(cur == NULL) return -1; while(back->next != cur) back = back->next;
back->next = cur->next; memcpy(save, cur, sizeof(LLIST)); free(cur); return 0; }
|
makefile
1 2 3 4
| llist : main.o llist.o gcc -o $@ $^ clean : rm -rf *.o llist
|