Basic operation of doubly linked list

Source: Internet
Author: User

A doubly linked list is also called a doubly linked list, which is a list of two pointers in each data node, pointing directly to successive and direct precursors respectively. So, starting from any node in a doubly linked list, it is easy to access its predecessor and successor nodes.

In order to practice, I set sentinel node this time, sentinel node, I personally understand is our general sense of the head node (is a linked list of an additional node, the data field does not store any information), but the list is placed on both ends of a, so the image is called "Sentinel." So, as with the basic operation of a single-linked list, the effective node of the list should start from the second one.
The convenience of the sentinel node is that it does not require the first node of the linear table to be processed separately, such as inserting a node at a location, the general method is to find the previous node of the original node in that location, and then add the new node back, if there is no sentinel node, When the insertion position is the first position of the linear table, it is necessary to consider that the original node in the position has no precursor node, and if there is a sentinel node, the nodes in each position of the linear table have a precursor node, so they can be processed uniformly. (Note: The Sentinel node is not in the linear table at all, so although it has no precursor, the preceding sentence is not contradictory)

On the code:

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _node {void *data;
	struct _node *prior;
struct _node *next;


}node;
	typedef struct DOUBLELINKLIST {NODE *head;
	NODE *last;
int length;


}dllist;												                Dllist *createlist ();					  Create a doubly-linked list int appendlist (dllist *l, void *data, int size);   Append the node int insertlist (dllist *l, int pos, void *data, int size) after the linked list;      Insert int Deletenode (dllist *l, int pos, void *e, int size) before the element in POS position;				Delete the node where POS is located, and return the deleted node data void Printlist (Dllist *l, Void (*printnode) (void *)) with E;                                  print void Clearlist (Dllist *l);                               Emptying the list void Destorylist (Dllist **l);


Destroy linked list Double d[5] = {10.23, 34.23, 54.65, 122, 35.5};
  void Printdata (void *data) {double *d = (double *) data;
printf ("D =%lf\n", *d);
  } int main () {int i;
  Double E;
  Dllist *list = CreateList (); for (i = 0; i < 4; ++i) {appendlist (list, &amP;d[i], sizeof (d[i));
  } printlist (List,printdata);

  printf ("\ n");
  Insertlist (list, 2, &d[4], sizeof (d[4]));
  Printlist (List,printdata);

  printf ("\ n");
  Deletenode (list, 1, &e, sizeof (double));
  printf ("The deleted element is:");

  printf ("%lf\n\n", e);
	Destorylist (&list);
  if (NULL = = list) printf ("list is null\n");
 	else printf ("List not null\n");
return 0;
	}//Create doubly linked list Dllist *createlist () {dllist *l = (dllist *) malloc (sizeof (dllist));
	
	if (NULL = = L) exit (0);					L->head = (node *) malloc (sizeof (node));
	Set sentinel node to facilitate double-linked list operation if (NULL = = l->head) exit (0);

	memset (l->head, 0, sizeof (NODE));
	L->last = (node *) malloc (sizeof (node));
	if (NULL = = l->last) exit (0);

	memset (l->last, 0, sizeof (NODE));
	L->head->next = l->last;
	L->last->prior = l->head;
	l->length = 0;
return l;

	}//Append node int appendlist (dllist *l, void *data, int size) after list {node *p = NULL; if (NULL = = L | |

	NULL = = data) return 0; p = (NODE *) malloc (sizeof (NODE));

	if (NULL = = p) return 0;
	P->data = malloc (size);
		if (NULL = = P->data) {free (p);
	return 0;
	
	} memcpy (P->data, data, size);
	P->next = l->last;
	P->prior = l->last->prior;
	L->last->prior->next = p;
	L->last->prior = p;
	l->length++;
return 1;
  }//Insert int insertlist (dllist *l, int pos, void *data, int size) before the element in POS position {NODE *p = null, *q = NULL;

  int i = 0; if (NULL = = L | | NULL = = Data | | POS < 1 | |

  POS > L->length) return 0;
  p = (node *) malloc (sizeof (node));

  if (NULL = = p) return 0;
  P->data = malloc (size);
    if (NULL = = P->data) {free (p);
  return 0;

  } memcpy (P->data, data, size);
  Q = l->head;
    while (I < pos-1) {q = q->next;
  ++i;
  } P->next = q->next;
  Q->next = p;
  P->prior = q;
  P->next->prior = p;
  l->length++;
return 1; }//delete the node at POS, and return the deleted node data with e int deletenode (dllist *l, int pos, void *e, int size) {NODE *p = null, *q = NULL;

  int i = 0;
  if (NULL = = L | | pos < 1 | | pos > L->length) return 0;
  p = l->head;
  while (I < pos-1) {//This iterates over the previous node in the POS position, and can also traverse directly to the node at the POS location, removing p = p->next; by prior operation
  } q = p->next;
  
  memcpy (E, q->data, size);
  P->next = q->next;
  Q->next->prior = p;
  Free (q->data);
  Free (q);
  l->length--;
return 1;
	}//print void Printlist (Dllist *l, Void (*printnode) (void *)) {NODE *p = NULL;

	int i = 0; if (NULL = = L | |

	NULL = = Printnode) return;
	p = l->head;
		while (I < l->length) {p = p->next;
		Printnode (P->data);
	++i;

  }}//emptying the list void Clearlist (Dllist *l) {NODE *p = NULL;
  if (NULL = = L->head->next) return;
  p = l->head->next;
  L->head->next = p->next;
  Free (p);                               if (l->head->next! = l->last)//If the next node of P is not a tail node, prevent the deletion of the tail node clearlist (l); Here with recursion}//Destroy linked list
void Destorylist (Dllist **l) {dllist *p = *l;

  if (NULL = = p) return;
  Clearlist (P);            Free (p->head);
  Release Sentinel node free (p->last);
  Free (p);
*l = NULL; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.