Double linked list initialization, building, inserting, locating, deleting

Source: Internet
Author: User

Double linked list initialization, build, insert, find, delete. Author:wang Yong////date:2010.8.19//////////////////////////////////////////////#include <stdio.h> Include <stdlib.h> typedef int ELEMTYPE; Defines a double link table node type typedef struct Node {elemtype data struct node *prior;//point to the precursor node Point struct node *next; Point to successor node}node, *dlinklist; The establishment of double linked list, the use of tail interpolation method to establish a double linked list dlinklist Dlinklistcreatt () {Node *l,*p,*r; L = (node *) malloc (sizeof (node));//Request Header node L->next = NULL; R = L; R->next = NULL; R is a pointer to a terminal node Elemtype x; while (scanf ("%d", &x)!= EOF)//input double linked list elements, establish a doubly linked list {p = (node *) malloc (sizeof (node)); p->data = x; p->next = R->ne xt R->next = p; r = P; } r->next = NULL; return L; ///////////////////////////////////////////Double linked list lookup, find the position of element x Dlinklistfind (dlinklist l,elemtype x) {dlinklist p; P for retrieval, p = l->next; int i = 1; while (P!= NULL && p->data!)= x)//looking for an element with a value of x * * Note that the condition of the loop cannot be written against {//cause, when p = = NULL time P->data will error ++i;/for (i = 1, p = l->next; p = p->next, i++) {//if (P->data = = x) break;} p = p->next; if (p = = NULL)//If not found returns 0 return 0; else return i; If you find an insert that returns the i}///////////////////////////////////////////Double linked list, insert the element dlinklist the value x in the first position of the double linked list Dlinklistinsert ( Dlinklist l,int I,elemtype x) {dlinklist p,s; for node p =//s to be inserted look for int l->next; starting from the first node position; for (tempi = Tempi 1;tempi T I-1; tempi++) p = p->next; s = (node *) malloc (sizeof (node)); S->data = x; Assign X to the data field of s S->next = p->next; Inserting a node into p->next->prior = s; S->prior = p; P->next = s; return L; ////////////////////////////////////////////////Double linked list Delete, delete the double linked list of the first I node Dlinklist dlinklistdelete (dlinklist L,int i) { int tempi = 1; Dlinklist p; P is the lookup node. p = l->next; while ((tempi++)!= i && p!= NULL) {p = p->next;} if (p = = null)//check if the position in the double linked list is printf ("location is illegal.") /n "); else if (P->next = NULL)//Last node special handling, reason last node P->nexT prior {p->prior->next = NULL; free (p);} else//delete operation {p->prior->next = p->next; p->next->prior = p->prior; Free (p); }///////////////////////////////////////////int Main () {dlinklist list,start; list = Dlinklistcreatt (); for (start = l ist->next; Start!= NULL; start = Start->next) printf ("%d", start->data); printf ("n"); int i; Elemtype x; printf ("Enter the value of the element you want to find:"); scanf ("%d", &x); i = Dlinklistfind (list,x); if (i) printf ("Position in the list is:%d/n", i); else printf ("There is no this element.") /n "); printf ("Please enter insertion position:"); scanf ("%d", &i); printf ("Please enter the value of the inserted element:"); scanf ("%d", &x); Dlinklistinsert (LIST,I,X); for (start = list->next; start!= NULL; start = start->next) printf ("%d", start->data); printf ("n"); printf ("Please enter a location to delete:"); scanf ("%d", &i); Dlinklistdelete (List,i); for (start = list->next; start!= NULL; start = start->next) printf ("%d", start->data); printf ("n"); return 0; }

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.