[Data Structure] Linear Table|C/C++ Realizes Basic Operations of Singly Linked List|2021 Wangdao Data Structure Notes Sorting and Testing

The physical/storage structure of the linear list - single linked list

[Written in the front] This blog is the author's notes organized according to the video course of the 2021 Wangdao Data Structure PubMed Review Guide. All of them have been tested and feasible with a compiler. Some functions have been improved in code robustness according to the teacher's prompts, so you can use them with confidence.

Basic operation of singly linked list

1. Definition of singly linked list

typedef struct LNode {
    
    
	int data;
	struct LNode *next;
}LNode,*LinkList;

is equivalent to the following:

//或者下面的写法
struct LNode{
    
    
    int data;
    struct LNode *next;
};
typedef struct LNode LNode;//取一个别名为LNode
typedef struct LNode *LinkList;//用LinkList表明这是一个指向LNode的指针

To represent a singly linked list, you only need to declare ahead pointer L, pointing to the first node of the singly linked list.

LNode *L;
//或者
LinkList L;
//都表示声明一个指向单链表第一个结点的指针

2. Basic operation: Initialize an empty singly linked list

2.1 Singly linked list with head node

//初始化一个空的单链表(带头结点)
bool InitList(LinkList &L) {
    
    
	L = (LNode *)malloc(sizeof(LNode));//分配一个头结点
	if (L == NULL) {
    
       //分配失败返回false
		return false;
	}
	L->next = NULL; //头结点之后暂时还没有结点
	return true;
}

2.2 Singly linked list without head node

//初始化一个空的单链表(不带头结点)
bool InitList(LinkList &L) {
    
    
	L = NULL;  //空表,暂时还没有任何结点,防止脏数据
	return true;
}

3. Basic operation: Empty judgment of single linked list

3.1 Singly linked list with head node

//判断单链表是否为空(带头节点)
bool Empty(LinkList L) {
    
    
	if (L->next == NULL) {
    
    
		return true;
	}
	else {
    
    
		return false;
	}
}

3.2 Singly linked list without head node

//判断单链表是否为空(不带头节点)
bool Empty(LinkList L) {
    
    
	/*if (L == NULL) {
		return true;
	}
	else {
		return false;
	}*/
	//或者
	return(L==NULL); //L==NULL的运算结果本身就是一个bool型变量
}

4. Basic operation: bitwise search of single linked list

//基础操作:单链表的按位查找
LNode *GetElem(LinkList L, int i) {
    
    
	int j = 1;
	LNode *p = L->next;
	if (i == 0)  //是第一个结点
		return L;
	if (i < 1)   //非法输入
		return NULL;
	while (p != NULL && j < i) {
    
    
		p = p->next;       
		j++;
	}
	return p;
}

[Note] LNode * emphasizes that this is a node, and LinkList emphasizes that this is a singly linked list.

5. Basic operation: Insertion of single linked list

5.1 Insert in bit order (singly linked list with head node)

//基础操作:按位序插入:在第i个位置插入元素e
bool ListInsert(LinkList &L, int i, int e) {
    
    
	if (i < 1) {
    
    
		return false;
	}
	LNode *p;
	p = L;//p指向头结点,头结点是第0个结点(不存数据)
	//但单链表中实际存放的是头结点后面的结点,位序从1开始
	int j = 0;//表示当前p指向的是第几个结点
	while (p!=NULL && j < i-1) {
    
     //j<i-1是因为我们只需要循环到第i-1个结点在后面插入e
		p = p->next;
		j++;
	}
	if (p == NULL) {
    
    
		return false;
	}
	LNode *s = (LNode *)malloc(sizeof(LNode));
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}

5.2 Insert in bit order (singly linked list without head node)

//基础操作:按位序插入:在第i个位置插入元素e
bool ListInsert(LinkList &L, int i, int e) {
    
    
	if (i < 1) {
    
    
		return false;
	}
	if (i == 1) {
    
    
		LNode *s = (LNode *)malloc(sizeof(LNode));
		s->data = e;
		s->next = L;
		L = s;
		return true;
	}
	LNode *p;
	p = L;//p指向头结点,头结点是第0个结点(不存数据)
	//但单链表中实际存放的是头结点后面的结点,位序从1开始
	/*********************1.找到第i-1个结点*******************************/
	int j = 1;//表示当前p指向的是第几个结点
	while (p != NULL && j < i - 1) {
    
        //j<i-1是因为我们只需要循环到第i-1个结点在后面插入e
		p = p->next;
		j++;
	}
	if (p == NULL) {
    
    
		return false;
	}
	/*********************2.在第i-1个结点后面插入e**********************/
	LNode *s = (LNode *)malloc(sizeof(LNode));
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}

