Design and Implementation of an experiment three-hop Table Algorithm

Source: Internet
Author: User

Design and Implementation of an experiment three-hop Table Algorithm

I. Experiment Name: Design and Implementation of the table jumping Algorithm

Ii. Purpose:

Iii. experiment content

Complete the following procedures and answer questions.

1 # include <iostream. h> 2 # include <stdlib. h> 3 4 enum ResultCode {Underflow, Overflow, Success, Duplicate, RangeError, NotPresent}; 5 template <class T> 6 struct SNode 7 {8 SNode (int mSize) 9 {10 link = new SNode * [mSize]; 11} 12 ~ SNode () {delete [] link;} 13 T element; 14 SNode <T> ** link; 15 }; 16 17 template <class T> 18 class SkipList 19 {20 public: 21 SkipList (T large, int MeV); 22 ~ SkipList () {}; 23 ResultCode Insert (T x); // For function definitions, see section 3.1.1 24 void printList () {// zengjiabufei25 SNode <T> * p; 26 for (int le = levels; le> = 0; le --) {27 p = head-> link [le]; 28 cout <"head-> "; 29 SNode <T> * q; 30 q = head-> link [0]; 31 while (p! = Tail) {32 while (q-> element! = P-> element) {33 cout <"---"; 34 q = q-> link [0]; 35} 36 if (p-> element <10) cout <"-"; 37 cout <p-> element; 38 cout <"-"; 39 p = p-> link [le]; 40 q = q-> link [0]; 41} 42 while (q! = Tail) {cout <"---"; q = q-> link [0];} 43 cout <"tail"; 44 cout <endl; 45} 46 }; 47 private: 48 int Level (); 49 SNode <T> * SaveSearch (T x); 50 int maxLevel, levels; 51 SNode <T> * head, * tail, ** last; 52}; 53 54 template <class T> 55 SkipList <T>: SkipList (T large, int mlev) 56 {57 maxLevel = mlev; levels = 0; 58 head = new SNode <T> (maxLevel + 1 ); // points to the header node 59 tail = new SNode that includes the element Field and maxLevel + 1 pointer <T> (0); // points to only the element field, the End Node of the pointer field is not 60 last = new SNode <T> * [maxLevel + 1]; // maxLevel + 1 pointer 61 tail-> element = large; // The maximum large62 for (int I = 0; I <= maxLevel; I ++) 63 head-> link [I] = tail; // All pointers to the header node point to the End Node 64} 65 66 template <class T> 67 int SkipList <T>: Level () 68 {69 student writing 70} 71 72 template <class T> 73 SNode <T> * SkipList <T>: SaveSearch (T x) 74 {75 student writing 76} 77 template <class T> 78 ResultCode SkipList <T>: Insert (T x) 79 {80 student writing part 81} 82 83 void main () {84 int maxlevel = 5; 85 SkipList <int> slist (10000, maxlevel ); 86 int ele [30] = {,}; 87 int n = 14; 88 cout <"--- jump table ---" <endl; 89 cout <"The numbers inserted respectively are:"; 90 for (int j = 0; j <n; j ++) cout <ele [j] <","; 91 cout <"\ n initial status: \ n"; 92 slist. printList (); 93 for (int I = 0; I <n; I ++) {94 slist. insert (ele [I]); 95 cout <"\ n Insert" <ele [I] <"" <endl; 96 slist. printList (); 97} 98}View Code

 

Program Problems

1. In the SkipList constructor, what are the meanings of the large and mleev parameters? In the program insert, what is the role of the if (lev> levels) statement?

2. Analyze the Skip table Insertion Algorithm, and provide the flowchart of the Insertion Algorithm or describe the algorithm IDEA.

3. If you enter, the final structure of the running table is:

4. If the input value ranges from 1 to 30 and the number of layers is 7, what is the final structure of the table to be jumped.

5. (Optional) create a new program file. Based on the preceding program, modify the Insertion Algorithm so that repeated elements can be inserted and enter 3,7,, and.

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.