C ++ data structure and algorithm _ 1 _ linear table, data structure and algorithm _ 1

Source: Internet
Author: User

C ++ data structure and algorithm _ 1 _ linear table, data structure and algorithm _ 1
Implementation and Analysis of sequence tables Abstract base class of a linear table:

Template <typename T> class LinearList {public: LinearList ();~ LinearList (); virtual int Size () const = 0; // returns the maximum Length that a linear table can store. virtual int Length () const = 0; // The length of the current linear table virtual int Search (T & x) const = 0; virtual int Locate (int I) const = 0; virtual bool getData (int I, T & x) const = 0; virtual void setData (int I, T & x) = 0; virtual bool Insert (int I, T & x) = 0; virtual bool Remove (int I, T & x) = 0; virtual bool IsFull () const = 0; virtual bool IsEmpty () const = 0; virtual void input () = 0; virtual void output () const = 0; virtual void Sort () = 0; virtual LinearList <T> operator = (LinearList <T> & L) = 0; // supports linear table copy };

Body-Sequence Table Implementation

The sequence table has the following features:

Loc (I) = Loc (j) + (I-j) * sizeof (T)

The class is defined as follows:

// Note: For some unknown meaning of the C ++ keyword meaning and usage, // please refer to the series of Blog: http://blog.csdn.net/column/details/zjf666.htmlconst int defaultSize = 10; template <class T> class SeqList: public LinearList <T> {public: SeqList (int sz = defaultSize); SeqList (SeqList <T> & L );~ SeqList () {delete [] data;} int Size () const {return maxSize;} int Length () const {return last + 1;} int Search (T & x) const; int Locate (int I) const; bool getData (int I, T & x) {if (I> 0 & I <= last + 1) {x = data [I-1]; return true;} else {return false ;}} void setData (int I, T & x) {if (I> 0 & I <= last + 1) {data [I-1] = x ;}} bool Insert (int I, T & x ); bool Remove (int I, T & x); bool IsFull () const {return last = maxSize-1;} bool isEmpty () const {return last =-1 ;} void input (); void output (); void Sort (); SeqList <T> operator = (SeqList <T> & L); protected: T * data; // array int maxSize; // maximum allowed int last; // void reSize (int newSize), the last position of the currently stored element ); // change the space size of the data array };

 

-- Implementation and analysis of each operation:

1) constructor and copy constructor

Template <typename T> SeqList <T>: SeqList (int sz) // construct the sequence table {if (sz> 0) {maxSize = sz; last =-1; // set the actual table length to NULL data = new T [sz]; checkNew (data); // check whether the array is allocated successfully} template <typename T> SeqList <T>:: SeqList (SeqList <T> & L) {maxSize = L. size (); last = L. length ()-1; data = new T [maxSize]; checkNew (data); T value; for (int I = 1; I <= L. length (); ++ I) {L. getData (I, value); // extract data from the original table data [I-1] = value; // insert data into the new table }}

 

2) Private operation: expand the size of the storage array of the sequence table

Template <typename T> void SeqList <T>: reSize (int newSize) {if (newSize <= 0) // invalid array length {cerr <"invalid size! "<Endl; exit (1) ;}if (newSize! = MaxSize) {T * newArr = new T [newSize]; // reassign a new array checkNew (newArr); int times = last + 1; // obtain the original array length T * srcPtr = data; T * destPtr = newArr; while (times --) // copy the original array content to the new array {* destPtr ++ = * srcPtr ++;} delete [] data; // do not forget to release the original data memory! Data = newArr; maxSize = newSize ;}// when the application calls a function with the SeqList type return value, the copy constructor must be used to return the operation result.

 

3) Search and locate operations