5.3 Post-insertion operation of specified node

	//基础操作:后插操作
bool InsertNextNode(LNode *p, int e) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	LNode *s = (LNode *)malloc(sizeof(LNode));
	if (s == NULL) {
    
      //分配内存失败
		return false;
	}
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}

Encapsulated bit-order insertion: (Note that the definition of the post-insertion operation function must be before the bit-order insertion function, otherwise an error will be reported.)

//基础操作:后插操作
bool InsertNextNode(LNode *p, int e) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	/*********************2.在第i-1个结点后面插入e**********************/
	LNode *s = (LNode *)malloc(sizeof(LNode));
	if (s == NULL) {
    
      //分配内存失败
		return false;
	}
	s->data = e;
	s->next = p->next;
	p->next = s;
	return true;
}

//基础操作:按位序插入:在第i个位置插入元素e
bool ListInsert(LinkList &L, int i, int e) {
    
    
	if (i < 1) {
    
    
		return false;
	}
	LNode *p;
	p = L;//p指向头结点,头结点是第0个结点(不存数据)
	//但单链表中实际存放的是头结点后面的结点,位序从1开始
	/*********************1.找到第i-1个结点*******************************/
	int j = 0;//表示当前p指向的是第几个结点
	while (p!=NULL && j < i-1) {
    
        //j<i-1是因为我们只需要循环到第i-1个结点在后面插入e
		p = p->next;
		j++;
	}
	return InsertNextNode(p, e);
}

5.4 Forward insertion of specified nodes

//基础操作:前插操作
bool InsertPriorNode(LNode *p,int e) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	LNode *s = (LNode *)malloc(sizeof(LNode));
	if (s == NULL) {
    
    
		return false;
	}
	s->next = p->next;
	p->next = s;
	s->data = p->data;
	p->data = e;
	return true;

}

Forward insertion operation [Book of Kings Version]

//基础操作:前插操作【王道书版本】
//在p结点之前插入结点s
bool InsertPriorNode(LNode *p, LNode *s) {
    
    
	if (p == NULL||s==NULL) {
    
    
		return false;
	}
	s->next = p->next;
	p->next = s;
	int temp = p->data;
	p->data = s->data;
	s->data = temp;
	return true;

}

6. Basic operation: printing operation of singly linked list

//基础操作:打印单链表数据
void DisplayList(LinkList L) {
    
    
	LNode *p;
	p = L->next;
	int i = 0;
	while (p!= NULL) {
    
    
		printf("%d  ", p->data);
		p = p->next;
		i++;
	}
	printf("一共有%d个元素\n",i);
}

7. Delete operation of singly linked list

7.1 Delete in bit order (singly linked list with head node)

//基础操作:按位序删除
bool DeleteNode(LinkList &L, int i,int &e) {
    
    
	if (i < 1) {
    
    
		return false;
	}
	LNode *p;
	LNode *q;
	int j = 0;
	p = L;

	while (p != NULL && j < i - 1) {
    
    
		p = p->next;
		j++;
	}

	if (p == NULL) {
    
    
		return false;
	}
	if (p->next == NULL) {
    
    //第i-1个结点后面没有其他结点了
		return false;
	}

	q = p->next;
	e = q->data;
	p->next = q->next;
	
	free(q);
	return true;
}

7.2 Deletion of designated nodes

[Note] This has been modified according to the tips of Wangdao Video Lesson. When the value of the specified node exceeds the value of the existing node, no error will be reported. At this time, the deletion fails, and the return value of the function is false.

//基础操作:删除指定结点p
bool DeleteNode(LNode *p) {
    
    
	if (p == NULL) {
    
    
		return false;
	}
	LNode *q = p->next;
	if (q == NULL) {
    
    
		return false;
	}
	p->data = q->data;
	p->next = q->next;
	free(q);
	return true;
}

8. Test code for all basic operations of singly linked list

The code can be compiled and run in the VS environment. Due to space reasons, I will put the test code in another article. The link is [Data Structure (2)] Singly Linked List | (Full) Test Code | Use C language/C++ to realize the basic operations such as the definition, insertion, deletion, search, and printout of the single linked list .
The test results are as follows, including the definition of the singly linked list, inserting and deleting elements into it.
insert image description here

Guess you like

Origin blog.csdn.net/dxy1128/article/details/107944822