/* Sichuan Engineering vocational and Technical College
15 Software
*/
Head_linknode.h
/* header file for single-linked list class */
#include <assert.h>
#include "compare.h"
typedef int STATUS;//State value for shaping
#define OK 1;//status Parameters
#define ERROR 0;//status parameter
Template <class type>
Class Linknode
{
Protected
linknode* Head;
Public
void out ();//Output function
Linknode ()//object initialization
{
data=0;
Next=null;
Head=null;
}
void menu ();//Menus
Linknode<type> operator= (linknode<type> right);
Private
void sort ();//Sort
void Putin (int a);//Input function
Status Insert (int a,type b);//Insert function
void clear ();//Empty
void inversion ();//Inverted node
Status Delc (Type a);//delete all specified data nodes
Status find (Type a);//lookup (by parameter number)
Status del (int a);//delete function (refers to the anchor number)
Status Locateelem (Type E,status (*compare) (Type,type));
Member variables
type data;
Linknode<type> *next;
static int n;//The number of current single-linked list elements
Member variables
typedef linknode * nodepoint;//Pointer to node (not given type in class when object generation is determined)
};
Template <class type>
int linknode<type>::n=0;//can only be initialized outside the class
/*
Function: operator overloading
Method: The left side of the right is assigned to the left by sliding and continuing
*/
Template <class type>
Linknode<type> linknode<type>::operator= (linknode<type> right)
{
Nodepoint p=null;//pointing to the left of the single linked list the current node is empty
Nodepoint rp=right.head;//pointing to the right of the single-linked list current node
Nodepoint s;//A new node that may be needed on the left during the assignment
if (this!=&right)
{
Clear ();//Clear Left
while (RP)
{
s= new Linknode;
ASSERT (s!=0);
s->data=rp->data;
if (!head)
Head=s;
Else
p->next=s;
P=s;
rp=rp->next;
}
if (p)
p->next=null;
}
return *this;
}
/*
function: Inserting a node in front of the first node
Method: First to determine a special case (in the head insertion) and special treatment for the general situation using the first cycle to the designated node before processing
*/
Template <class type>
Status Linknode<type>::insert (int a,type b)
{
int j=1;//Indicates the ordinal of the current single-linked list
Nodepoint p=head;//starts the loop from the first node;
Nodepoint s;//for inserting
if (a==1)//In the case of head insertion
{
s= new Linknode;
ASSERT (s!=0);
(*s). Data=b;
(*s). Next=head;
Head=s;
n++;
return OK;
}
if (a>n)
return ERROR;
while (P&&j<a-1)//start from the beginning node to the previous node at the specified insertion point
{
p=p->next;
j + +;
}
s= new Linknode;
ASSERT (s!=0);
s->data=b;
s->next=p->next;
p->next=s;
n++;
Char u= ' y ';
cout<< "Is it sorted? (Y or n?) "<<endl;
cin>>u;
if (u== ' y ')
Sort ();
return OK;
}
/*
function: Input
Method: Through the insertion and inversion function implementation
*/
Template <class type>
void Linknode<type>::p utin (int a)
{
Clear ();
Type B;
cout<< "Input Start" <<endl;
for (; a>0;a--)
{
cin>>b;
Insert (1,B);
}
Inversion ();
}
/*
Function: Output single link list
Method: Continuously slide the output node data through the next pointer
*/
Template <class type>
void Linknode<type>::out ()
{
Nodepoint P=head;
cout<< "Current single-linked list" <<endl;
while (p)
{
cout<<p->data<< ' \ t ';
p=p->next;
}
cout<<endl;
}
/*
Function: Delete the specified node (refers to the anchor number)
Method: Slide The precursor node and the current node to the specified bit number to connect the predecessor node directly to the successor.
*/
Template <class type>
Status Linknode<type>::d el (int a)
{
Nodepoint p=head;//Current Node
Nodepoint r=null;//precursor node
int i=1;
if (a>n)
return ERROR;
if (a==1)//Special handling Delete first node
{
head=p->next;
P=null;
n--;
return OK;
}
while (P&&i!=a)
{
R=p;
P= (*p). Next;
i++;
}
if (a==n)//Special handling Delete tail node
{
r->next=null;
P=null;
n--;
return OK;
}
r->next=p->next;
n--;
P=null;
return OK;
}
/*
Function: Returns the found node
Method: Slide The current node to the specified bit number to return to data.
*/
Template <class type>
Status Linknode<type>::find (Type a)
{
Nodepoint P=head;
int i=1;
while (p)
{
if (A==p->data)
{
cout<<a<< "is the first <<i<<" node "in a single-linked list <<endl;
return OK;
}
i++;
p=p->next;
}
return ERROR;
}
/*
Function: Empty single linked list
Method: Delete the first node by calling the Del function on the basis of the number of nodes N of a single linked list to empty the single linked list
*/
Template <class type>
void Linknode<type>::clear ()
{
int i=1;
while (head!=null&&i<=n)
{
Del (1);
i--;
}
n=0;
}
/*
Function: Inverted single linked list
Method: The next pointer is changed from the chain backward to the chain forward and then through the transformation of the precursor, the current, successive points of the repeated transformation next and complete the inversion
*/
Template <class type>
void Linknode<type>::inversion ()
{
if (n==0)
Return
Nodepoint P=head;
Nodepoint R=null;
Nodepoint q=p->next;
while (p)
{
p->next=r;
R=p;
p=q;
if (q)//Ensure that the subsequent pointer q is not out of the table
q=p->next;
}
Head=r;
}
/*
Function: Delete all specified data nodes
Method: Scroll to the end of the chain and find the data that is equal to parameter a by using the Del function to delete the node
*/
Template <class type>
Status Linknode<type>::d ELC (Type a)
{
Nodepoint P=head;
int i=1;
int c=error;
while (i<=n)
{
if (p->data==a)
{
Del (i);
i=i-1;//after deletion from the previous one to start looking for
C=ok;
}
p=p->next;
i++;
}
return C;
}
/* Menu */
Template <class type>
void Linknode<type>::menu ()
{
Char p= ' y ';
int A;
for (;;)
{
cout<< "**************************** processing menu ****************************" <<endl;
cout<< "1. Input" <<endl;
cout<< "2. Output" <<endl;
cout<< "3. Insert" <<endl;
cout<< "4. Inverted" <<endl;
cout<< "5. Find" <<endl;
cout<< "6. Delete Point anchor node" <<endl;
cout<< "7. Delete all nodes of the specified element in a single-linked list" <<endl;
cout<< "8. Clear single-linked list" <<endl;
cout<< "9. Sort" <<endl;
Out ();
cout<< "**************************** processing menu ****************************" <<endl;
cout<< "Please select:";
cin>>a;
Switch (a)
{
Case 1:{
int b;
cout<< "Please enter the number of nodes you want to enter" <<endl;
cin>>b;
Putin (b);
};break;
Case 2:{
Out ();
};break;
Case 3:{
Type C;
int y;
cout<< "Please input you want to insert in front of that node" <<endl;
cin>>y;
cout<< "Please enter the element you want to insert" <<endl;
cin>>c;
if (insert (Y,C))
cout<< "OK" <<endl;
Else
cout<< "ERROR";
};break;
Case 4:{
Inversion ();
};break;
Case 5:{
Type Y;
cout<< "Please enter the data you want to find" <<endl;
cin>>y;
if (find (y))
cout<< "OK" <<endl;
Else
cout<< "ERROR" <<endl;
};break;
Case 6:{
int y;
cout<< "Please enter the node number you want to delete" <<endl;
cin>>y;
if (Del (y))
cout<< "OK" <<endl;
Else
cout<< "ERROR";
};break;
Case 7:{
Type Y;
cout<< "Please enter the element you want to delete" <<endl;
cin>>y;
if (Delc (y))
cout<< "OK" <<endl;
Else
cout<< "ERROR";
};break;
Case 8:{
Clear ();
};break;
Case 9:{
Sort ();
};break;
}
GetChar ();//Accept carriage return
cout<< "Do you want to continue processing? (n exit) "<<endl;
P=getchar ();
System ("CLS");
if (p== ' n ')
Break
}
}
/* Function: Sort
Method: Use bubble sort to sort by scrolling and comparing data exchange data
*/
Template <class type>
void Linknode<type>::sort ()
{
Nodepoint P=head;
Nodepoint q=p->next;
Type S;
for (int i=0;i<n;i++)
{
P=head;
q=p->next;
for (int c=0;c<n-i-1;c++)
{
if (P->data>q->data)
{
s=p->data;
p->data=q->data;
q->data=s;
}
p=p->next;
q=p->next;
}
}
}
, &NB Sp , &NB Sp LinkNode.cpp
#include "head_linknode.h"
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
int A;
Char p= ' y ';
for (;;)
{
cout<< "Please enter the type of data you want to process (char (1) int (2) float (3) double (4))" <<endl;
cin>>a;
Switch (a)
{
Case 1: {linknode<char> sq;
Sq.menu ();
}break;
Case 2: {linknode<int> sq;
Sq.menu ();
}break;
Case 3: {linknode<float> sq;
Sq.menu ();
}break;
Case 4: {linknode<double> sq;
Sq.menu ();
}break;
default:cout<< "Error Please re-enter" <<endl;
}
cout<< "Do you want to continue processing other types of sequential tables?" Please enter any character to continue or N end "<<endl;
GetChar ();
P=getchar ();
if (p== ' n ')
Break
}
System ("pause");
}
C + + single linked list basic functions