forked from 131/lab6_list
pain
This commit is contained in:
154
list.c
154
list.c
@@ -1,4 +1,3 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -21,26 +20,56 @@ list_node_orphan(list_node *li)
|
||||
return li;
|
||||
}
|
||||
|
||||
//NOTE: Можем ли мы расчитывать на то, что node->next валидно после вызова list_free_cb???
|
||||
void
|
||||
list_clear(list *l, list_free_cb cb)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
list_node *current = l->head;
|
||||
while (current != NULL) {
|
||||
list_node *next = current->next;
|
||||
if (cb) {
|
||||
cb(current);
|
||||
} else {
|
||||
free(current);
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
l->head = NULL;
|
||||
l->tail = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
list_push_front(list *l, list_node *item)
|
||||
{
|
||||
assert(l && item);
|
||||
// <YOURCODE>
|
||||
list_node_orphan(item);
|
||||
|
||||
if (l->head == NULL) {
|
||||
l->head = item;
|
||||
l->tail = item;
|
||||
return;
|
||||
}
|
||||
|
||||
item->next = l->head;
|
||||
l->head->prev = item;
|
||||
l->head = item;
|
||||
}
|
||||
|
||||
void
|
||||
list_push_back(list *l, list_node *item)
|
||||
{
|
||||
assert(l && item);
|
||||
// <YOURCODE>
|
||||
list_node_orphan(item);
|
||||
|
||||
if (l->head == NULL) {
|
||||
l->head = item;
|
||||
l->tail = item;
|
||||
return;
|
||||
}
|
||||
|
||||
item->prev = l->tail;
|
||||
l->tail->next = item;
|
||||
l->tail = item;
|
||||
}
|
||||
|
||||
list_node *
|
||||
@@ -61,39 +90,96 @@ size_t
|
||||
list_len(list *l)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
return 0;
|
||||
size_t len = 0;
|
||||
list_node *current = l->head;
|
||||
while (current != NULL) {
|
||||
len++;
|
||||
current = current->next;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
list_node *
|
||||
list_get(list *l, int idx)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
return NULL;
|
||||
if (idx < 0) return NULL;
|
||||
|
||||
list_node *current = l->head;
|
||||
for (int i = 0; current != NULL && i < idx; i++) {
|
||||
current = current->next;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
void
|
||||
list_insert(list *l, list_node *item, int idx)
|
||||
{
|
||||
assert(l && item);
|
||||
// <YOURCODE>
|
||||
|
||||
if (idx < 0) {
|
||||
list_push_back(l, item);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t length = list_len(l);
|
||||
if (idx > length) {
|
||||
xerror(1, "Index out of bounds in list_insert");
|
||||
return;
|
||||
}
|
||||
|
||||
if (idx == 0) {
|
||||
list_push_front(l, item);
|
||||
return;
|
||||
}
|
||||
|
||||
if (idx == length) {
|
||||
list_push_back(l, item);
|
||||
return;
|
||||
}
|
||||
|
||||
list_node *prev_node = list_get(l, idx - 1);
|
||||
list_node_orphan(item);
|
||||
|
||||
item->next = prev_node->next;
|
||||
item->prev = prev_node;
|
||||
prev_node->next->prev = item;
|
||||
prev_node->next = item;
|
||||
}
|
||||
|
||||
list_node *
|
||||
list_pop_front(list *l)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
return NULL;
|
||||
if (l->head == NULL) return NULL;
|
||||
list_node *node = l->head;
|
||||
l->head = node->next;
|
||||
|
||||
if (l->head != NULL) {
|
||||
l->head->prev = NULL;
|
||||
} else {
|
||||
l->tail = NULL;
|
||||
}
|
||||
|
||||
return list_node_orphan(node);
|
||||
}
|
||||
|
||||
list_node *
|
||||
list_pop_back(list *l)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
return NULL;
|
||||
if (l->tail == NULL) return NULL;
|
||||
|
||||
list_node *node = l->tail;
|
||||
l->tail = node->prev;
|
||||
|
||||
if (l->tail != NULL) {
|
||||
l->tail->next = NULL;
|
||||
} else {
|
||||
l->head = NULL;
|
||||
}
|
||||
|
||||
return list_node_orphan(node);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -114,18 +200,48 @@ list_node *
|
||||
list_remove(list *l, int idx)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
return NULL;
|
||||
if (idx < 0 || l->head == NULL) return NULL;
|
||||
|
||||
if (idx == 0) return list_pop_front(l);
|
||||
|
||||
list_node *node = list_get(l, idx);
|
||||
if (node == NULL) return NULL;
|
||||
|
||||
if (node->prev != NULL) {
|
||||
node->prev->next = node->next;
|
||||
}
|
||||
|
||||
if (node->next != NULL) {
|
||||
node->next->prev = node->prev;
|
||||
} else {
|
||||
l->tail = node->prev;
|
||||
}
|
||||
|
||||
return list_node_orphan(node);
|
||||
}
|
||||
|
||||
void
|
||||
list_reverse(list *l)
|
||||
{
|
||||
assert(l);
|
||||
// <YOURCODE>
|
||||
if (l->head == NULL) return;
|
||||
|
||||
list_node *current = l->head;
|
||||
list_node *temp = NULL;
|
||||
l->tail = l->head;
|
||||
|
||||
while (current != NULL) {
|
||||
temp = current->prev;
|
||||
current->prev = current->next;
|
||||
current->next = temp;
|
||||
current = current->prev;
|
||||
}
|
||||
|
||||
if (temp != NULL) {
|
||||
l->head = temp->prev;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
list_pass(list *l, list_pass_cb cb, void *opaq)
|
||||
{
|
||||
@@ -133,4 +249,4 @@ list_pass(list *l, list_pass_cb cb, void *opaq)
|
||||
list_for_each(iter, l) {
|
||||
cb(iter, opaq);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user