Linked List c ++ instance program in asp.net

Source: Internet
Author: User
Tags define null
The code is as follows: Copy code

# Include <iostream. h>
// # Define NULL 0
Class Node // Node class
{
Public:
Node (){}
Node (int n) {num = n; next = NULL ;}
Node (int n, Node * p) {num = n; next = p ;}
Void setNum (int n) {num = n ;}
Void setNext (Node * p) {next = p ;}
 
Int getNum () {return num ;}
Node * getNext () {return next ;}
Private:
Int num;
Node * next;
};
Class Linklist // linked list class
{
Public:
Linklist () {head = NULL ;}
Linklist (int n) {head = new Node (n );}
Void addAtEnd (int n );
Void addAtEnd (Node * p );
Void visitAllNode ();
Void addBeforeNum (Node * p, int num); // add a new Node p before the Node value is num
Void deleteNum (int num); // delete a node with the value of num
Private:
Node * head;
};
Void Linklist: addAtEnd (int n) // add a node to the end of the linked list
{
Node * p = head;
If (head = NULL) {head = new Node (n );}
Else
 {
While (p-> getNext ()! = NULL)
{P = p-> getNext ();} // Use p to point to the last node.
P-> setNext (new Node (n); // Insert a Node
 }
 
}
Void Linklist: addAtEnd (Node * p) // add a Node to the end of the linked list
{
Node * pn = head;
If (head = NULL) {head = p ;}
Else
 {
While (pn-> getNext ()! = NULL)
{Pn = pn-> getNext ();} // use pn to point to the last node.
Pn-> setNext (p); // Insert a node
 }
 
}
Void Linklist: visitAllNode () // you can call this operation to create a traversal table.
{
Node * p;
P = head;
If (p = NULL) {cout <"empty linked list! "<Endl ;}
Else
 {
Cout <"header :";
While (p-> getNext ()! = NULL)
  {
Cout <p-> getNum () <"--> ";
P = p-> getNext ();
  }
Cout <p-> getNum () <"table end" <endl;
 }
}
Void Linklist: addBeforeNum (Node * p, int num) // add a new Node p before the Node value is num
{
Node * pn = head, * follow = pn;
If (pn = NULL)
 {
Cout <"empty linked list! Insert a node. ";
Head = p;
Return;
 }
While (pn-> getNext ()! = NULL & pn-> getNum ()! = Num)
// Find the node pointing to the node with the value of num
 {
Follow = pn; // follow always points to a node before pn
Pn = pn-> getNext ();
 }
If (pn = head & pn-> getNum () = num) // locate the node and use the header pointer
 {
P-> setNext (pn );
Head = p;
 }
Else if (pn-> getNum () = num) // locate this node. Note: follow points to the previous node.
 {
P-> setNext (pn );
Follow-> setNext (p );
 }
Else // no node found. The pn points to the last node and inserts the new node to the end of the linked list.
 {
Cout <"is not found. It is inserted to the end of the linked list. "<Endl;
Pn-> setNext (p );
 }
 
}
Void Linklist: deleteNum (int num) // delete a node
{
Node * p = head, * follow = p;
If (p-> getNum () = num) // if the deleted node is 1st
 {
Head = p-> getNext ();
Delete p;
 }
Else
 {
While (p-> getNext ()! = NULL & p-> getNum ()! = Num)
  {
Follow = p; // follow always points to the previous node of p
P = p-> getNext ();
  }
If (p-> getNum () = num)
  {
Follow-> setNext (p-> getNext ());
Delete p;
  }
 
 }
}
Void main ()
{
Linklist * pl = NULL;
Int num, n;
Node * p;
Char option;
While (1)
 {
Cout <"\ n linked list operation exercise:" <endl;
Cout <"1: Create a linked list" <endl;
Cout <"2: insert a new node at the end of the linked list" <endl;
Cout <"3: insert a new node before a node with the node value of num" <endl;
Cout <"4: deleting a node" <endl;
Cout <"5: traversing nodes" <endl;
Cout <"0: Exit" <endl;
 
Cout <"\ n enter your choice :";
Cin> option;
 
Switch (option)
  {
Case '0 ':
Break;
Case '1 ':
Cout <"enter the first node value" <endl;
Cin> num;
Pl = new Linklist (num );
Pl-> visitAllNode ();
Break;
Case '2 ':
Cout <"Enter the node value :";
Cin> num;
If (pl = NULL) {pl = new Linklist (num );}
Else
   {
Pl-> addAtEnd (num); // pl-> addAtEnd (new Node (num ));
   }
Pl-> visitAllNode ();
Break;
Case '3 ':
Cout <"Enter the inserted node value :";
Cin> num;
Cout <"Enter the node to which you want to insert data, expressed as the value of the node :";
Cin> n;
Pl-> addBeforeNum (new Node (num), n );
Pl-> visitAllNode ();
Break;
Case '4 ':
Cout <"Enter the value of the node to be deleted :";
Cin> num;
Pl-> deleteNum (num );
Pl-> visitAllNode ();
Break;
Case '5 ':
Pl-> visitAllNode ();
Break;
Default:
Cout <"Your input is incorrect. Please enter it again! "<Endl;
  }
If (option = '0 ')
Break;
 }
}
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.