Data structure (C implementation) ------- two-way linked list, data structure -------

Source: Internet
Author: User
Tags define null

Data structure (C implementation) ------- two-way linked list, data structure -------

Each node in a two-way linked list contains two pointer fields. one pointer field stores the storage address of its successor node, and the other pointer field stores the storage address of its predecessor node.

Type description of the two-way linked list node:

// Type description of two-way linked list typedef int ElemType; typedef struct node {ElemType data; struct node * prior, * next;} DuLNode, * DuLinkList;

The prior domain stores the storage address of its precursor node, and the next domain stores the storage address of its successor node.

Two-way linked list has two features: one is that you can search for a node from two directions, which makes some operations (such as insertion and deletion) of the linked list easier; second, the whole two-way linked list can be traversed no matter whether the front or back chain is used.


The operations of a two-way linked list are basically the same as those of a single-chain table;

1. Create a two-way linked list Create_DLinkListF (int n) of the lead node by using the header Insertion Method)

// Create a two-way linked list DuLinkList Create_DLinkListF (int n) {DuLinkList L, p; int I = n-1; ElemType x; // create a new header node L = (DuLinkList) malloc (sizeof (DuLNode); L-> prior = NULL; L-> next = NULL; // Add the first node scanf ("% d", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; l-> next = p; p-> prior = L; p-> next = NULL; // Add other nodes while (I> 0) {scanf ("% d ", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; p-> next = L-> next; l-> next-> prior = p; p-> prior = L; L-> next = p; I --;} return L ;}

2. Create a two-way linked list Create_DLinkListR (int n) with the lead node by means of the End Plug)

// Create a two-way linked list DuLinkList Create_DLinkListR (int n) {DuLinkList L, p, lastNode; int I = n-1; ElemType x; // create a new header node L = (DuLinkList) malloc (sizeof (DuLNode); L-> prior = NULL; L-> next = NULL; // Add the first node scanf ("% d", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; l-> next = p; p-> prior = L; p-> next = NULL; lastNode = p; // Add other nodes while (I> 0) {scanf ("% d", & x); p = (DuLinkList) malloc (sizeof (DuLNode); p-> data = x; lastNode-> next = p; p-> prior = lastNode; p-> next = NULL; lastNode = p; I --;} return L ;}
3. Insert the new node Insert_DLinkListBefore (DuLinkList p, ElemType x) before the specified node)

// Insert the new node void Insert_DLinkListBefore (DuLinkList p, ElemType x) {DuLinkList newNode before the specified node; // determine the legality of the node before node p: if (p-> prior = NULL) printf ("the node is invalid and cannot be inserted before the node \ n"); else {newNode = (DuLinkList) malloc (sizeof (DuLNode); newNode-> data = x; newNode-> next = p; p-> prior-> next = newNode; newNode-> prior = p-> prior; p-> prior = newNode ;}}
4. Insert the new node Insert_DLinkListAfter (DuLinkList p, ElemType x) after the specified node)

// Insert the new void Insert_DLinkListAfter (DuLinkList p, ElemType x) {DuLinkList newNode; newNode = (DuLinkList) malloc (sizeof (DuLNode) after the specified node )); newNode-> data = x; // if (p-> next = NULL) {p-> next = newNode when the insertion position is after the last node; newNode-> prior = p; newNode-> next = NULL;} else {newNode-> next = p-> next; p-> next-> prior = newNode; p-> next = newNode; newNode-> prior = p ;}}
5. Delete the specified node Delete_DLinkList (DuLinkList p)

// Delete the specified node void Delete_DLinkList (DuLinkList p) {// if the last element is deleted, if (p-> next = NULL) p-> prior-> next = NULL; else {p-> prior-> next = p-> next; p-> next-> prior = p-> prior ;} free (p );}
6. Two-way linked list Print_DLinkListN (DuLinkList L)

// The back-link outputs the two-way linked list void Print_DLinkListN (DuLinkList p) {while (p! = NULL) {printf ("% d \ t", p-> data); p = p-> next;} printf ("\ n ");}
7. Print_DLinkListP (DuLinkList p)

