A simple tail pointer single-way linked list (C realization)

xiaoxiao2021-03-06  53

Write a one-way linked list with a header pointer with c, which is only inserted on the tail, and the operation is performed in any location. Because it is only used, because it is lazy, it has not been expanded. Because the insert is fixed at the end, the advantage of a tail pointer is obvious. Of course, you should pay some overhead when deleting.

List.h

-------------------------------------------

/ * list.h ** Copyright 2004 coon Xu. ** Author: coon Xu ** Date: 06 Sep 2004 * /

#ifndef list_h # define list_h

#include #include

Struct ListNode {struct listnode * next; int data;};

Struct List {structure; structure * head; struct listNode * tail; int count;};

Void list_init (struct list *); void list_insert (struct list *, struct listNode *); int list_delete (struct list *);

#ENDIF

------------------------------------------

List.c

------------------------------------------

/ * list.c ** Copyright 2004 Coon Xu. ** Author: coon Xu ** Date: 06 Sep 2004 * / # include "list.h"

void list_init (struct list * myroot) {myroot-> count = 0; myroot-> head = NULL; myroot-> tail = NULL;} void list_insert (struct list * myroot, struct listnode * mylistnode) {myroot-> count ; MyListNode-> next = null; if (myroot-> head == null) {myroot-> head = myListNode; myroot-> tail = myrootnode;} else {myroot-> tail-> next = myListNode; myroot-> tail = MYListNode;}}

int list_delete (struct list * myroot, struct listnode * mylistnode) {struct listnode * p_listnode = myroot-> head; struct listnode * pre_listnode; // myroot is empty if (p_listnode == NULL) {return 0;} if (p_listnode = = MyListNode) {myroot-> count -; // myroot HAS Only One node if (myroot-> tail == mylistNode) {myroot-> head = null; myroot-> tail = null;} else {myroot-> head = p_listnode-> next;} return 1;} while (p_listnode = mylistnode && p_listnode-> next = NULL!!) {pre_listnode = p_listnode; p_listnode = p_listnode-> next;} if (p_listnode == mylistnode) {pre_listnode-> Next = p_listnode-> next; myroot-> count -; return 1;} else {return 0;}}

------------------------------------------ Main.c

-------------------------------------------

#include #include #include "list.h"

转载请注明原文地址:https://www.9cbs.com/read-115100.html

New Post(0)