Basic single-chain table operation C ++ implementation

Source: Internet
Author: User

The following is a simple procedure to implement a single-chain table using C ++ code:

Viod add (T Val );// Add an element at the end of the linked list

Bool insertat (INT POs, t Val );// Insert the element Val at the position where the index is POS

Bool remove ();// Delete the tail element of the linked list

Boo removeat (int pos );// Delete the element of any specified index

 

 

T getheadval ();// Return the value of the element in the head of the linked list.

T gettailval ();// End Element value

Int find (T Val );// If this element is found in the linked list and the index number is not found,-1 is returned.

Bool isempty ();// Judge whether the linked list is empty

Void clear ();// Clear the linked list

Int size ();// Returns the total number of elements in the linked list.

 

1. node class definition

Template <class T>

Struct Node

{

T val; // node Value

Node <t> * Next; // pointer to the next element

Node (T nval)

{

Val = nval;

Next = NULL;

}

Node (void ){}

};

 

2. Definition of linked list classes

# Include "node. H"

# Include <iostream>

Using namespace STD;

 

Template <class T>

Class linklist

{

Int size; // number of elements

Public:

Node <t> * head; // The head pointer of the linked list.

Node <t> * tail; // tail pointer of the linked list

Linklist (void) // Constructor

{

Head = tail = NULL;

Size = 0;

}

~ Linklist (void) {clear ();}

 

VoidAdd (T Val)// Add elements at the end

{

Node <t> * pnode = new node <t> (VAL );

If (Head = NULL) // when the linked list is empty

{

Head = pnode;

Tail = pnode;

}

Else {

Tail-> next = pnode;

Tail = pnode;

}

Size ++;

}

 

 

Bool insertat (intPOs, t Val)// Insert element

{

Node <t> * pnode = NULL;

If (Pos <0 | POS> size ){

Cout <"out of range" <Endl;

Return false;

}

 

If (Pos = size) // insert an element at the end

{

Add (VAL );

Returntrue;

}

Elseif (Pos
= 0) // insert an element in the header

{

Pnode = new node <t> (VAL );

Pnode-> next = head;

Head = pnode;

}

Else {

Node <t> * pnode = getpointerat (POS-1); // returns the element pointer before the insert position.

Node <t> * newnode = new node <t> (VAL );

Newnode-> next = pnode-> next;

Pnode-> next = newnode;

}

Size ++;

Return true;

}

 

BoolRemove (){// Delete the tail Element

Return removeat (size-1 );

}

 

Bool removeat (intPos)// Delete the specified position Element

{

Node <t> * pnode = NULL;

If (size = 0 ){

Cout <"list is empty" <Endl;

Returnfalse;

}

If (Pos <0 | POS> size-1 ){

Cout <"out of range" <Endl;

Returnfalse;

}

 

If (size = 1) // when there is only one element, the linked list is cleared.

{

Clear ();

}

Else {

If (Pos = 0) // Delete the Header element

{

Pnode = head;

Head = head-> next;

Delete pnode;

}

Else {

Node <t> * pprenode = getpointerat (POS-1 );

Pnode = pprenode-> next;

Pprenode-> next = pnode-> next;

Delete pnode;

If (Pos = size-1) // tail Element

Tail = pprenode;

}

}

Size --;

Return true;

}

 

T getheadval (){

If (size = 0 ){

Cout <"list is empty" <Endl;

Return NULL;

}

Return head-> val;

}

T gettailval (){

If (size = 0 ){

Cout <"list is empty" <Endl;

Return NULL;

}

Return tail-> val;

}

 

IntFind (T Val)// Search for elements

{

Int Index = 0;

Node <t> * IP = head;

While (IP! = NULL ){

If (IP-> val = Val)

Return Index;

IP = IP-> next;

Index ++;

}

Return-1;

}

 

Bool isempty (){Return size = 0? True: false ;}

Int size (){Return
Size ;}

Void clear (){

While (Head! = NULL ){

Node <t> * TMP = head-> next;

Delete head;

Head = TMP;

}

Tail = NULL;

Size = 0;

}

 

PRIVATE:

Node <t> * getpointerat(Int pos ){

Node <t> * pnode = NULL;

If (Pos <0 | POS> size-1)

Cout <"out of range" <Endl;

Else {

Pnode = head;

For (int
I = 1; I <= Pos; I ++)

Pnode = pnode-> next;

}

Return pnode;

}

 

}

;

 

 

Related Article

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.