Search for common data structures in iOS system

⛓ two-way linked list

Function: Add and delete the bidirectional linked list.

Header file: #include

Platform: POSIX

Function signature:

// Insert a list element element after pred. If pred is NULL, element is the head of the list. void insque(void *element, void *pred); // Remove the element from the list. void remque(void *element);Copy the code

Parameters:

Element :[in] List element to be added or removed.

Pred :[in] The list inserts the prefix element of element element.

Description:

The system does not specify the data structure of the linked list, but requires that the first two data members of the linked list element structure must refer to the latter element and the former element respectively. Here is the list element structure template:

struct que_elem { struct que_elem *next; struct que_elem *prev; // Other custom data members};Copy the code

The above two functions are only responsible for inserting and removing elements from the list. Maintaining the number of headers or lists, as well as memory allocation and destruction of linked list elements, is our responsibility.

Sample code:

Typedef student {struct student *next; struct student *prev; int age; char *name; }student_t; void traverse(student_t *head) {while(head->next ! = NULL) {printf("student's age = %d, name=%s\n",head->age, head->name);
         head = head->next;
    }
}

void main()
{
     student_t *student1 = malloc(sizeof(student_t));
     student1->age = 10;
     student1->name = "Alex";

     student_t *student2 = malloc(sizeof(student_t));
     student2->age = 20;
     student2->name = "Bob"; //student1 is the head of the linked list insque(student1, NULL); // insert student2 after student1 insque(student2, student1); // traverse the linked list (student1); // Delete student1 remque(student1); free(student1); // Delete student2 remque(student2); free(student2); }Copy the code