Data Structure: Linked List


1. Definition of linked list

A linked list consists of a series of structures (called nodes), where each node contains a pointer to the next node in the linked list (except the last node is a null pointer), where each node can contain several storage The data field of the data.

2. Advantages and disadvantages

Compared with the array, the use of the linked list structure can overcome the disadvantage that the array needs to know the data size in advance, can make full use of the computer memory space , and realize flexible dynamic memory management. However, the linked list loses the advantages of random reading of the array , and at the same time, the space overhead of the linked list is relatively large due to the increase of the pointer field of the node.

Structure diagram of a singly linked list:

3. C language implementation of singly linked list

(1) Type declaration

#include <stdlib.h>
#include <stdbool.h>

#define DATA_TYPE int
struct node;
typedef struct node* List;
typedef struct node* Position;

List New_List(List head,DATA_TYPE X); //Create a new linked list
bool is_empty(List head); //Determine whether the list is empty
bool is_last(Position P); //Determine whether position P is the end of the linked list
Position Find(DATA_TYPE X, List head); //Find an element and return a pointer to the element
void Delete(DATA_TYPE X, List head); //Delete element
void Insert(DATA_TYPE X, List head); //Insert elements
List Delete_list(List head); //Delete the linked list

struct node
{
	DATA_TYPE data;
	Position next;
};


(2) New linked list operation

List New_List(List head,DATA_TYPE X)
{
	Position new_node;

	if (!is_empty)
	{
		printf("The linked list already exists\n");
		return NULL;
	}
	
	new_node = malloc(sizeof(struct node));
	if (new_node == NULL)
	{
		printf("Allocation failed\n");
		return NULL;
	}
	head = NULL;
	new_node->data = X;
	new_node->next = head;
	head = new_node;
	return head;
}

Icon:




(3) Determine whether the linked list is empty and determine whether the target node is the last node of the linked list

bool is_empty(List head)
{
	return head->next == NULL;
}

bool is_last(Position P)
{
	return P->next == NULL;
}



(4) Find the target node X

Position Find(DATA_TYPE X, List head)
{
	Position TmpPt;
	for (TmpPt = head; TmpPt->next != NULL; TmpPt = TmpPt->next)
		if (TmpPt->data == X)
			return TmpPt;
	return NULL;
}



(5) Delete the target node X

void Delete(DATA_TYPE X, List head)
{
	Position cur, prev, TmpPt; //prev points to the previous node of cur

	TmpPt = head;
	for (cur = head, prev = NULL; cur != NULL&&cur->data != X; prev = cur, cur = cur->next);
	
	if (cur == NULL)
		return head; //Data X not found
	if (prev == NULL)
		TmpPt = TmpPt->next; //Data X is in the head node
	else
		prev->next = cur->next; //on other nodes
	free(cur);
	return TmpPt;
		
}

In the following linked list, we assume that the node to be deleted is 40.



(6) Insert node X

void Insert(DATA_TYPE X, List head)
{
	Position TmpPt, new_node;
	new_node = malloc(sizeof(struct node));
	if (new_node == NULL)
	{
		printf("Cannot allocate new node\n");
		return NULL;
	}

	new_node->data = X;
	new_node->next = head;
	head = new_node;
}

Icon:


(7) Delete the linked list

List Delete_list(List head)
{
	Position P, TmpPt;

	P = head->next;
	head->next = NULL;
	while (P != NULL)
	{
		TmpPt = P->next;
		free(P);
		P = TmpPt;
	}
	free(head);
	return NULL;
}


(4) Data structure algorithm questions


Topic description: Suppose an arithmetic expression can contain three kinds of parentheses: parentheses " ( " and " ) ", square brackets " [ " and " ] " and curly brackets " { " and "}", and these three kinds of parentheses Can be nested in any order (eg: … [ { [ ] ] [ ] ( ) ) . Write an algorithm that determines whether parentheses contained in a given expression are paired correctly. The output result is YES or NO.

Input: 5+{[2X5]+2}

output: NO

#include<stdio.h>
#include<stdlib.h>
#define LEN 10000
void push_stack(const char par);
int pop_stack(const char par);
void clear_stack(void);

struct node
{
	char par;
	struct node *next;
};

struct node *top = NULL;
struct node *new_node;

char bracket [LEN + 1];

intmain()
{
	int i, j;
	scanf("%s", bracket);
	for (j = 0;; j++)
	{
		if (bracket[j] == '(' || bracket[j] == '[' || bracket[j] == '{')
			push_stack(bracket[j]);
		else if (bracket[j] == ')' || bracket[j] == ']' || bracket[j] == '}')
		{
			if (pop_stack(bracket[j]) == -1)
				break;
		}
		else if (bracket[j] == '\0')
		{
			if (top != NULL)
			{
				printf("NO\n");
				break;
			}
				printf("YES\n");
				break;
			}
			else continue;
	}
	clear_stack();
	return 0;
}

void push_stack(const char par)
{
	new_node = (struct node *)malloc(sizeof(struct node));
	if (new_node == NULL) exit(EXIT_FAILURE);
	new_node->par = par;
	new_node->next = top;
	top = new_node;
}

int pop_stack(const char par)
{
	struct node *TmpPt;
	if (by == ')')
	{
		if (top == NULL)
		{
			printf("NO\n");
			return -1;
		}
		if (top->par != '(')
		{
			printf("NO\n");
			return -1;
		}
		else
		{
			TmpPt = top;
			top = top->next;
			free(TmpPt);
		}
	}
	else if (par == ']')
	{
		if (top == NULL)
		{
			printf("NO\n");
			return -1;
		}
		if (top->par != '[')
		{
			printf("NO\n");
			return -1;
		}
		else
		{
			TmpPt = top;
			top = top->next;
			free(TmpPt);
		}
	}
	else if (par == '}')
	{
		if (top == NULL)
		{
			printf("NO\n");
			return -1;
		}
		if (top->par != '{')
		{
			printf("NO\n");
			return -1;
		}
		else
		{
			TmpPt = top;
			top = top->next;
			free(TmpPt);
		}
	}
	return 0;
}

void clear_stack(void)
{
	struct node *TmpPt;
	while (top != NULL)
	{
		TmpPt = top;
		top = top->next;
		free(TmpPt);
	}
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325988988&siteId=291194637