Data structures-some basic operations of linear tables C + + code

Source: Internet
Author: User

Sequential storage structure for linear tables template <class t>class linearlist{public:linearlist (int maxlistsize = =); ~linearlist () {delete [] element;} BOOL IsEmpty () const {return length = = 0;} BOOL Isfull () const {return length = = MaxSize;} int length () const {return length;}  BOOL Find (int k, T &item) Const;int Search (const T &item) const;void Delete (int k, t &item); void Insert (int k, Const T &item);p rivate:int MaxSize, length, t *element;}; Template <class t>linearlist<t>::linearlist (int maxlistsize = =) {MaxSize = Maxlistsize;element = new T[   MaxSize];                Size of maxsize array space length = 0; There is no real table node at the beginning, so the table length is 0}//access: assigns the field value of the node labeled K to item and returns True, and returns false;template <class T>bool linearlist<t> If it does not exist! :: Find (int k, T &item) const{if (k < 0 | | length = = 0 | | k >length-1) {cout << "unreasonable position or Empty list! "<< Endl;return false;} Else{item = Element[k];return true;}} Lookup: Finds the node in the table whose value is item and returns its subscript, or returns -1;template <class T>int line If there is no item in the tableArlist<t>::search (const T &item) const{for (int k = 0; k <= length-1; k++) {if (element[k] = = Item) return k;} return-1;}   Delete the node with table K in the table and assign its value to ItemTemplate <class t>void linearlist<t>::D elete (int k, T &item) {if (Find (k, item)) If a node labeled K is found, then all nodes after it are moved forward one position {for (int i=k; i<length; i++) {element[i] = element[i+1];} item = element[k];length--;}}                   Insert the ItemTemplate <class t>void linearlist<t>::insert (int k, const T &item) {if (Isfull ()) After the nodes in table K below. If the table is full, you cannot insert a new node {cout << "the list is full!" << endl;exit (1);} else if (k<0 | | k>length-1) {cout << "the node does not exist!" << endl;exit (1);} Else{for (i=length-1; i>=k+1; i--) {element[i+1] = Element[i];}} ELEMENT[K+1] = item;length++;}                      Single-linked table node structure slnodetemplate<class t>struct slnode{t data;             Data domain slnode<t> *next; Pointer field Slnode (slnode *nextnode = NULL) {next = NextNode;} Slnode (const T &item, Slnode *nextnode = NULL) {data= Item;next = nextnode;}};/ /slnode Definition Reference Blog data structure-some basic operations of the stack C + + code//single-linked list of chained storage structure template <class T>class sllist{public:sllist () {head = new Slnode   <T> ()};                       constructor, constructs an empty table sllist (T &item) with only sentinel nodes; Constructor ~sllist (); bool IsEmpty () const {return head->next = = NULL;}; int length () const;//bool Find (int k, T &item) const;int Search (const t &item) const;void Delete (int k, T &ite m); void Insert (int k, const T &item);p rivate:slnode<t> *head; Slnode<t> *tail; Slnode<t> *currptr;};/ /single-linked list constructor, generates an empty table with only Sentinel nodes Template<class T>sllist<t>::sllist () {head = tail = Currptr = new slnode<t> (); size = 0;} A single-linked list constructor that generates a table containing sentinel nodes and a table node Template<class t>sllist<t>::sllist (T &item) {tail = Currptr = new Slnode         <T> (item);               Generate a table node head = new slnode<t> (CURRPTR); Generate Sentinel node size = 1;} Single-linked list destructor template <class t>sllist<t>::~sllist () {while (! IsEmpty ()) {currptr = Head->next;head->next =Currptr->next;delete Currptr;} Delete Head;} Algorithm insert//inserts a data field value to item's node template <class t>void sllist<t>::insert (const T &item) After the current node { Currptr->next = new Slnode<t> (item, Currptr->next); if (tail = = currptr) tail = currptr->next;size++;} Insert a data field value for item in the end of the table template <class t>void sllist<t>::insertfromtail (const T &item) {tail-> Next = new Slnode<t> (item, NULL); tail = tail->next;size++;} Insert Template <class t>void sllist<t>::insertfromhead (const T &item) {if (IsEmpty ()) After sentinel node {head-> Next = new Slnode<t> (item, head->next); tail = Head->next;} Else{head->next = new Slnode<t> (item, head->next);} size++;} The algorithm delete//deletes the successor node of the current node and returns its data value to the variable de_itemtemplate <class t>bool sllist<t>::D elete (T &de_item) {if (currptr = = Tail | | IsEmpty ()) {cout << "No next node or empty list!"; return false;} slnode<t> *temp = Currptr->next;currptr->next = temp->next;size--;d E_item = temP->data;if (temp = = tail) tail = Currptr;delete Temp;return true;} Delete the first true table node after the Sentinel node and return its data value to the variable de_itemtemplate <class t>bool sllist<t>::D eletefromhead (T &de_item) {if (IsEmpty ()) {cout << "Empty list!"; return false;} slnode<t> *temp = Head->next;head->next = temp->next;size--;d e_item = temp->data;if (temp = = tail) { tail = head;} Delete Temp;return true;} Delete the footer node and return its data value to the variable de_itemtemplate <class t>bool sllist<t>::D eletefromtail (T &de_item) {if ( IsEmpty ()) {cout << "Empty list!"; return false;} The current pointer points to the precursor node of the footer node setend (); Prev ();d E_item = Tail->data;currptr->next = null;size--;d elete tail;tail = Currptr;return true;}

Data structures-some basic operations of linear tables C + + code

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.