Singly Linked List Operations
topic
Singly linked list construction, insertion, deletion, modification, search (generally not used, simple traversal)
Example 1
A and B are two single-linked lists (leading nodes), in which the elements are in ascending order. Design an algorithm to merge A and B into one non-decreasing linked list C by elements. C is composed of nodes in A and B.
analyze:
A, B singly linked list elements are incremented, just select a smaller one from the start nodes of A and B to insert at the end of C,
For the remaining elements in A and B, just connect A or B to the end of C.
enter:
1 3 5 7 -1
2 4 6 8 -1
output:
1 2 3 4 5 6 7 8 9
Head interpolation - creating a linked list
//创建链表1.头插法
void CreateListHead(List C){
LNode* s,* r;
C = (LNode*)malloc(sizeof(LNode));
C->next = NULL;
r = C;
int x = 0;
while (scanf_s("%d", &x) && x > 0) {
s = (LNode*)malloc(sizeof(LNode));//s指向新申请的结点
s->data=x;
/*下边两句是头插法的灵魂*/
r->next = s;//用r来接受新的结点
//为什么不用L->next=s;ans:如果这样操作,头结点就会丢失,最終头结点会指向最后一个节点
r = r->next;//r指向终端结点,以便于接纳下一个新到来的LNode
}
r->next = NULL;
}
Tail insertion method - create a linked list
//创建链表2.尾插法
void CreateListBack(LNode *&C) {
LNode* s;
C = (LNode*)malloc(sizeof(LNode));
C->next = NULL;
int x;
while (scanf_s("%d", &x) && x > 0) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = C->next;//s所指新结点的指针域next指向C的开始结点
C->next = s;//头结点指针域next指向s结点,0使得s成为新的开始结点
}
}
Connect double-linked list by head interpolation
//头插法
void mergeHead(LNode* A, LNode* B, LNode*& C) {
LNode* p = A->next;
LNode* q = B->next;
LNode* r;//r始终指向C的终端结点
C = A;
C->next = NULL;
free(B);
r = C;
while (p != NULL && q != NULL) {
if (p->data <= q->data) {
r->next = p; p = p->next;
r = r->next;
}
else {
r->next = q; q = q->next;
r = r->next;
}
}
if (p != NULL) r->next = p;
if (q != NULL) r->next = q;
}
Tail insertion double linked list join
Extension: tail insertion double linked list connection
void mergeBack(LNode* A, LNode* B, LNode* &C) {
LNode* p = A->next;
LNode* q = B->next;
LNode* s;
C = A;
C->next = NULL;
free(B);
while (p != NULL && q != NULL) {
if (p->data <= q->data) {
s = p; p = p->next;
s->next = C->next;
C->next = s;
}
else {
s = q; q = q->next;
s->next = C->next;
C->next = s;
}
}
while (p != NULL) {
s = p;
p = p->next;
s->next = C->next;
C->next = s;
}
while (q != NULL) {
s = q;
q = q->next;
s->next = C->next;
C->next = s;
}
}
complete program
#include<iostream>
using namespace std;
typedef struct LNode {
int data;
struct LNode* next;
}LNode, *List ;
void CreateListHead(LNode* &L) {
L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
LNode* r,*s;
r = L;
int x = 0;
while (scanf_s("%d", &x) && x > 0) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = r->next;
}
r->next = NULL;//输入元素已全部装入链表,L的终端终点指针域置为NULL,L建立完毕
//如果缺少这一步,会导致链表遍历指针域未初始化
}
void mergeHead(LNode* A, LNode* B, LNode*& C) {
LNode* p = A->next;
LNode* q = B->next;
LNode* r;//r始终指向C的终端结点
C = A;
C->next = NULL;
free(B);
r = C;
while (p != NULL && q != NULL) {
if (p->data <= q->data) {
r->next = p; p = p->next;
r = r->next;
}
else {
r->next = q; q = q->next;
r = r->next;
}
}
if (p != NULL) r->next = p;
if (q != NULL) r->next = q;
}
int main() {
List L1, L2;
CreateListHead(L1);
CreateListHead(L2);
List L3;
mergeHead(L1,L2,L3);
while (L3->next != NULL) {
cout << L3->next->data << " ";
L3 = L3->next;
}
return 0;
}