Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language problem: establish a linked list of three nodes, and then output the data of each node.
C language problem: establish a linked list of three nodes, and then output the data of each node.
//class CLinkList

# contains "CLinkList.h"

# contains "CNode.h"

CLinkList::CLinkList()

{

Cout & lt& lt "This is a constructor"

M _ Head = & ampm _ Node// The head pointer of the linked list points to the head node.

M _ node. SetNodeData(0,0); //Assign a value of 0 to the contents of the head node.

M _ node. SetNodeNext(NULL); //Set the next pointer of the head node to NULL.

}

CLinkList::~CLinkList()

{

Cout & lt& lt "This is a destructor"

}

Void CLinkList::CreateList() // Create a linked list by appending backward, and enter 0 to exit.

{

int ns core = 0; //Define a temporary integer variable to store data.

char chNum = 0; //Define a temporary character variable to store data.

Cout & lt& lt "Welcome to create a linked list!" & lt& ltendl

CNode * pTemp = NULL// Defines a temporary node pointer for adding a new node.

CNode * pNode = m _ Head// Define a tag pointer, and first call it to point to the head node.

while( 1)

{

PTemp = new LinkNode

Cout & lt& lt "Please enter the content of the next node!" & lt& ltendl

CIN & gt; & gtnScore// Enter 0 to exit.

CIN & gt; & gtchNum

if ('0' == nScore)

{

Break;

}

pTemp-& gt; SetNodeData(nScore,chNum); //Assign a value to the content of the new node

pNode-& gt; SetNodeNext(pTemp); //Let Next at the end of the chain point to the newly created node.

PNode = pTemp// Move the closing element back.

}

Cout & lt& lt "End of creating linked list"

}

LinkNode * clink list::GetListNode(int _ nIndex)

{

Cout & lt& lt "This is a member function to find the specified node by location"

link node * pNode = m _ Head-& gt; GetNodeNext(); //Define a temporary node pointer to initialize the node pointing to the head.

int Temp = 0; //Define a temporary variable to mark the number of selected nodes.

If(- 1 == _nIndex) // returns the head node (that is, the head pointer).

{

Return m _ Head

}

if(_ nIndex & lt; -1)/_ nindex control conditions

{

Cout & lt& lt "You are in the wrong position!" & lt& ltendl

Returns 0;

}

And (pNode! = empty)

{

if(_nIndex == Temp)

{

Return to pNode

}

pNode = pNode-& gt; GetNodeNext(); //The temporary node moves backward.

++temperature;

}

Returns pNode// NULL if the node cannot be found.

}

Voidclinklist:: insert list (int _ nindex, int _ nscore, char _ chnum)// Insert the function member of the node.

{

Cout & lt& lt "This is the member function of the inserted node"

LinkNode * pNode = GetListNode(_ nIndex- 1); //Define a pointer to the node class, pointing to the previous pointer where you want to insert it.

LinkNode* pTemp = new CNode// Defines a temporary node pointer for adding a new node.

pTemp-& gt; SetNodeData(_nScore,_ chNum); //Set the content of the inserted node.

pTemp-& gt; SetNodeNext(pNode-& gt; GetNodeNext());

pNode-& gt; SetNodeNext(pTemp);

}

void clink list::delete list(int _ nIndex)

{

Cout & lt& lt "This is a member function to delete nodes"

LinkNode * pNode = GetListNode(_ nIndex- 1); //Define a pointer to the node class, pointing to the previous pointer to the location to be deleted.

LinkNode * pTemp = NULL// Defines a temporary node pointer to the node to be deleted.

pTemp = pNode-& gt; GetNodeNext(); //Point pTemp to the node to be deleted.

pNode-& gt; SetNodeNext(pTemp-& gt; GetNodeNext()); //Point the pNode to the node after the node to be deleted.

Delete pTemp// Delete node

pTemp = NULL

}

LinkNode * clink list::GetHeadList()

{

Cout & lt& lt "This is a member function that gets the head pointer."

Return m _ Head

}

void clink list::SetListData(int _ nIndex,int _nScore,char _chNum)

{

Cout & lt& lt "This is a member function that sets the values of nodes in the linked list."

CNode * pNode = GetListNode(_ nIndex); //Defines a pointer to the node class, pointing to the node where the content is to be modified.

pNode-& gt; SetNodeData(_nScore,_ chNum); //Modify the content

}

void clink list::ShowListData(int _ nIndex)

{

Cout & lt& lt "This is a member function that displays the values of nodes in the linked list"

CNode * pNode = GetListNode(_ nIndex); //Defines a pointer to the node class, pointing to the node where the content is to be obtained.

pNode-& gt; show nodedata(); //Returns the contents of the node at the required position.

}

void clink list::DestroyList(int _ nIndex)

