Chapter 10 introduction to algorithms Data Structure-two-way linked list; Chapter 10 Introduction

Source: Internet
Author: User

Chapter 10 introduction to algorithms Data Structure-two-way linked list; Chapter 10 Introduction

The concept of reading is quite vague. I don't know what methods are required for a double-stranded table. In fact, there should be many methods for this structure, and there is no specific standard.

However, I can't do it without training. I have to handle all kinds of errors and overflow.

# Include <iostream>


Using namespace std;
Template <class T> struct Node
{
T value;
Node <T> * pre;
Node <T> * next;


};
Template <class T> class Flist
{
Private:
Node <T> * front;
Node <T> * end; // each time a function is written, the variable in the traversal class needs to be maintained
Int count;

Public:
Flist (); // defaut constructor
~ Flist ();
Flist (const Flist & C_list); // This is supported. If no write support = is supported, you can write an empty inline function to empty this behavior. Otherwise, the compiler will automatically generate related operations.
Inline void Deeply_Copy (const Flist & Copylist );
Bool Isempty () const;
Int Listsize () const;
T & Listfront () const;
T & Listend () const; // The return reference. Can I change the key value of the node at the beginning and end?
Void push_front (t n );
Void push_back (t n );
Void del_front ();
Void del_back ();
// Node <T> * Listfind (T x); // search nodes with key value x
T ShowKey (int n); // output the nth Value
 
};
Template <class T> Flist <T>: Flist (): count (0), front (0), end (0) // The initialization sequence is irrelevant to the declaration in the class.
{

}
Template <class T> Flist <T> ::~ Flist ()
{
Node <T> * temp;
While (front! = 0)
{
Temp = front;
Delete temp;
Front = front-> next;
}
Temp = NULL;
}
Template <class T> bool Flist <T>: Isempty () const
{
Return 0 = count;
}
Template <class T> int Flist <T>: Listsize () const {
Return count;
}
Template <class T> Flist <T>: Flist (const Flist & C_list)
{
Deeply_Copy (C_list );

}
Template <class T> T & Flist <T>: Listfront () const
{
Return front-> value;
}
Template <class T> T & Flist <T>: Listend () const
{
Return end-> value;
}
Template <class T> void Flist <T>: push_front (t n)
{
Node <T> * p = new Node <T>;
P-> value = N;
P-> next = front;
If (front! = 0)
Front-> pre = p;
Front = p;
If (end = 0)
End = p;
P-> pre = 0;
Count ++;
}
Template <class T> void Flist <T>: push_back (t n)
{
Node <T> * p = new Node <T>;
P-> value = N;
P-> pre = end;
If (end! = 0) // At the beginning, the crash occurs because the end is not initialized.
End-> next = p;
End = p;
End-> next = 0;
Count ++;
}
Template <class T> void Flist <T>: del_front ()
{
Node <T> * temp = front;
Front = front-> next;
Count --;
Front-> pre = 0;
Delete temp;
}
Template <class T> void Flist <T>: del_back ()
{
Node <T> * temp = end;
End = end-> pre;
End-> next = 0;
Count --;
Delete temp;
}

Template <class T> T Flist <T>: ShowKey (int n)
{

/*
If (front = 0)
{
Cout <"there is no element is the list..." <endl;
Return; // unsolved. How can I prompt when the size is 0?
}*/
If (front! = 0) & (n <= count) & (n> = 1 ))
{
Node <T> * temp = front;
While (-- n) // here, if while (n --) overflows and crashes, n is used and then subtracted. Here we use judgment. Don't think that we can use it before and after brackets.
{
Temp = temp-> next; // if n is greater than the size, it will also overflow.
}
Return temp-> value;
// Here temp points to one and other pointers point to the node. Do not delete
}

}
Template <class T> void Flist <T>: Deeply_Copy (const Flist & Copylist)
{
Front = end = 0;
If (Copylist. front = 0)
Return; // The end function is used here.
Front = new Node <T>;
Node <T> * cp = Copylist. front;
Node <T> * np = front;
Np-> value = cp-> value;
Count = 1;
Np-> pre = 0;
Cp = cp-> next;
While (cp! = 0)
{
Np-> next = new Node <T>;
Count ++;
(Np-> next)-> pre = np;
Np = np-> next;
Np-> value = cp-> value;
Cp = cp-> next;
}
End = np;
}
Int main ()
{
Flist <int> listf;
Int a [5] = {1, 2, 3, 4, 5 };
For (int I = 0; I <5; I ++)
Listf. push_front (a [I]);
Cout <"lisrfsize:" <listf. Listsize () <endl;
For (int I = 0; I <5; I ++)
Listf. push_back (a [I]);
Cout <"lisrfsize:" <listf. Listsize () <endl;
Cout <"listf is empty? : "<Listf. Isempty () <endl;
Flist <int> listf2 (listf );
Cout <"Listf2size:" <listf2.Listsize () <endl;
Listf2.del _ front ();
Listf2.del _ back ();
Cout <"Listf2 front:" <listf2.Listfront () <endl;
Cout <"Listf2 end:" <listf2.Listend () <endl;
Cout <"Listf2 size:" <listf2.Listsize () <endl;
For (int I = 1; I <= listf2.Listsize (); I ++)
Cout <listf2.ShowKey (I) <endl;


Return 0;
}


