Data structure - operation and implementation of single linked list (including all codes)

Posted by Karlos94 on Sun, 23 Jan 2022 04:57:50 +0100

Article catalogue

preface

Recently, I learned the linked list in learning the data structure, then tested myself and wrote the completion of the basic operation, mainly based on Yan Weimin's data structure textbook and the guidance book for the postgraduate entrance examination of kingly data structure.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Basic operation

printlist(linklist l); / / traversal of single linked list
createlist_ h(linklist& l, int n); / / create a single linked list (header insertion method)
createlist_ r(linklist& l, int n); / / create a single linked list (post interpolation)
initlist(linklist& l); / / initialize the single linked list
listempty(linklist l); / / judge whether it is an empty table
destroylist_ l(linklist& l); / / destroy the single linked list
clearlist(linklist& l); / / clear the single linked list
lengthlist_l(linklist l); / / calculate the length of the single linked list
getelem(linklist& l, int i, elemtype& e); / / value of single linked list
locateelem(linklist l, elemtype e); / / single linked list lookup
listinsert(linklist& l, int i, elemtype e); / / insertion of single linked list
listdelete(linklist& l, int i); / / delete single linked list
 

2, Code implementation

1. Code

This is test Part C is mainly the part of the main function

#include "test.h"

//Viewing single linked list
void printlist(linklist l);
//Create a single linked list (header insertion)
void createlist_h(linklist& l, int n);
//Create single linked list (post interpolation)
void createlist_r(linklist& l, int n);
//Judge whether the creation is successful
void create(linklist &l);
//Initialize single linked list
status initlist(linklist& l);
//Judge whether it is an empty table
int listempty(linklist l);
//Destroy single linked list
status destroylist_l(linklist& l);
//Empty single linked list
status clearlist(linklist& l);
//Calculate the length of single linked list
int lengthlist_l(linklist l);
//Value of single linked list
void get(linklist& l);
bool getelem(linklist& l, int i, elemtype& e);
//Single linked list lookup
void locate(linklist l);
int locateelem(linklist l, elemtype e);
//Insertion of single linked list
void insert(linklist& l);		//Determine whether the insertion is successful
status listinsert(linklist& l, int i, elemtype e);
//Deletion of single linked list
void delete_elem(linklist& l);
bool listdelete(linklist& l, int i);





void menu()
{

	cout << "------Operations of single linked list--------" << endl;
	cout << "----------Please select--------------" << endl;
	cout << "------1,establish    2. Insert------" << endl;
	cout << "------3,delete    4. Search------" << endl;
	cout << "------5,empty    6. Value------" << endl;
	cout << "------7,display	  0,sign out------" << endl;
}

int main()
{
	linklist l;
	initlist(l);
	int n = 0;
	int i = 0;
	do{
		menu();			//menu
		cin >> n;		
		switch (n)
		{
		case 1:
			create(l);
			break;
		case 2:
			insert(l);
			break;
		case 3:
			delete_elem(l);
			break;
		case 4:
			locate(l);
			break;
		case 5:
			 i = clearlist(l);
			if (i)
			{
				cout << "Empty successfully" << endl;
			}
			break;
		case 6:
			get(l);
			break;
		case 7:
			printlist(l);
			break;
		case 0:
			cout << "sign out" << endl;
			break;
		default:
			cout << "Input error" << endl;
			break;
		}
	} while (n);
}

This is linklist Part C is mainly the part of the function body

#include "test.h"


void printlist(linklist l)
{
	lnode* p = l->next;
	while (p)
	{
		cout << p->data << endl;			//print contents
		p = p->next;						//Point to the next node
	}
}


status initlist(linklist& l)
{	//Construct an empty single linked list
	l = new lnode;		//It can also be l = (*linklist)malloc(sizeof(lnode)); Generate a new node as the head node, and point to the head node with the head pointer L 				// Dynamically allocate a linklist type lnode size space to L
	l->next = NULL;		//The pointer field of the header node is set to null
	return ok;
}

