当前位置:网站首页>Implementation of linear list linked list structure

Implementation of linear list linked list structure

2022-06-23 05:57:00 Octopus bro

Recently, I learned data structure , Practice the implementation of chain storage structure of linear table , New linked list , Calculate the length of the list , lookup , Insert , Delete the four functions of the node , The code is as follows :

#include "stdio.h"
#include "stdlib.h"
#define TRUE 1
#define FALSE 0
typedef int bool;
typedef int ElementType;
typedef struct LNode * PtrToLNode;
struct LNode{
	ElementType Data;
	PtrToLNode Next;
};

typedef PtrToLNode List;
typedef PtrToLNode Position;

// All of the following functions have header nodes by default  
List MakeEmpty();// Create a new linked list of leading nodes  
int Lenth(List L);// Calculate the length of the list  
ElementType FindKth(List L, int K);// Find the... In the linked list K Elements and return values  
Position Find(List L, ElementType X);// According to the value lookup , Find the value for X The node of  
bool Insert(List L, ElementType X, int i);// Will be worth x The node insertion bit order of is i The location of  
bool Delete(List L, int i);// Delete the bit sequence i The node of  
void PrintLiner(List L);// Print linked list  

int main()
{
	List L = MakeEmpty();
	while(1) 
	{
		printf(" Please enter the list you want to match L Actions performed :\n");
		printf("1. Calculate the length of the list \n2. Print linked list \n");
		printf("3. Find No K Nodes and return values \n4. Find the value for K And return the location \n");
		printf("5. In the ordinal position i The insertion value of the position is X The node of \n6. The deletion order is i The node of \n"); 
		printf("0. Exit procedure \n");
		int i;
		scanf("%d",&i);
		switch(i)
		{
		case 0:
			exit(-1);// Exit the program when the parameter is negative  
		case 1:
			printf(" The length of the list is :%d\n",Lenth(L));
			printf("\n\n\n");
			break;
		case 2:
			PrintLiner(L);
			printf("\n\n\n");
			break;
		case 3:
			printf(" Please enter the node sequence ( from 1 Start ):\n");
			int K;
			scanf("%d",&K);
			if(FindKth(L,K) > 0)
				printf(" The first %d The node value is :%d\n",K,FindKth(L,K));
			else
				printf(" The bit order does not exist \n");
			printf("\n\n\n");
			break;
		case 4:
			printf(" Please enter the value you want to find ( Greater than 0):\n");
			int N;
			scanf("%d",&N);
			Position tmp;
			tmp = Find(L,N);
			if(tmp)
			printf(" The node value is :%d\n",tmp->Data);//( Because the return is the address , Unable to detect , Therefore, whether the detection value is the value to be found ) 
			printf("\n\n\n");
			break;
		case 5:
			printf(" Please enter the position and value to insert :\n");
			int pos,num;
			scanf("%d %d",&pos, &num);
			Insert(L,num,pos);
			printf("\n\n\n");
			break;
		case 6:
			printf(" Please enter the bit order to delete ( from 1 Start ):\n");
			int del;
			scanf("%d",&del);
			Delete(L,del);
			printf("\n\n\n");
			break;
		default:
			break;
		}
	}
	return 0;
}

List MakeEmpty()// Create a new linked list of leading nodes  
{
	List L = (List)malloc(sizeof(List));
	L->Data = 0;// The value of the header node is 0 
	L->Next = NULL;// Address is empty  
	return L;
} 
int Lenth(List L)
{
	int cnt = 0;// Initialize counter , Because there are header nodes , So from 0 Start  
	Position p = L->Next;// Point to the first node ,L It's the head node  
	while(p)
	{
		p = p->Next;
		cnt++;
	}
	return cnt;
}
ElementType FindKth(List L, int K)// Find the... In the linked list K Elements and return values  
{
	Position p = L;
	int cnt = 0;// Initialize counter , Because there are header nodes , So from 0 Start , If there is no header node, start from 1 Start  
	while(p && cnt < K)
	{
		p = p->Next;
		cnt++;
	}
	if(p && cnt == K)
		return p->Data;
	else
		return -1;
} 
Position Find(List L, ElementType X)// According to the value lookup , Find the value for X The node of 
{
	Position p = L,tmp;
	while(p && p->Data != X) 	
	{
		p = p->Next;
	}
	if(p)
		return p;
	else
		return NULL;
} 
bool Insert(List L, ElementType X, int i)// Will be worth x The node insertion bit order of is i The location of  
{
	if(X <= 0)
	{
		printf(" The value entered is illegal \n"); 
		return FALSE;
	}
	Position pre = L, tmp;
	int cnt = 0;
	while(pre && cnt < i - 1)// Want to insert bit order i The location of , We need to find i-1	
	{
		pre = pre->Next;
		cnt++; 
	}
	if(pre && cnt == i - 1 )
	{
		tmp = (Position)malloc(sizeof(Position));// Apply for a new node  
		tmp->Data = X;// Assign values to nodes  
		tmp->Next = pre->Next;// take i-1 The following node address is assigned to the new node  
		pre->Next = tmp;// Insert the new node into i-1 after  
		return TRUE;
	}
	else
	{
		printf(" The inserted position does not exist \n");
		return FALSE;
	}
} 
bool Delete(List L, int i)// Delete the bit sequence i( from 1 Start ) The node of 
{
	Position pre = L, tmp;
	int cnt = 0;
	while(pre && cnt < i - 1)// Cycle to the first i-1 Nodes  
	{
		pre = pre->Next;
		cnt++;
	}
	
	if(pre == NULL || cnt != i-1 || pre->Next == NULL)// If pre->Next It's empty , explain i-1 For the last node , The first i Nodes do not exist  
	{
		printf(" The node to be deleted does not exist \n");
		return FALSE; 
	}
	else
	{
		tmp = pre->Next;
		pre->Next = tmp->Next;
		free(tmp);
		return TRUE;
	}
} 
void PrintLiner(List L)// Print linked list  
{
	Position p = L->Next;// Because there are header nodes ,p Is the first node after the header node  
	while(p)	
	{
		printf("%d ",p->Data);
		p = p->Next;
	}
	printf("\n");// Line break  
} 


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410347883.html