First, the purpose of the experiment
To consolidate the storage methods and related operations of the data structure of linear tables, learn to solve specific problems by using the relevant knowledge of linear tables for specific applications.
Ii. contents of the experiment
Set up a list of the scores of n students, the size of n is determined by themselves, each student's performance information is determined by themselves, the implementation of data to the table to insert, delete, find and other operations. Outputs the results separately.
Third, the experimental steps
1, according to the experimental content, respectively, the definition of the data type used in the experimental procedure
Public:
DLL ();
DLL (T score[], int n); The parameter constructor
~dll (); destructor int length
();//Returns a single-linked list of lengths
void Insert (int i, T x);//insert operation, in position I insert element
T get (int i); Bitwise LOOKUP
int Locate (T x); Find
T Delete by value (int i); Delete operation
void print (); Traversal Operation
Private:
node<t> *first; Double-linked list of head pointers
int length; The length count of the chain
2. Algorithm expression of related operation
The set element in the code is float, with 8 elements. Defines the data type of the sequential table-the double-linked list class DLL, including INSERT, delete, find, output and other basic operations.
Insert operation: 1. P,s initialization of working pointer;
2. Find the I-1 node and make the working pointer P point to it;
3. If the search is unsuccessful, it indicates that the insertion position is unreasonable and the insertion position is illegal.
Otherwise, 3.1 generates a new node s with an element value of x;
3.2 Insert the new node after the node P (different from the single-linked list);
Delete operation: 1. Working pointer p initialization, accumulator count initialization;
2. Find the I-1 node and make the working pointer P point to it;
3. If p does not exist or the subsequent node of P does not exist, it is illegal to throw the insertion position;
Otherwise, the value of the deleted node and the deleted element are 3.1 temporary.
3.2 Pick the chain, the node P's successor node from the list off;
3.3 Releasing the deleted node
3.4 Returns the value of the deleted element
Find operations: (1) Bitwise lookup
1. Working pointer p initialization, accumulator count initialization;
2. From the beginning of the pointer to next field-by-node search down until a node, determine whether it is the first node.
3. If so, the search succeeds; otherwise the work pointer p moves back;
(2) Search by value
1. Working pointer p initialization, accumulator count initialization;
2. Compare the elements of a single linked list in turn. Returns the element ordinal if the lookup succeeds; otherwise, 0 indicates that the lookup failed;
Output operation: 1. Working pointer p initialization;
2. Repeat the following until P is empty:
2.1 The data field of the output node p;
2.2 Work node P move back
Because the node element type is indeterminate, the C + + template mechanism is used.
The source code is as follows:
#include <iostream> using namespace std;
Template <typename t> class Node {public:t data;
Node<t> *prior;
Node<t> *next;
};
Template <typename t> class DLL {Public:dll (); DLL (T score[], int n); The parameter constructor ~dll (); destructor int Length (); Returns the single-linked table length void insert (int i, T x); Insert operation, in position I insert element T get (int i); Bitwise lookup int Locate (T x); Find T Delete by value (int i); Delete operation void print (); Traverse Operation Private:node<t> *first; Double-linked list of head pointers int length;
The length count of the chain};
Template <typename t> Dll<t>::D ll (T score[], int n) {length=0;
First = new node<t>;
First->next = NULL;
First->prior = NULL;
for (int i = 0; i<n; i++) {node<t> *s = new node<t>;
S->data = Score[i];
S->next = first->next;
First->next = s; }} template <typename t> Dll<t>::~dll () {while (First->next!=first->prior) {//temporary pointer, storing the upcoming
The pointer to the freed node node<t> *temp = First; Off-Chain FirSt->prior->next = first->next;
First->next-Prior = first->prior;
Head pointer moves back first = first->next;
Free memory Delete temp;
} delete first;
} template<typename t> int dll<t>::length () {node<t> *p; int count;
p=first->next;
count=0;
while (p!=null) {p=p->next;
count++;
} return length;
} template <typename t> void dll<t>::insert (int i,t x) {node<t>*p,*s;int count;
P=first;
count=0;
while (p!=null&&count<i-1) {p=p->next;
count++;
} if (p==null) throw "position";
else {s=new node<t>;
s->data=x;
s->next=p->next;
p->next=s;
}} template <typename t> T dll<t>::get (int i) {node<t> *p;int count; count = 1;
p = first->next; while (P! = null&&count<i) {p = p->next;
count++;
} if (p = = NULL) throw "location illegal";
else return p->data;
} template <typename t> int dll<t>::locate (T x) {node<t> *p; int count; p = first->next;
Count = 1;
while (p!= NULL) {if (P->data = = x) return count;
p = p->next;
count++;
} return 0;
} template <typename t> T dll<t>::D elete (int i) {node<t> *p,*q; p = first->next; int count, X;
Count = 1;
while (P! = null&&count<i-1) {p = p->next; count++;
} if (p = = NULL | | p->next = = NULL) throw "location illegal";
else {q = p->next;
x=q->data;
if (p->next! = NULL) {if (q->next!=null) Q->next->prior = p;
else {p->next=null;
P->next = q->next;
Delete q;
Q = NULL;
return x;
}} P->next = q->next;
Delete q;
Q = NULL;
return x; }} template <typename t> void dll<t>::p rint () {NODE<
T> *p;
p = first->next;
while (p->next! = NULL) {cout << p->data << "";
p = p->next;
} cout << p->data << Endl;
} void Main () {float Score[8] = {66.5,89.5,95,74.5,32.5,68,100,86.5};
Dll<float>student (score, 8);
cout << "Student data structure score" << Endl;
Student.print ();
cout << Endl << endl << "Insert score 78 at position 3, insert results as follows:" << Endl;
Student.insert (3,66);
Student.print (); cout << Endl << endl << "Delete score at location 2:" << student.
Delete (2) << "," << "after deletion results as follows:" << Endl;
Student.print ();
cout << Endl << endl << "position 3 score:" << student.get (3) << Endl;
cout << Endl << endl << "score 32.5 is located at:" << student.locate (32.5) << Endl; }
3, summary, operation results and analysis
① Summary
In the program to specify the result array type is float type, array total 8 elements. Float Score[8] = {66.5,89.5,95,74.5,32.5,68,100,86.5};
The object that defines the class template. Dll<float>student (score, 8);
By calling member functions, the basic functions such as output, insert, find, delete are implemented. ② operation results are as follows:
③ Analysis
There are two pointers in each data node of a doubly linked list, pointing directly to successive and direct precursors respectively. So, starting from any node in a doubly linked list, it is easy to access its predecessor and successor nodes. In the doubly linked list, the implementation of the table length, lookup, traversal and other operations is basically the same as a single linked list, only the insertion, deletion is different. Since the circular doubly linked list is a symmetric structure, insertion and deletion before or after node P is very easy.
4. Overall harvest and deficiencies, questions, etc.
The completion of the double-linked list of the experimental time coincides with the national day, the holidays took more time to complete the experiment. In general, the functionality of a doubly linked list is basically the same as a single-linked list, but there are also differences, such as the ability to delete and insert. At the beginning of the experiment, reference was made to some of the code in the textbook, but it was used without careful thought, resulting in the program not running and wasting a lot of time. Later after Baidu asked questions, ask classmates, and finally will be wrong to correct over, completed the experiment.
It is true that "practice is the only criterion for testing truth". In the classroom short study time, and can not be very good understanding of the knowledge learned, on the contrary, we need to spend more time on the machine after the class experiment, in order to more grasp the principle of knowledge learned.
Through this experiment, I also deepened the understanding of the function operation of the double linked list. But I still need to spend more time studying this course.