int listempty(linklist l)
{//If there is no element in the linked list, it is called an empty linked list (the head pointer and head node are still there)
	if (l->next)
		return 0;	//If not null, return 0
	else
		return 1;
}
void destroylist_l(linklist& l)
{//Starting from the pointer, release all nodes in turn
	lnode* p;					//Define a pointer to a node p
	while (l)					//When the pointer to l is not null
	{
		p = l;					//l's address to p
		l = l->next;			//l points to the next node
		delete p;				//Delete p pointer
	}
}
status clearlist(linklist& l)
{//Release all nodes in turn and set the pointer field of the header node to null
	lnode* p, * q;		//Create a pointer p, q to the header node
	p = l->next;		//The address of the next node is assigned to p
	while (p)			//When p is not empty
	{
		q = p->next;	//The address of the next node of p is assigned to q
		delete p;		//Delete p
		p = q;			//The address of q is assigned to p
	}
	l->next = NULL;		//Null header pointer field
	return ok;
}

int lengthlist_l(linklist l)
{//Start from the initial node and count all nodes in turn
	int i = 0;
	lnode* p;				//Create a pointer to the head node p
	p = l->next;			//The pointer field of the head node is assigned to p
	while (p)				//When p is not empty
	{
		i++;				//count
		p = p->next;		//Point to the next node
	}
	return i;
}
status getelem(linklist& l, int i, elemtype& e)
{//In the single linked list l of the leading node, obtain the value of the element according to the sequence number i, and use e to return the value of the ith element in l (return address)
	int j = 1;				//Calculation j
	lnode* p;				//Create a pointer to the head node p
	p = l->next;			//The pointer field of the head node is assigned to p
	while (p && j < i)		//When p is not empty and j is not less than i
	{
		p = p->next;		//p points to the next node
		j++;				//Count++
	}
	if (!p || j > i)		//If p is empty or j > I
		return error;
	e = p->data;			//The data field of the node pointed to by p is assigned to e
	return ok;
}
void get(linklist& l)
{
	int e ,place= 0;
	cout << "Please enter the location to get the value" << endl;
	cin >> place;
	bool flag = getelem(l, place, e);
	if (flag)
	{
		cout << "The value you take is" << e << endl;
	}
	else
	{
		cout << "Nonstandard value" << endl;
	}
}
int locateelem(linklist l, elemtype e)
{
	lnode* p;
	int j = 1;
	p = l->next;
	while (p && p->data != e)
	{
		p = p->next;
		j++;
	}
	if (p) return j;
	else return error;
}
void locate(linklist l)
{
	int e = 0;
	cout << "Please enter the location to find" << endl;
	cin >> e;
	int place = locateelem(l,e);
	if (place)
	{
		cout <<"The value you're looking for is in the second" << place <<"Location" << endl;
	}
	else
	{
		cout << "The value you are looking for cannot be found" << endl;
	}
}
//lnode* locateelem(linklist l, elemtype e)
//{/ / find the element with value e in the single linked list l of the leading node
//	lnode* p; 						// Create a p pointer to the head pointer
//	p = l->next; 					// The address of the next node is assigned to P
//	while (p && p->data != e) 		// The judgment condition is that P is not empty or the data field is not equal to E
//	{
//		p = p->next; 				// Find the next node
//	}
//	return p; 						// Returns the address of P
//}

bool listinsert(linklist& l, int i, elemtype e)
{//Insert a new node with the value e at the ith position in the single linked list l of the leading node
	lnode* p;							//Create a p pointer to the head pointer
	p = l;
	int j = 0;
	while (p && (j < i - 1))			//The judgment condition p is not empty, and j is positioned before the subscript of the inserted element
	{
		p = p->next;					//Look down
		j++;
	}
	if (!p || (j > i - 1))				//! p means I > N + 1 has exceeded the length of the linked list, and j > I-1 means I < 1
	{
		return false;
	}
	lnode* s = new lnode;				//Create a new node
	s->data = e;						//Assign a value to the data field of the new node
	s->next = p->next;					//Originally, p points to the address of the next node to s points to the address of the next node
	p->next = s;						//The address of s points p to the next node
	return true;
}
void insert(linklist& l)
{
	int place, e = 0;
	cout << "Please enter the location and value to insert" << endl;
	cin >> place >> e;
	bool flag = listinsert(l, place, e);
	if (flag)
	{
		cout << "Insert successful" << endl;
		printlist(l);
	}
	else
	{
		cout << "Insert failed" << endl;
	}
		
}

