# "Bogey sends you an invitation to learn", a one-way and two-way linked list of data structure

Posted by sonny on Sun, 09 Jan 2022 01:53:30 +0100

catalogue

order

Hi, this is fox

Experiment content:

Experiment Description:

Source code

Operation screenshot:

Experiment content:

Experiment Description:

source code

Summary

# order

On the train of youth, if you want to get off early, please don't wake me up

## Hi, this is fox

Today is the weekend. I didn't sleep all night yesterday. I don't know why I lost sleep. Just get up and thank you for your blog. Recently, the anxiety of middle-aged people has slowly spread to my heart and my whole body is uncomfortable. Maybe I can get a little peace by sitting in front of the computer screen. Today, let's share the knowledge of data structure - single linked list and two-way linked list.

Single linked list is a data structure with chain access. A group of storage units with arbitrary address are used to store the data elements in the linear list. The data in the linked list is represented by nodes. The composition of each node is: element (image of data element) + pointer (indicating the storage location of subsequent elements). The element is the storage unit for storing data, and the pointer is the address data connecting each node.

## Node of one-way linked list

As shown in the following code, the node of the two-way linked list contains two pointing relationships and a data space. The two points to the previous and next nodes connecting the node respectively. The data type can be a structure type or other types.

```typedef struct node
{
int data;
struct node *pre;
struct node *next;
}Node;```

### Experiment content:

1. Randomly generate or input a group of elements (no less than 10 elements) on the keyboard to establish a single linked list of leading nodes.
2. Invert the elements in the single linked list (it is not allowed to apply for new node space).
3. Delete all even element nodes in the single linked list.
4. Write a function to insert an element into the non decreasing ordered linked list so that the elements of the linked list are still ordered, and use this function to establish a
Non decreasing ordered single linked list.
5. Use algorithm 4 to establish two non decreasing ordered single linked lists, and then merge them into a non increasing linked list.
6. Decompose the linked list established by algorithm 1 into two linked lists, one of which is all odd and the other is all even (as far as possible)
Use existing storage space).
7. Design a simple menu in the main function and call the above algorithm.

### Experiment Description:

1. Node type definition

```   typedef  int  ElemType； // Element type
typedef  struct  LNode
{
ElemType  data ;
struct  LNode * next ;

### Source code

```#include <iostream>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
typedef int ElemType;