Data structure two-way linked list

This is my previous job. It should be okay if you make some changes accordingly. If you don't, contact me again. I am online.
Include "stdio. h"
# Include <malloc. h>

Typedef char ElemType;

Typedef struct LNode
{ElemType data;
Struct LNode * next;
} LinkList;

Void CreatListF (LinkList * & L, ElemType a [], int n) // create a table using the header Insertion Method
{
LinkList * s; int I;
L = (LinkList *) malloc (sizeof (LinkList ));
L-> next = L;
For (I = 0; I <n; I ++)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = a [I];
S-> next = L-> next;
L-> next = s;
}
}

Void CreateListR (LinkList * & L, ElemType a [], int n) // create a table by means of end insertion
{
LinkList * s, * r; int I;
L = (LinkList *) malloc (sizeof (LinkList ));
R = L;
For (I = 0; I <n; I ++)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = a [I];
R-> next = s;
R = s;
}
R-> next = L;
}

Void InitList (LinkList * & L) // initialize the linear table
{
L = (LinkList *) malloc (sizeof (LinkList ));
L-> next = L;
}

Void DestroyList (LinkList * & L) // destroy the linear table
{
LinkList * p = L-> next, * q = p-> next;
While (q! = L)
{
Free (p );
P = q;
Q = p-> next;
}
Free (p );
}

Int ListEmpty (LinkList * L) // determines whether the linear table is empty.
{
Return (L-> next = L );
}

Int ListLength (LinkList * L) // evaluate the length of a linear table
{
LinkList * p = L; int n = 0;
While (p-> next! = L)
{
N ++; p = p-> next;
}
Return (n );
}

Void DispList (LinkList * L) // output linear table
{
LinkList * p = L-> next;
While (p! = L)
{
Printf ("% c", p-> data );
P = p-> next;
}
}

Int GetElem (LinkList * L, int I, ElemType & e) // evaluate the value of a data element in a linear table.
{
Int j = 1;
LinkList * p = L-> next;
While (j <I & p! = L)
{
J ++;... the remaining full text>

The problem of two-way linked list of c ++ Data Structure

You are wrong .. I changed it for you. You can take a closer look .. Remember to add extra points. Haha
# Include "stdio. h"
# Include <iostream. h>
# Define elemtype int
Class dulink
{
Public:
Elemtype data;
Dulink * next, * prior;
};
Class dulinklist
{
Protected:
Dulink * head;
Public:
// Create a two-way linked list with the leading node by using the header Insertion Method
Dulink * hcreat ()
{
Dulink * p, * s;
Elemtype I;
Cout <"enter multiple node values (separated by spaces). If the value is 0, the algorithm ends ";
Cin> I;
P = new dulink;
P-> prior = NULL;
P-> next = NULL;
While (I) // when the input data is not 0, create a bidirectional linked list cyclically
{
S = new dulink;
S-> data = I;
S-> next = p-> next;
If (s-> next)
S-> next-> prior = s;
S-> prior = p;
P-> next = s;
Cin> I;
}
Return p;
}
// Outputs a bidirectional list
Void print (dulink * head)
{
Dulink * p;
P = head-> next;
While (p! = NULL)
{
Cout <p-> data <"";
P = p-> next;
}

}
};
Void main ()
{Dulink * p;
Dulinklist h;
P = h. hcreat ();
H. print (p );
}

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.