bool listdelete(linklist& l, int i)
{
	lnode* q, * p;						//Create p and q pointers to header pointers
	p = l;
	int j = 0;
	while (p->next && (j < i - 1))			//The judgment condition p is not empty, and p and j are positioned before the subscript of the inserted element
	{
		p = p->next;					//p points to the next node
		j++;
	}
	if (!(p->next) || (j > i - 1))		//! P - > next means I > n has exceeded the length of the linked list, and j > I-1 means I < 1
	{
		return false;
	}
	q = p->next;						//Let q go to the next node
	p->next = q->next;					//The next node pointed to by p is the next node of q
	delete q;							//Delete q
	return true;
}
void delete_elem(linklist& l)
{
	int place = 0;
	cout << "Please enter the location you want to delete" << endl;
	cin >> place;
	bool flag = listdelete(l, place);
	if (flag)
	{
		cout << "Delete succeeded" << endl;
		printlist(l);
	}
	else
	{
		cout << "Deletion failed" << endl;
	}	
}
void createlist_h(linklist& l, int n)
{
	initlist(l);							//Initialize single linked list
	for (int i = 0; i < n; i++)				//According to the linked list length n
	{
		lnode* p = new lnode;				//Create a new node
		cin >> p->data;						//Enter the data field for the new node
		p->next = l->next;					//Leave p - > next blank
		l->next = p;						//The lower node of the header pointer l is p
	}
}
bool createlist_r(linklist& l, int n)
{//Enter the values of n elements in the positive bit order to establish a single linked list l with header nodes
	initlist(l);							//Initialize single linked list
	clearlist(l);							//Empty single linked list
	lnode* r = l;							//The tail pointer r points to the head node
	for (int i = 0; i < n; i++)				//According to the linked list length n
	{
		lnode* p = new lnode;				//Create a new node
		cin >> p->data;						//Enter a data field for the new node
		p->next = NULL;						//Leave p - > next blank
		r->next = p;						//The address of p is assigned to the pointer field of r
		r=p;								//Think of r as p
	}
	return true;
}

void create(linklist &l)
{
	int n = 0;
	cout << "Please enter the length of the single linked list to be created" << endl;
	cin >> n;
	bool flag = createlist_r(l, n);
	if (flag)
	{
		cout << "Created successfully" << endl;
		printlist(l);					//Displays the contents of the current single linked list
	}
	else
	{
		cout << "Creation failed" << endl;
	}
}

This is test H, mainly the header file

//Inclusion of header file
#include <iostream>
#include <stdlib.h>
using namespace std;

//Definition of symbols
#define status int
#define ok 1
#define error 0
#define elemtype int
//Storage structure of linked list
typedef struct lnode
{
	elemtype data;			//Data field of node
	struct lnode* next;	//Pointer field of node
}lnode, * linklist;			//linklist is the pointer type to the structure lnode

2. Display effect

summary

There is still a big difference between realizing code and understanding code. Details determine success or failure. The habit of writing code can determine your height and your accuracy. If you make mistakes in the process of writing, don't be agitated. Calm down and analyze them layer by layer. The main thing is to be cold and quiet. Don't hit the computer. After writing the program, it's not my success, I warn that there are 32 hahaha, so I still need to improve the robustness of the code, and the single linked list uses a large number of pointers. I'm very flustered. The pointer error is implicit and difficult to detect. Be careful when using it. I recommend a book about pointers. You must know that you don't know the best. Otherwise, I wrote a very good book "c and pointers" in vain. I'm still improving. Let's work together, hh~~

Topics: data structure linked list