Objective: To understand the definition and implementation of the linked list, learn how to use it, understand the definition and implementation of the stack class, learn how to use it, and understand the definition and implementation of the queue, learn how to use it. Learn how to use the C ++ standard template library STL.
Content
Program:
// Lab9_2.cpp
// Configure list. h
# Include <malloc. h>
# Ifndef extends list_class
# Define list_class
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
# Ifndef null
Const int null = 0;
# Endif // null
Template <class T>
Class lnode
{PRIVATE:
Public:
T num;
Lnode <t> * _ next;
};
Template <class T>
Class linklist
{PRIVATE:
Public:
Lnode <t> * _ head;
Int _ Len;
Lnode <t> * initlist (void) // creates an empty linked list function for the leading node.
{Lnode <t> * h;
H = (lnode <t> *) malloc (sizeof (lnode <t> ));
H-> num = 0; // The exponent value of the header node is-1.
H-> _ next = NULL;
Return h ;}
Void creatpolyn (linklist <t> P, int M, t j); // create a linked list };
Template <class T>
Void linklist <t >:: creatpolyn (linklist <t> P, int M, t j) // create a linked list
{Lnode <t> * H, * E, * q;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!
H = P. initlist (); // create an empty linked list of the leading Node
Q = E = h;
P. _ head = h;
P. _ Len = m;
For (INT I = 1; I <= m; ++ I)
{Cout <"Enter the number of <I <" items: "; // number of inputs
Cin> J;
E = (lnode <t> *) malloc (sizeof (lnode <t> ));
E-> num = J; // Insert the new node to the linked list.
E-> _ next = NULL;
Q-> _ next = E;
Q = Q-> _ next ;}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!
H = P. _ head; // display the value in the constructed linked list Node
E = H-> _ next;
Int I1 = 1;
While (E)
{Cout <"nodes in the linked list" <I1 <"the stored value is" <Endl;
Cout <e-> num <Endl;
M --;
E = e-> _ next;
I1 ++ ;}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!
E = q = P. _ head; // uses loops to release the memory space of each node in the linked list.
Int I2 = 0;
While (P. _ Len + 1)
{Q = Q-> _ next;
Cout <"release" <I2 <"node! "<Endl;
Free (E); // release the memory space of nodes in the linked list
E = Q;
P. _ Len --;
I2 ++;} // release the memory space of nodes in the linked list
} # Endif
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////
// Queue. h
# Ifndef queue_class
# Define queue_class
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
# Ifndef null
Const int null = 0;
# Endif // null
# Include "listing list. H"
Template <class T>
Class queue
{PRIVATE:
T J0;
Int M0;
Public:
Void creatqueue (int m, t j) // create
{J0 = J;
M0 = m;
Linklist <t> L;
L. creatpolyn (L, M0, J0 );
};
// Implementation of member functions
# Endif
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////
// Lab9_2
# Include "queue. H"
Void main ()
{Int N;
N1: cout <"======================================== = "<Endl;
Cout <"** input menu type **" <Endl;
Cout <"1.int type 2. Float Type" <Endl <"3. Double Type 4. Char type" <Endl;
Cout <"======================================== = "<Endl;
Cout <"select your input type :";
Int K1; CIN> K1;
Cout <"Enter the number you want to enter :";
Cin> N;
Switch (K1)
{
Case 1: int J1;
Queue <int> L1;
L1.creatqueue (n, J1 );
Cout <"======================================== = "<Endl;
Cout <"your operation options include:" <Endl <"1. Continue operation 2. End operation" <Endl;
Cout <"======================================== = "<Endl;
Int M1; cout <"Enter your choice:"; CIN> m1;
If (M1 = 1) goto N1;
Else break;
Case 2: Float J2;
Queue <float> L2;
L2.creatqueue (n, J2 );
Cout <"======================================== = "<Endl;
Cout <"your operation options include:" <Endl <"1. Continue operation 2. End operation" <Endl;
Cout <"======================================== = "<Endl;
Int m2; cout <"Enter your choice :";
Cin> m2;
If (M2 = 1) goto N1;
Else break;
Case 3: Double J3;
Queue <double> l3;
L3.creatqueue (n, J3 );
Cout <"======================================== = "<Endl;
Cout <"your operation options include:" <Endl <"1. Continue operation 2. End operation" <Endl;
Cout <"======================================== = "<Endl;
Int m3; cout <"Enter your choice :";
Cin> m3;
If (M3 = 1) goto N1;
LSE break;
Case 4: Char J4;
Queue <char> L4;
L4.creatqueue (n, J4 );
Cout <"======================================== = "<Endl;
Cout <"your operation options include:" <Endl <"1. Continue 2. End operation" <Endl;
Cout <"======================================== = "<Endl;
Int M4; cout <"Enter your choice :";
Cin> M4;
If (M4 = 1) goto N1;
Else break;
Default:
Cout <"your input is illegal! Please select again! "<Endl;
Goto N1;
Break;
}
}
// Lab9_3.cpp
# Include <iostream>
# Include <deque>
Using namespace STD;
Int main ()
{Int N; cout <"Enter the number you want to enter :";
Cin> N;
Deque <int> D; // constructs a queue to store integers.
Int key, item;
For (INT I = 0; I <n; I ++) // input n Integers to insert them to the queue in sequence.
{Cout <"Enter the" <I + 1 <"value" <Endl;
Cin> item;
D. push_back (item); // Insert the function value following the previous number
// D. push_front (item); // implement the function value before inserting the previous number}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Cout <"the input queue is:"; // The output queue
Deque <int >:: iterator P = D. Begin ();
While (P! = D. End () // output data until the end of the queue
{Cout <* P <"";
P ++ ;}
Cout <Endl <"queue length is" <D. Size () <Endl; // output queue length
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
D. pop_front (); // deletes the first value of the queue.
// D. pop_back (); // Delete the last value of the queue
Cout <"use pop_front () to delete the first number of Queues:"; // output queue
Deque <int >:: iterator p1 = D. Begin ();
While (P1! = D. End () // output data until the end of the queue
{
Cout <* P1 <"";
P1 ++;
}
Cout <Endl <"queue length is" <D. Size () <Endl; // output queue length
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Cout <"D. Empty () display is" <D. Empty () <Endl; // implement access ???
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Return 0;
}