typedef struct LNode
{
ElemType data;
struct LNode* next;

{
int n;

{
cout << "Failed to create linked list!" << endl;
exit(0);
}
cout << "Enter the number of elements to be generated randomly(No less than 10): ";
cin >> n;
srand(unsigned(time(NULL)));
for (int i = 0; i < n; i++)
{
p = new LNode;
p->data = rand() % 100;
temp->next = p;
temp = p;
}
temp->next = NULL;
}
{
{
}
cout << endl;
}
{
LNode *p;
{
delete p;
}
}
{
LNode *p1, *p2, *p3;                         //p3 is the first node, p2 is the second node and p1 is the third node
{
cout << "The linked list is empty!" << endl;
return;
}
p2 = p3->next;
if (p2 == NULL)
{
return;
}
p1 = p2->next;
p3->next = NULL;
while (p1)
{
p2->next = p3;
p3 = p2;
p2 = p1;
p1 = p1->next;
}
p2->next = p3;
}
{
LNode *p1, *p2;
{
if (phead->data % 2 == 0)
{
delete p1;
continue;
}
}
}
{
ElemType *num, temp;
LNode *p;
int count = 0, i = 0;
{
cout << "The linked list is empty!" << endl;
return;
}
while (p)
{
count++;
p = p->next;
}
num = new ElemType[count];
while (p)
{
num[i++] = p->data;
p = p->next;
}
for (int j = 0; j < count - 1; j++)
for (int k = 0; k < count - j - 1; k++)
if (num[k] > num[k + 1])
{
temp = num[k];
num[k] = num[k + 1];
num[k + 1] = temp;
}
i = 0;
while (p)
{
p->data = num[i++];
p = p->next;
}
delete[] num;
}
{
LNode *p1, *p2, *tmp = NULL;
while (p1)
{
if (elem < p1->data)
break;
p2 = p1;
p1 = p1->next;
}
tmp = new LNode;
tmp->data = elem;
p2->next = tmp;
tmp->next = p1;
}
{
LNode *p1, *p2;
p2 = L1;
p1 = L1->next;
while (p1)
{
if ((p1->data) % 2 == 0)
{
L2->next = p1;
L2 = p1;
p1 = p1->next;
p2->next = p1;
}
else
{
p2 = p1; p1 = p1->next;
}
}
L2->next = NULL;
}
{
p2 = L1;
p1 = L1->next;
while (p1)
{
p2 = p1; p1 = p1->next;
}
p2->next = L2->next;
}
int main()
{
LNode *L = NULL, *L1 = NULL, *L2 = NULL;
int num;
cout << "1:Random generation or keyboard input of a group of elements (no less than 10 elements),Establish a single linked list of leading nodes" << endl;
cout << "2:Element inversion in single linked list" << endl;
cout << "3:Single linked list sort output" << endl;
cout << "4:Delete all even element nodes in the single linked list" << endl;
cout << "5:Insert an element and use this function to establish a non decreasing ordered single linked list (insert)" << endl;
cout << "6:Create two non decreasing ordered single linked lists, and then merge them into a non increasing linked list (merge)" << endl;
cout << "7:The linked list is decomposed into two linked lists, one of which is all odd and the other is all even" << endl;
cout << "8:sign out" << endl;
cout << endl;
while (true)
{
cout << "Please enter a numeric option:";
cin >> num;
switch (num)
{
case 1:
cout << "L The linked list is:";
cout << endl;
}break;
case 2:
cout << "The linked list is:";
cout << endl;
}break;
case 3:
cout << "The linked list is:";
cout << endl;
}break;
case 4:
cout << "The linked list is:";
cout << endl;
}break;
case 5:
{   ElemType elem;
cout << "Enter the element to insert:";
cin >> elem;
cout << "The linked list is:";
cout << endl;
}break;
case 6:
{
cout << "L1 The linked list is:";
cout << "L2 The linked list is:";
cout << "The merged linked list is:";
cout << endl;
}break;
case 7:
{
L2 = new LNode;
cout << "L1 The linked list is:";
cout << "L2 The linked list is:";
cout << endl;
}break;
case 8:
{
delete L1;
cout << endl;
cout << "The linked list has been released and deleted"<<endl;
cout << endl;
}break;
default:
cout << "Input error! Please re-enter!" << endl;
cout << endl;
}
}
return 0;
}```

### Operation screenshot:

Bidirectional linked list, also known as double linked list, is a kind of linked list. There are two pointers in each data node, pointing to the direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its predecessor node and successor node. Generally, we construct a two-way circular linked list.

### Creation of bidirectional linked list

Head interpolation and tail interpolation only look at the code, which is difficult to understand. It is suggested that drawing the change diagram of the linked list when inserting nodes will be very easy to understand. In addition, I think head interpolation is better because its insertion logic is in place in one step. Unlike tail interpolation, it also needs to complete the ring connection of the two-way linked list later.

```Node * creatList()
{
Node * cur = NULL;
int data;
scanf("%d",&data);
while(data)
{
cur = (Node*)malloc(sizeof(Node));
cur->data = data;
cur->next->pre = cur;
scanf("%d",&data);
}
}```

2. Tail interpolation

```Node * creatList()
{
Node * cur = NULL;
int data;
scanf("%d",&data);
while(data)
{
cur = (Node*)malloc(sizeof(Node));
cur->data = data;
scanf("%d",&data);
}
//Complete loopback
}```

### Experiment content:

1. Establish a two-way linked list by tail interpolation.
2. Traverse the two-way linked list.
3. Delete a specified element from the bidirectional linked list.
4. Implement the algorithm of inserting element e in non decreasing ordered bidirectional linked list.
5. Judge whether the elements in the two-way linked list are symmetrical. If they are symmetrical, return 1, otherwise return 0.
6. Set the element as positive integer, and implement the algorithm to arrange all odd numbers before even numbers.
7. Design a simple menu in the main function and call the above algorithm.

### Experiment Description:

Type definition of bidirectional linked list

```   typedef  int  ElemType；  // Element type
typedef  struct  DuLNode
{
ElemType  data;
DuLNode *prior, *next;

### source code

```#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;

typedef  int  ElemType;
typedef  struct  DuLNode
{
ElemType  data;
struct DuLNode *prior, *next;

{
int n;
srand(unsigned(time(NULL)));
cout << "Enter the number of elements to create the linked list:";
cin >> n;
for (int i = 0; i < n; i++)
{
tmp = new DuLNode;
tmp->data = rand() % 100;
tmp->prior = p;
p->next = tmp;
p = tmp;
}
}
{
DuLNode *p;
{
cout << setw(4) << p->data;
p = p->next;
}
cout << endl;
}
{
DuLNode *p, *t;
{
t = p;
p = p->next;
delete t;
}
delete p;
}
{
DuLNode *p1, *p2;
while (p1)
{
if (x == p1->data)
{
p2->next = p1->next;
p1->next->prior = p2;
delete p1;
return;
}
p2 = p1;
p1 = p1->next;
}
}
{
DuLNode *p;
int count = 0, i = 0;
ElemType *num, tmp;
{
cout << "The two-way linked list is empty!" << endl;
return;
}
{
count++;
p = p->next;
}
num = new ElemType[count];
{
num[i++] = p->data;
p = p->next;
}
for (int k = 0; k < count - 1; k++)
for (int j = 0; j < count - k - 1; j++)
if (num[j] > num[j + 1])
{
tmp = num[j];
num[j] = num[j + 1];
num[j + 1] = tmp;
}
i = 0;
{
p->data = num[i++];
p = p->next;
}
delete[] num;
}
{
DuLNode *p, *tmp;

{
if (elem <= p->data)
break;
p = p->next;
}
tmp = new DuLNode;
tmp->data = elem;
tmp->next = p;
tmp->prior = p->prior;
p->prior->next = tmp;
p->prior = tmp;
}
{
DuLNode *p1, *p2;
while (p1 != p2)
{
if (p1->data != p2->data)
{
return 0;
}
p1 = p1->prior;
p2 = p2->next;
}
return 1;
}
{
DuLNode *p1, *p2;
p2 = p1->next;
{
cout << "The two-way linked list is empty!" << endl;
return;
}
{
return;
}
{
if ((p1->data) % 2 != 0)
{
p2 = p1;
p1 = p1->next;

p1->prior = p2->prior;
p2->prior->next = p1;

}
else
p1 = p1->next;
}
}
int main()
{
DuLNode *L = NULL;
ElemType t;
int num;
cout << "1:Using tail interpolation method to establish a two-way linked list" << endl;
cout << "2:Traversing bidirectional linked list" << endl;
cout << "3:Deletes a specified element from a two-way linked list" << endl;
cout << "4:Insert elements in non decreasing ordered bidirectional linked list e Still ordered algorithm." << endl;
cout << "5:Judge whether the elements in the two-way linked list are symmetrical. If they are symmetrical, return 1; otherwise, return 0" << endl;
cout << "6:Set the element as positive integer, and the implementation algorithm arranges all odd numbers before even numbers" << endl;
cout << "7:sign out" << endl;
while (true)
{
cout << "Please enter a numeric option:";
cin >> num;
switch (num)
{
case 1:
{
cout << "L The two-way linked list is:";
cout << endl;
}break;
case 2:
{
cout << "The two-way linked list is:";
cout << endl;
}break;
case 3:
{
cout << "Enter the element to delete:";
cin >> t;
cout << "The two-way linked list is:";
cout << endl;
}break;
case 4:
{
cout << "Enter the element to insert:";
cin >> t;
cout << "The two-way linked list is:";
cout << endl;
}break;
case 5:
{
cout << "Element symmetry in bidirectional linked list" << endl;
else
cout << "Asymmetric elements in a two-way linked list" << endl;
cout << endl;
}break;
case 6:
{
cout << "The two-way linked list is:";
cout << endl;
}break;
case 7:
{
cout << endl;
return 0;
}break;
default:
cout << "Input error! Please re-enter!" << endl;
cout << endl;
}
}
return 0;
}```

# Summary

If the programming language is compared to a sentence, the data structure is like an article. Write a good sentence with clear meaning. If you write an article, you pay more attention to the idea it conveys. Mark Twain once said that when you hold a hammer in your hand, you see that many things are nails. I don't want to delve into the original meaning of this sentence. After all, it's not a technical problem. After reading a classic article by the famous hacker Raymond< asHe Cheng is a hacker >, I have a new understanding of this statement. He wrote in his article: "being a hacker will have a lot of fun, but it} is fun that takes a lot of effort. These efforts need motivation. Successful athletes get motivation from the pleasure of exercising and surpassing their own limits. Similarly, being a hacker, you have to get basic fun from solving problems, honing skills and exercising intelligence." Hammer is a tool, nail is a problem. When tools are used to solve your problems, it is difficult to describe the feeling of satisfaction. Learning C language is not just to learn C language. You want to use it to solve problems. Unfortunately, in reality, many students left this tool aside after finishing the C language course in their freshman and sophomore years, and even got a good score. It's a pity that they didn't enjoy it. Are you a man with a hammer? Hold on!

All right. That's all for today. It's time for me to go to bed. In the future, I will release more project sources or learning materials. I hope you can continue to pay attention and reply to any questions. If you want to master in advance, you can add groups to get C/C + + learning materials and the source code of other projects, you can add groups [1083227756] to understand. Interested in the future development of programmers, you can focus on WeChat official account: Fox encoding time, hope to learn progress with everyone!