// The frontend chain outputs the two-way linked list void Print_DLinkListP (DuLinkList p) {while (p! = NULL) {printf ("% d \ t", p-> data); p = p-prior;} printf ("\ n ");}

As for other operations of a two-way linked list, such as positioning, similar to the operations of a single-chain table, we will not repeat them here.








Use the two-way linked list in <Data Structure> as the data structure and combine the basic knowledge of C language to compile an address book management system.

# Include <stdio. h>
# Include <stdlib. h>
# Define Null 0
# Define OverFlow-1
# Define OK 0
# Define Error-2
Typedef int ElemType;
Typedef struct node
{
ElemType data;
Struct node * next;
} Node, * LinkList;
Void Init_LinkList (LinkList * Head_pointer)
{// Linear table Initialization
* Head_pointer = Null;
}
Int Insert_First (LinkList * Head_pointer, ElemType x)
{// Use the header insertion method to create a linear table
Node * p;
P = (Node *) malloc (sizeof Node );
If (p = NULL)
Return OverFlow;
P-> data = x;
P-> next = * Head_pointer;
* Head_pointer = p;
Return OK;
}
LinkList Location_LinkList (LinkList Head, ElemType x)
{// Query whether the data of a node in the linked list exists
LinkList p;
P = Head;
While (p! = Null)
{
If (p-> data = x)
Break;
P = p-> next;
}
Return p;
}
Int Delete_LinkList (LinkList * Head_pointer, ElemType x)
{// Delete a node in the linked list
Node * p, * q;
P = * Head_pointer;
If (p-> data = x) // the header node is the element to be deleted.
{
* Head_pointer = (* Head_pointer)-> next;
Free (p );
Return OK;
}
Else
{
Q = p; p = p-> next;
While (p! = Null)
{
If (p-> data = x)
{
Q-> next = p-> next;
Free (p );
Return OK;
}
Q = p; p = p-> next;
}
}
Return Error;
}
Void Show_LinkList (LinkList Head)
{// Traverse elements in a linear table
LinkList p = Head;
Int I = 0;
Printf ("--- print linked list --- \ n ");
If (p = Null)
Printf ("empty table \ n ");
While (p! = Null)
{
Printf ("[% d]: % d \ t", I ++, p-> data );
P = p-> next;
}
}
Int Length_LinkList (LinkList Head)
{// Calculate the length of the linked list
LinkList p = Head;
Int sum = 0;
While (p! = Null)
{
Sum ++;
... The remaining full text>
 
Write an address book system in C language to collect knowledge about two-way linked lists of data structures

// Similar // # include "stdafx. h"
# Include <iostream. h>
# Include <string. h>

# Include <iomanip. h>
Class stu
{
Char name [20];
Double age, homephone, telphone;
Char sex;
Public:
Stu (){}
Stu (char n [20], char se, double ag, double ho, double te)
{
Strcpy (name, n );
Age = ag;
Homephone = ho;
Telphone = te;
}
Friend void main ();
}; Void main ()
{
Cout <"select the operation you need! "<Endl;
Cout <"Operation:" <endl;
Cout <"(0) address book entry" <endl;
Cout <"(1) Add personnel" <endl;
Cout <"(2) delete a person" <endl;
Cout <"(3) modify data" <endl;
Cout <"(4) display records" <endl;
Cout <"(5) Quit" <endl;
Cout <"to select related operations, enter the Arabic numerals in the brackets! "<Endl;
Stu * s [50];
Int I = 0;
Int j = 0;
Bool flag2 = 0;
Char p;
Do
{
Cin> p;
If (p> = '0' & p <= '5 '))
Flag2 = 1;
Else
Cout <"command error! Enter again: "<endl;
} While (flag2 = 0); switch (p)
{Case '0': // (0) Address Book Entry
{
Char name [20];
Double age, homephone, telphone;
Char sex, c;
Do {cout <"Enter name:" <endl;
Cin> name;
Cout <"Enter Gender:" <endl;
Cin> sex;
Cout <"Enter age:" <endl;
Cin> age;
Cout <"enter the home phone number:" <endl;
Cin> homephone;
Cout <"Enter the mobile phone number:" <endl;
Cin> telphone;
J ++;
S [I] = new stu (name, sex, age ...... remaining full text>

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.