Common basic operations for a single-chain table

Source: Internet
Author: User

// Common basic operations of a single-chain table. cpp

# Include <iostream>
Using namespace STD;

Typedef struct node {
Char name [10];
Int age, score;
Struct node * next;
} Listnode;

Listnode * create_emptylist (void) // create an empty linked list and take the lead node ---------- 1
{
Listnode * temp = new listnode;
Temp-> next = NULL;
Return temp;
}

Void insert_list (listnode * List, listnode * POs, listnode X) // insert X ------------- 2 after the POs
{
If (list = NULL ){
Cout <"list is empty! "<Endl;
Return;
}

If (Pos = NULL ){
Cout <"error position found! /N ";
Return;
}

Listnode * temp = new listnode;

Strcpy (temp-> name, X. Name );
Temp-> age = x. Age;
Temp-> score = x. Score;
 
Temp-> next = pos-> next;
Pos-> next = temp;
}

Void delete_list (listnode * List, listnode * POS) // Delete the node ----------- 3 after the POs
{
If (list = NULL ){
Cout <"list is empty! "<Endl;
Return;
}

If (Pos = NULL ){
Cout <"error position found! /N ";
Return;
}

If (POS-> next = NULL ){
Cout <"no node after the postion! /N ";
Return;
}

Listnode * temp = pos-> next;
Pos-> next = temp-> next;
Delete temp;
}

Int get_length (listnode * List) // calculate the length of the linked list (number of elements) ---------- 4
{
Listnode * tail = List;
Int length = 0;
While (tail-> next! = NULL ){
Length ++;
Tail = tail-> next;
}
 
Return length;
}

Listnode * get_position (listnode * List, int order) // obtain the position value ---- 5 based on the order of the element.
{
If (! (Order> = 1 & Order <= get_length (list ))){
Cout <"error order found/N ";
Return NULL;
}

Listnode * t = List-> next;
Int num = 1;

While (Num <order ){
Num ++;
T = T-> next;
}
 
Return T;
}

Void browser (listnode * List) // The traversal Link Table (output element values) ------- 6
{
Listnode * t = List-> next;
Cout <"/nbrowsing the list:/N ";
While (T! = NULL ){
Cout <t-> name <"," <t-> age <"," <t-> score <Endl;
T = T-> next;
}

}

Listnode * get_tail (listnode * List) // obtain the position of the End Node of the linked list ----- 7
{
Listnode * tail = List;
While (tail-> next! = NULL)
Tail = tail-> next;

Return tail;
}

Void delete_list_order (listnode * List, int order) // Delete the element of the specified sequence number in a single-chain table ----- 8
{
Listnode * Pos = get_position (list, order); // The Position of the element
Listnode * pre = List;

While (pre-> next! = POS) Pre = pre-> next; // locate the front node of the element

Delete_list (list, pre );
}

 

Void main (void)
{
Listnode * mylist;
Mylist = create_emptylist ();


Listnode * tail = get_tail (mylist );

For (int K = 1; k <= 3; k ++) {// create a linked list composed of three elements
Listnode temp;
Temp. Next = NULL;

Cout <"/ninput the name :";
Cin> temp. Name;

Cout <"input the age :";
Cin> temp. Age;

Cout <"input the score :";
Cin> temp. Score;

Insert_list (mylist, get_tail (mylist), temp );

}

Browser (mylist );
Cout <"the length of the list is" <get_length (mylist) <Endl;

Delete_list (mylist, get_position (mylist, 2 ));
Cout <"after Delete the node after no. 2/N ";
Cout <"the length of the list is" <get_length (mylist) <Endl;
Browser (mylist );

Delete_list_order (mylist, 1 );
Cout <"after Delete the first node:/N ";
Cout <"the length of the list is" <get_length (mylist) <Endl;
Browser (mylist );
}

 
 

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.