C ++ Data Structure-linked list

Source: Internet
Author: User

 

Use C ++ to implement a linked list:

First, the function analysis: structure, clean up, add, delete, modify, and query, evaluate the size to determine whether it is null, and take the header and tail

 

# Include <iostream>

Using namespace std;

 

Typedef int T;

// Linked list

Class program list

{

Struct Node

{

T data;

Node * next;

Node (const T & t): data (t), next (NULL)

{

}

};

Public:

// Clear the structure

Partition list (): head (NULL)

{

}

~ LinedList ()

{

Clear ();

}

 

Void clear ()

{

}

// InsertBack insertFront (find)

Void insertFront (const T & t)

{

}

Void insertBack (const T & t)

{

}

Void erase (const T & t)

{

}

Void update (const T & t, const T & target)

{

}

Unsigned int find (const T & t)

{

Unsigned int position = 0;

Return position;

}

// Determine whether empty calculates the size traversal (travel)

Bool empty ()

{

}

Unsigned int size ()

{

Int size = 0;

Return size;

}

Void travel ()

{

}

// Get the header and the end

T getHead ()

{

}

T getTail ()

{

 

}

// Obtain the pointer at the specified position.

Node * getPointer (int position)

{

Return NULL;

}

Private:

// The most important part of the header pointer

Node * head;

};

 

Int main ()

{

}

Function addition:

 

# Include <iostream>

Using namespace std;

 

Typedef int T;

// Linked list

Class program list

{

Struct Node

{

T data;

Node * next;

// Initialize data next to block junk data

Node (const T & t = T (): data (t), next (NULL)

{

}

};

Public:

// Clear the structure

Partition list (): head (NULL)

{

}

~ Partition list ()

{

Clear ();

}

 

Void clear ()

{

Node * p = head;

While (p! = NULL)

{

Node * q = p-> next;

Delete p; // release the space of p

P = q;

}

}

// Determine whether empty calculates the size traversal (travel)

Bool empty ()

{

// Determine whether the header pointer is null or not. If it is null, the linked list does not exist.

Return head = NULL? True: false;

}

Unsigned int size ()

{

Unsigned int size = 0;

Node * p = head;

While (p! = NULL)

{

Size ++;

Pp = p-> next;

}

Return size;

}

 

Void travel ()

{

// Use the while loop output again and again until the pointer ends with NULL

Node * p = head;

While (p! = NULL)

{

Cout <p-> data <endl;

Pp = p-> next;

}

}

// InsertAfter insertFront (find)

Void insertFront (const T & t)

{

Node * p = new Node (t );

P-> next = head; // The address pointed to by the head pointer to p's next

Head = p; // Set * p as the header.

}

Void insertAfter (const T & t)

{

Node * p = new Node (t );

Node * tail = getPointer (size ()-1 );

Tail-> next = p;

}

Void erase (const T & t)

{

Unsigned int position = find (t );

Node * cur = getPointer (position );

If (position! = 0)

{

Node * pre = getPointer (find (t)-1 );

Pre-> next = cur-> next;

} Else {

Head = cur-> next;

}

Delete cur;

}

Void update (const T & t, const T & target)

{

Node * p = getPointer (find (target ));

P-> data = t;

}

Unsigned int find (const T & t)

{

Unsigned int position = 0;

Node * p = head;

While (p! = NULL)

{

If (p-> data = t)

{

Return position;

}

Pp = p-> next;

Position ++;

}

Return position;

}

// Get the header and the end

T getHead ()

{

Return head-> data;

}

T getTail ()

{

Node * p = getPointer (this-> size ()-1 );

Return p-> data;

}

// Obtain the pointer at the specified position.

Node * getPointer (int position)

{

Node * p = head;

For (int I = 0; I <position; I ++)

{

Pp = p-> next;

}

Return p;

}

Private:

// The most important part of the header pointer

Node * head;

};

 

Int main ()

{

}

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.