{

Cout & lt& lt "This is the member function of the linked list after it is destroyed in a certain position"

LinkNode * pTemp = GetListNode(_ nIndex- 1); //Define a node pointer to the node before the location to be destroyed.

link node * pNode = pTemp-& gt; GetNodeNext(); //Define a node pointer to the node of the location to be destroyed.

while(pTemp-& gt; GetNodeNext()! = NULL) // End condition or initial condition of the destruction operation

{

pTemp-& gt; SetNodeNext(pNode-& gt; GetNodeNext()); //Point the next node of the previous node at the location to be destroyed to the next node at the location to be destroyed.

Delete pNode// Destroy node

pNode = pTemp-& gt; GetNodeNext(); //The node that redirects the pNode to the location to be destroyed.

}

}

void CLinkList::ShowList()

{

Cout & lt& lt "This is a member function that displays a linked list"

int nTemp = 0; //Define a temporary integer variable to control the number of inputs.

link node * pTemp = m _ Head-& gt; GetNodeNext(); //Defines a node class pointer to a node with bit 0.

if(NULL == pTemp)

{

Cout & lt& lt "This is an empty chain"

}

while(pTemp! = empty)

{

pTemp-& gt; show nodedata();

++ nTemp;

if(0 = = nTemp % 5 & amp; & ampnTemp! = 0) // Control that only five nodes can be output per line.

{

cout & lt& ltendl

}

pTemp = pTemp-& gt; GetNodeNext();

}

}

//class CNode

# contains "CNode.h"

CNode::CNode()

{

int m _ ns core = 0;

char m _ chNum = 0;

m _ Next = NULL

}

CNode::~CNode()

{

}

void CNode::SetNodeData(int _ ns core,char _chNum)

{

m _ nScore = _ nScore

m _ chNum = _ chNum

}

void CNode::ShowNodeData()

{

cout & lt& ltm _ nScore & lt& lt\ t ' & lt& ltm _ chNum & lt& ltendl

}

void CNode::SetNodeNext(CNode * _ Next)

{

m _ Next = _ Next

}

CNode* CNode::GetNodeNext()

{

Return to m _ Next

}

# contains "CLinkList.h"

# contains "CNode.h"

Void text (); //Test function declaration

int main()

{

Cout & lt& lt "This is the function of encouragement"

text();

Returns 0;

}

Invalid text ()

{

Cout & lt& lt "This is a test function"

LinkList* pList = new linked list; //Create a memory linked list object

Cout & lt& lt"- Create list-"< & ltendl

pList->; create list(); //Initialize the function members of the linked list

pList->; ShowList();

cout & lt& ltendl

cout & lt& lt"-GetListNode-" & lt; & ltendl

LinkNode * pNode = NULL// Defines a temporary node class pointer to detect the members of the lookup function.

pNode = pList-& gt; GetListNode(3); //Test of finding the member function of the specified bit node by position.

Intermediate frequency (pNode)

{

Cout & lt& lt "The specified node was found by searching by location"

}

other

{

Cout & lt& lt Sorry, search by location did not find the specified bit node.

}

cout & lt& ltendl

Cout & lt& lt"- Insert list-"< & ltendl

pList->; InsertList(0,9,' x '); //Test the member function of the inserted node.

pList->; ShowList();

cout & lt& ltendl

Cout & lt& lt"- Delete list-"< & ltendl

pList->; Delete list (0); //Delete the test of the member function of a node.

pList->; ShowList();

cout & lt& ltendl

cout & lt& lt"-GetHeadList-" & lt; & ltendl

pNode = NULL

pNode = pList-& gt; GetHeadList(); //Get the test of the member function of the head pointer

Intermediate frequency (pNode)

{

Cout & lt& lt "Head pointer returned"

}

other

{

Cout & lt& lt Sorry, the head pointer is empty.

}

cout & lt& ltendl

cout & lt& lt"-GetHeadList-" & lt; & ltendl

pList->; SetListData(3,7,' @ '); //Test the member function that sets the node value in the linked list.

pList->; ShowList();

cout & lt& ltendl

cout & lt& lt"-getlist data-" & lt; & ltendl

cout & lt& lt" pList->; showlist data(3)= ";

pList->; showlist data(3); //Test the member function that gets the node value in the chain.

cout & lt& ltendl

cout & lt& lt"-destroy list(3)-" & lt; & ltendl

pList->; destroy list(3); //Test the member function of the linked list after destroying the third position.

pList->; ShowList();

cout & lt& ltendl

cout & lt& lt"-destroy list(0)-" & lt; & ltendl

pList->; DestroyList(0); //Test the member function of the linked list after destroying the zero position.

pList->; ShowList();

cout & lt& ltendl

Delete pList// Free up memory

PList = NULL// pointer is empty.

}

This is based on the linked list I wrote before. There should be some functions you don't need and you can delete them. There are basically comments, and you can change them yourself. Many of them are test programs, and you can delete them if you don't want them.