用C语言简单实现一个单向链表:
编写一个程序将一个头结点指针为pa的单链表A分解成两个单链表A和B,其头结点指针分别为pa和pb,使得A链表中含有原链表A中序号为奇数的元素,而链表B中含有原链表A中序号为偶数的元素,且保持原来的相对顺序。 题目什么意思?
意思是一开始有一个链表a
执行了一段代码后又生成了一个链表b
链表a里面是奇数,链表b里面是偶数
是这样吗?
没有完完全全按照题目要求来写代码
如果你是为了交作业的话,你可以参考这个代码写一个你自己的版本
如果你是为了学习的话,这个代码中有好多你需要学习的东西
$ ls
list.clist.hmain.c
$ cat main.c
#include "list.h"
#include <stdio.h>
void output(const list_t *list, size_t index) {
if(index >= list_size(list)) return;
size_t temp; list_get(list, index, &temp, sizeof(temp));
printf("%zd ", temp);
output(list, index + 1);
}
int main(void) {
list_t *a = list_init();
list_t *b = list_init();
for(size_t i = 0; i < 10; ++i) {
list_append(a, &i, sizeof(i));
}
printf("a: "); output(a, 0); puts("");
// 这里要在同一个循环中删除同一个链表中的很多元素
// 那就只能是倒着遍历这个链表了
for(ssize_t i = list_size(a) - 1; i >= 0; --i) {
if(i % 2 != 0) continue;
size_t temp; list_get(a, i, &temp, sizeof(temp));
list_delete(a, i);
list_insert(b, 0, &temp, sizeof(temp));
}
printf("a: "); output(a, 0); puts("");
printf("b: "); output(b, 0); puts("");
list_deinit(b);
list_deinit(a);
return 0;
}
$ cat list.h
#ifndef _LIST_H_
#define _LIST_H_
#include <stddef.h>
#include <stdbool.h>
struct list_node_tag {
void *data; size_t size;
struct list_node_tag *next;
};
typedef struct {
struct list_node_tag *head;
size_t size;
} list_t;
list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);
#endif
$ cat list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>
list_t *list_init(void) {
list_t *list = malloc(sizeof(*list));
if(!list) return NULL;
list->head = NULL;
list->size = 0;
return list;
}
void list_deinit(list_t *list) {
if(!list) return;
list_clean(list);
free(list);
}
bool list_clean(list_t *list) {
if(!list) return false;
while(!list_empty(list)) list_delete(list, 0);
return true;
}
bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
if(!list) return false;
if(list_size(list) < index) return false;
if(!data) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *node = malloc(sizeof(*node));
if(!node) return false;
node->data = malloc(size);
if(!node->data) {free(node); return false;}
memcpy(node->data, data, size);
node->size = size;
node->next = *current;
*current = node;
++list->size;
return true;
}
bool list_delete(list_t *list, size_t index) {
if(!list) return false;
if(list_size(list) <= index) return false;
struct list_node_tag **current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
*current = temp->next;
free(temp->data); free(temp);
--list->size;
return true;
}
bool list_get(const list_t *list, size_t index, void *data, size_t size) {
if(!list) return false;
if(list_size(list) <= index) return false;
if(!data) return false;
struct list_node_tag *const *current = &list->head;
while(index--) current = &(*current)->next;
struct list_node_tag *temp = *current;
if(temp->size > size) return false;
memcpy(data, temp->data, temp->size);
return true;
}
bool list_set(list_t *list, size_t index, const void *data, size_t size) {
bool res = list_delete(list, index);
return res ? list_insert(list, index, data, size) : res;
}
bool list_append(list_t *list, const void *data, size_t size) {
if(!list) return false;
return list_insert(list, list_size(list), data, size);
}
size_t list_size(const list_t *list) {
if(!list) return 0;
return list->size;
}
bool list_empty(const list_t *list) {
if(!list) return true;
return list_size(list) == 0;
}
$ gcc -g -Wall -o main main.c list.c
$ ls
list.clist.hmainmain.c
$ ./main
a: 0 1 2 3 4 5 6 7 8 9
a: 1 3 5 7 9
b: 0 2 4 6 8
$
页:
[1]