Template <typename T> int SeqList <T>: Search (T & x) const {for (int I = 0; I <= last; ++ I) {if (data [I] = x) {return I + 1; // The table item number is exactly 1} return 0;} less than the actual position number in the array ;} template <typename T> int SeqList <T>: Locate (int I) const {if (I >=1 & I <= last) return I; return 0 ;}

 

4) insert and delete sequence tables

Template <typename T> bool SeqList <T>: Insert (int I, T & x) {if (last + 1 = maxSize) // return false when the table is full; if (I <0 | I> last + 1) // return false if the table Item parameter is incorrect; for (int index = last; index> = I; -- index) // move back in sequence, and the position of the empty output I is {data [index] = data [index-1];} data [I] = x; ++ last; // table length + 1 [do not forget] return true;} template <typename T> bool SeqList <T >:: Remove (int I, T & x) {if (last =-1) // empty return false; if (I <1 | I> last + 1) // return false; x = data [I-1]; for (int index = I; index <= last; ++ index) {data [index-1] = data [index]; // loop forward, fill the I-1 position} -- last; // modify the table length return true ;}

 

5) input/output operations

Template <class T> void SeqList <T>: input () {while (1) {cin> last; // enter The location of The last element in The array if (last <= maxSize-1) break; cout <"The value of last is error, please try again! "<Endl ;}for (int I = 0; I <= last; ++ I) {cout <I + 1 <":"; cin> data [I] ;}}template <class T> void SeqList <T >:: output () const {for (int I = 0; I <= last; ++ I) {cout <"#" <I + 1 <":" <data [I] <endl ;}}

 

6) assign values

template <class T>SeqList<T> &SeqList<T>::operator=(const SeqList<T> &L){    if (maxSize < L.Length())    {        reSize(L.Length() * 2);    }    maxSize = L.Length() * 2;    last = L.Length() - 1;    T value;    for (int i = 1;i <= last + 1; ++i)    {        L.getData(i,value);        data[i - 1] = value;    }    return * this;}

-- Sequence table Performance Analysis

The most complex and time-consuming implementation of all operations on a sequence table is the implementation of search, insert, and delete operations. The time cost of search mainly focuses on the methods used for search, such as sequential search or binary search, the time cost of insertion and deletion of sequence tables is mainly the number of data moves in the cycle.


Data Structure algorithm 21 two linear tables la and lb, the new set that, how to write the C language version of the complete program

I wrote it. compile it and change it.

Void Union (LinkList * L1, LinkList * L2, LinkList * & L3) // Intersection
{
LinkList * p = L1-> next, * q = L2-> next, * s, * c;
L3 = (LinkList *) malloc (sizeof (LinkList ));
L3-> next = NULL;
C = L3;
While (p! = NULL & q! = NULL)
{If (p-> data <q-> data)
{S = (LinkList *) malloc (sizeof (LinkList); // copy Node
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
}
Else if (p-> data> q-> data)
{S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = q-> data;
C-> next = s; c = s;
Q = q-> next;
}
Else
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
Q = q-> next;
}
}

While (q! = NULL)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = q-> data;
C-> next = s; c = s;
Q = q-> next;
}
C-> next = NULL;

While (p! = NULL)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
}
C-> next = NULL;

}


Data Structure Algorithm Implementation: Two Linear tables LA and LB are used to represent two sets A and B respectively. A new set A = A and B is required.

I wrote it. compile it and change it.

Void Union (LinkList * L1, LinkList * L2, LinkList * & L3) // Intersection
{
LinkList * p = L1-> next, * q = L2-> next, * s, * c;
L3 = (LinkList *) malloc (sizeof (LinkList ));
L3-> next = NULL;
C = L3;
While (p! = NULL & q! = NULL)
{If (p-> data <q-> data)
{S = (LinkList *) malloc (sizeof (LinkList); // copy Node
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
}
Else if (p-> data> q-> data)
{S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = q-> data;
C-> next = s; c = s;
Q = q-> next;
}
Else
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
Q = q-> next;
}
}

While (q! = NULL)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = q-> data;
C-> next = s; c = s;
Q = q-> next;
}
C-> next = NULL;

While (p! = NULL)
{
S = (LinkList *) malloc (sizeof (LinkList ));
S-> data = p-> data;
C-> next = s; c = s;
P = p-> next;
}
C-> next = NULL;

}

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.