Experiment content:
1. Randomly generate or input a group of elements (no less than 10 elements) to establish a single chain table with leading nodes.
2. Invert the elements in the single chain table (it is not allowed to apply for new node space).
3. Delete all even element nodes in the single chain table.
4. Write a function to insert an element into a non decreasing ordered list so that the list elements are still ordered, and use this function to build a
Non decreasing ordered single chain table.
5. Using algorithm 4, two non decreasing ordered single chain tables are established, and then combined into a non increasing chain table.
6. Decompose the list established by algorithm 1 into two lists, one of which is all odd and the other is all even (try to
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 ; } LNode, *pLinkList ;
2. For simplicity, a single chain table with leading nodes is used.
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; }LNode, *pLinkList; pLinkList LinkList_Init() //Single chain table creation { int n; LNode *phead, *temp, *p; phead = new LNode; if (phead == NULL) { cout << "Link list creation failed!" << endl; exit(0); } cout << "Enter the number of elements that need to be randomly generated(No less than 10): "; cin >> n; temp = phead; 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; return phead; } void LinkList_Display(pLinkList phead) //Output linked list { phead = phead->next; while (phead) { cout << setw(4) << phead->data; phead = phead->next; } cout << endl; } void LinkList_Free(pLinkList phead) //Linked list release { LNode *p; while (phead) { p = phead; phead = phead->next; delete p; } } void LinkList_Convert(pLinkList phead) //Element inversion in single chain table { LNode *p1, *p2, *p3; //p3 is the first node, p2 is the second node and p1 is the third node if (phead->next == NULL) { cout << "The list is empty!" << endl; return; } p3 = phead->next; 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; phead->next = p2; } void LinkLIst_Del_oushu(pLinkList phead) //Delete all even element nodes in a single chain table { LNode *p1, *p2; p2 = phead; phead = phead->next; while (phead) { if (phead->data % 2 == 0) { p1 = phead; phead = phead->next; delete p1; p2->next = phead; continue; } p2 = phead; phead = phead->next; } } void LinkList_Sort(pLinkList phead) //sort { ElemType *num, temp; LNode *p; int count = 0, i = 0; if (phead->next == NULL) { cout << "The list is empty!" << endl; return; } p = phead->next; while (p) { count++; p = p->next; } num = new ElemType[count]; p = phead->next; 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; } p = phead->next; i = 0; while (p) { p->data = num[i++]; p = p->next; } delete[] num; } void LinkList_Insert(pLinkList phead, ElemType elem) //Insert an element { LNode *p1, *p2, *tmp = NULL; p1 = phead->next; p2 = phead; while (p1) { if (elem < p1->data) break; p2 = p1; p1 = p1->next; } tmp = new LNode; tmp->data = elem; p2->next = tmp; tmp->next = p1; } void LinkList_Divide(pLinkList L1, pLinkList L2) //The list is divided into odd and even lists { 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; } void LinkList_Cat(pLinkList L1, pLinkList L2) //List merging, { pLinkList p1, p2; p2 = L1; p1 = L1->next; while (p1) { p2 = p1; p1 = p1->next; } p2->next = L2->next; LinkList_Sort(L1); LinkList_Convert(L1); } 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),Establishing a single chain table with leading nodes" << endl; cout << "2:Element inversion in single chain table" << endl; cout << "3:Single chain table sort output" << endl; cout << "4:Delete all even element nodes in a single chain table" << endl; cout << "5:Insert an element and use this function to create a non decreasing ordered single chain table (insert)" << endl; cout << "6:Create two non decreasing ordered single chain tables and merge them into a non increasing chain table (merge)" << endl; cout << "7:The list is divided into two 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 number option:"; cin >> num; switch (num) { case 1: { L = LinkList_Init(); cout << "L The linked list is:"; LinkList_Display(L); cout << endl; }break; case 2: { LinkList_Convert(L); cout << "The linked list is:"; LinkList_Display(L); cout << endl; }break; case 3: { LinkList_Sort(L); cout << "The linked list is:"; LinkList_Display(L); cout << endl; }break; case 4: { LinkLIst_Del_oushu(L); cout << "The linked list is:"; LinkList_Display(L); cout << endl; }break; case 5: { ElemType elem; cout << "Enter the element to insert:"; cin >> elem; LinkList_Sort(L); LinkList_Insert(L, elem); cout << "The linked list is:"; LinkList_Display(L); cout << endl; }break; case 6: { L1 = LinkList_Init(); cout << "L1 The linked list is:"; LinkList_Display(L1); L2 = LinkList_Init(); cout << "L2 The linked list is:"; LinkList_Display(L2); LinkList_Cat(L1, L2); cout << "The merged linked list is:"; LinkList_Display(L1); cout << endl; }break; case 7: { L2 = new LNode; LinkList_Divide(L1, L2); cout << "L1 The linked list is:"; LinkList_Display(L1); cout << "L2 The linked list is:"; LinkList_Display(L2); LinkList_Free(L2); cout << endl; }break; case 8: { LinkList_Free(L); delete L1; cout << endl; cout << "List released and deleted"<<endl; cout << endl; }break; default: cout << "Input error! Please re-enter!" << endl; cout << endl; } } return 0; }
Running screenshot: