Experiment 2 single-chain table implementation

Source: Internet
Author: User

Experiment 2 single-chain table implementation

I. Tutorial Purpose

1. Master the link storage structure of the linear table;

2. verify the implementation of a single-chain table and its basic operations;

3. Further master the basic methods for Program Implementation of data structures and algorithms.

Ii. experiment content.

1. Based on the courseware definition of a single-chain table, a single-chain table with the leading node is implemented;

2. Create a single-chain table with the header (or tail) method to take the lead node;

3. implement the local reverse () method of the basic linear table, that is, to arrange the data elements in the basic linear table in the exchange position. The new sequence is exactly the opposite of the original sequence, for example, the original sequence "abcdef ", after inversion, it becomes "fedcba ".

[Prompt]: disconnects a single-chain table into two single-chain tables. One of them is directed by the first pointer, and only one header node is contained in the table. The other single-chain table is directed by the P pointer, create a single-linked table with no leading nodes. Then, extract the first element from the P Single-linked list and insert it into the header of the first linked list until P points to null.

4. Use a single-chain table to store the student table. The test data is as follows:

5. A. Create an empty table and check whether the table is empty.

B. insert data in sequence 001
Zhang San 2005 70

002
Li Si 2004 65

C. Insert 003 after Michael Jacob
Wang Wu 2005 80

James inserted 004 before
Ma 6 2006 90

Insert 005 after Li Si
Huang Rong 2004 85

D. Delete the first record.

Delete the last record

E. Check whether Huang Rong is in the table. In this case, print "existing records: 005
Huang Rong 2004 85 "; otherwise," nonexistent "is printed"


Check whether guo qing is in the table and print the result.

F. Number of printed table records,

G. Print the entire table.

Iii. Lab Environment

1. PC, Windows XP operating system

2. VC ++ 6.0 software

4. Answer questions

1. What is the procedure for inserting a node P into a double-stranded table?

If (D = 0 ){

Newnode-> llink = Current-> llink;

Current-> llink-> rlink = newnode;

Newnode-> llink-> rlink = newnode;

Newnode-> rlink = current;

}

Else {

Newnode-> rlink = Current-> rlink;

Current-> rlink = newnode;

Newnode-> rlink-> llink = newnode;

Newnode-> llink = current;

 

}

 

2. What are the steps for deleting a node P in a double-linked table?

If (current = NULL) returnfalse;

Current-> rlink-> llink = Current-> llink;

Current-> llink-> rlink = Current-> rlink;

X = Current-> data; deletecurrent;

 

 

 

 

 

# Include <iostream>

Using namespace STD;

Template <class T>

Structlinknode {

Tdata;

Linknode <t> * link;

Linknode (linknode <t> * PTR = NULL) {link = PTR ;}

Linknode (constt & item, linknode <t> * PTR = NULL) {DATA = item; link = PTR ;}

};

Template <class T>

Class list {

Public:

List () {First = new linknode <t> ;}

List (const T & X) {First = newlinknode <t> (x );}

List (list <t> & L );

~ List () {makeempty ();}

Void makeempty ();

Int length () const;

Linknode <t> * gethead () const {returnfirst ;}

Linknode <t> * seartch (t x );

Linknode <t> * locate (int I );

Bool getdata (int I, T & X) const;

Void setdata (int I, T & X );

Void insert (T endtag );

Bool remove (int I, T & X );

Bool isempty () const {returnfirst-> link = NULL? True: false ;}

Bool isfull () const {return false ;}

Void input ();

Void output ();

Linknode <t> * reverse ();

Void move ();

Protected:

Linknode <t> * first;

};

Template <class T>

List <t>: List (list <t> & L)

{

Tvalue;

Linknode <t> * srcptr = L. gethead ();

Linknode <t> * destptr = first = new linknode <t>;

While (srcptr-> link! = NULL)

{

Value = srcptr-> link-> data;

Destptr-> link = newlinknode <t> (value );

Destptr = destptr-> link;

Srcptr = srcptr-> link;

}

Destptr-> link = NULL;

}

 

Template <class T>

Voidlist <t >:: makeempty ()

{

Linknode <t> * q;

While (first-> link! = NULL)

{

Q = first-> link;

First-> link = Q-> link;

Delete Q;

}

}

Template <class T>

Intlist <t>: length () const

{

Linknode <t> * m = first-> link;

Int COUNT = 0;

While (P! = NULL)

{M = m-> link; count ++ ;}

Return count;

}

Template <class T>

Linknode <t> * List <t>: seartch (t x)

{

Linknode <t> * Current = first-> link;

While (currnet! = NULL)

If (current-> DATA = x) break;

Else current = Current-> link;

Return Current;

}

Template <class T>

Linknode <t> * List <t>: locate (int I)

{

If (I <0) returnnull;

Linknode <t> * Current = first; int K = 0;

While (current! = NULL & K <I)

{Current = Current-> link; k ++ ;}

Returncurrent;

}

Template <class T>

Voidlist <t >:: setdata (int I, T & X)

{

If (I <= 0) return;

Linknode <t> * Current = locate (I );

If (current = NULL) return;

Else current-> DATA = x;

}

Template <class T>

Voidlist <t>: insert (T endtag)

{

Linknode <t> * newnode, * last;

T val;

Makeempty ();

Cin> val;

Last = first;

While (Val! = Endtag)

{

Newnode = new linknode <t> (VAL );

If (newnode = NULL) {cerr <"wrong" <Endl; exit (1 );}

Last-> link = newnode; last = newnode;

Cin> val;

}

Last-> link = NULL;

}

Template <class T>

Linknode <t> * List <t>: reverse ()

{

/* Linknode <t> * newnode; t val;

Makeempty ();

Cin> val;

Linknode <t> * P;

P = first-> link;

 

While (Val! = '#')

{

Newnode = new linknode <t> (VAL );

If (newnode = NULL)

{Cerr <"wrong" <Endl; exit (1 );}

Newnode-> link = first-> link;

First-> link = newnode;

Cin> val;

}

*/

Linknode <t> * P, * V1, * V2;

V2 = first;

V1 = NULL;

While (V2! = NULL ){

P = v2-> link;

V2-> link = V1;

V1 = V2;

V2 = P;

}

While (V1! = NULL)

{

Cout <V1-> data;

V1 = V1-> link;

}

Return V1;

 

 

}

Template <class T>

Voidlist <t>: output ()

{

Linknode <t> * Current = first-> link;

While (current! = NULL)

{

Cout <Current-> data <Endl;

Current = Current-> link;

}

}

Voidmain ()

{

List <char> L;

L. insert ('#');

L. Output ();

L. Reverse ();

L. Output ();

 

}

 

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.