Implementation of single linked list of algorithm data structure + operation and comparison with sequential tables

Source: Internet
Author: User

Order comparison of advantages and disadvantages of tables and single-linked lists:

The advantage of sequential tables, without the need to add additional storage space to represent the logical relationship between elements in the table ;

Quick access to elements from anywhere in the table .

The disadvantage of the sequential table, after inserting the delete operation need to move a large number of elements ;

when the linear table length is not stable, storage space is difficult to determine, it is easy to cause storage space fragmentation.


For single-linked lists

Chained storage is the memory unit that the element stores can be discontinuous, decentralized. For how elements are maintained between their relationships (that is, the logical structure, the precursors and successors of each element. )

A pointer field is used to store the relationship between him and the predecessor or the subsequent direct.

As above is the pointer structure of a single-linked list, that is, each element stores the memory space address of his successor element.


Application Scenario Selection

By the simple to know the data of the common operation, increase, delete, change, check.

When data manipulation is biased toward checking and changing, sequential storage speed is very fast, and the linked list, you must start from the head node to traverse the lookup.

But when it comes to inserting or deleting elements, sequentially storing each operation in order to maintain the order, must move the element, and for the linked list, just add a storage unit, the pointer to a few of them in the field to make a change in the line.

Tips1. However, it is important to note that the INSERT and delete operations of a table actually consist of two parts: traversing to find the element I, and then deleting or inserting it. In fact, the time complexity of the sequential table is O (1),

While the time complexity of the single-linked list is O (n), although the subsequent order table needs to move the elements behind I back one bit, the time of the bill is relatively o (n), and the single-linked list does not need to move the elements,

The time is O (1). It seems to be even, so the single-linked list has little advantage over the sequential storage structure of the linear table. However, if you insert a K element at point I,

So relative Yu Xinzu, as long as the first traversal to find the element I, after the operation is basically O (1), and the sequence of operations, is to move the n-i element each time, O (n),

The effect is out.

Tips2. And for sequential tables, if we insert and delete operations, regardless of the storage space allocation of this dead corner (that is, the storage space can be dynamically applied, regardless of overflow),

Is the last element of the operation, it is not also very good, that is, delete and insert are at the end of the table, then you do not have to move a large number of elements.



Code implementation:

#ifndef _list_h#define _list_h#include<iostream> #include <assert.h>//assertion #include<string.h># include<stdlib.h> #include <malloc.h> #include <iostream>using namespace std; #define Elemtype inttypedef struct Node{elemtype data;struct Node *next;} Node, *pnode;typedef pnode list;/*-------------------------Function declaration------------------------*/void menu ();//Menu void Initlist (list *list);//Initialize function bool CreateList (list *list, int count);//CREATE function bool IsEmpty (list *list);//Judge null bool Push_front (List *list, Elemtype x);//head interpolation int push_back (list *list, Elemtype x);//tail plug bool Pop_front (list *list);//Header delete bool Pop_back (list *list);//tail-delete node* find (list *list, Elemtype x);//query returns the pointer to the input ordinal intlocatepos (list *list, Elemtype x);//query Gets the value of the input ordinal void Length (list *list);//Delete_pos (list *list, Elemtype POS);//Position delete bool Delete_val (list *list, Elemtype x);//value Delete bool Modify (list *list, int pos, elemtype x);//Modify the range of a location bool Destroylist (list *list);//Destroy void Showlist (list list);//display bool Insert_pos (List *list, int pOS, elemtype x);//Position insert bool Insert_val (list *list, Elemtype x);//value insertion (ordered premise) bool sort (list *list);//Sort node* resver (list *li ST);//reverse void reverse_2 (list *list);//Reverse node* Next (list *list, Elemtype key);//returns the input location of the successor node* Prio (list *list, Elemtype key);//returns the input position of the precursor node node* pfind (List *list, size_t POS);//Returns a pointer to the corresponding position #endif


#include "List.h"/*-----------------function Implementation Part----------------------*/void menu () {cout << "" << endl;cout << "**********************************" << endl;cout << "* [+] createlist *" << en Dl;cout << "* [0] quit_system [1] push_back *" << endl;cout << "* [2] push_front [3] show_list *" &lt ;< endl;cout << "* [4] pop_back [5] Pop_front *" << endl;cout << "* [6] Insert_pos *" << en Dl;cout << "* [8] delete_pos [9] delete_val *" << endl;cout << "* [ten] find [all] Locatepos *" &lt ;< Endl; cout << "* [[] modify [] Destroy *" << endl;cout << "* [+] sort [] resver *" &LT;&L T Endl;cout << "* [+] length [+] Next *" << endl;cout << "* [] Prio *" & lt;< endl;cout << "**********************************" << endl;cout << "Plese chose:>";} /*-----------Initialize the single-linked list ——————————————— */void initlist (List *list) {*list = (node *) malloc (sizeof (node));(*list)->next = NULL; (*list)->data = 0;} /* Create a single-linked list---------------------*///???????---------the head node BOOL CreateList (List *list,int count)//node * *list{node *p = *list;  Elemtype x = 0;for (int i = 1; I <= count; ++i) {cin >> x;p = P->next = (node *) malloc (sizeof (node));p->data = X;p->next = NULL; (*list)->data++;} return false;} /*----Determine the empty--------*/bool isEmpty (List *list) {return (NULL = = (*list)->next);} /*-----------------tail plug ———————————— */int push_back (list *list, Elemtype x) {if (IsEmpty (list)) return 0; node* cur = (*list)->next;while (cur->next! = NULL) {cur = cur->next;} Node *p = (node *) malloc (sizeof (node)), if (p = = null) return 0;p->data = X;cur->next = P;p->next = NULL; (*list)-&G t;data++;} /*-----------------Head plug ———————————— */bool push_front (List *list, elemtype x) {Node *p = (node *) malloc (sizeof (node)); if (p = = NULL) return false;p->data = X;p->next = (*list)->next; (*lIST)->next = p; (*list)->data++;} /*------Display--------------*/void showlist (List list) {Node *p = List->next;while (P! = NULL) {cout << p->data &lt ;< "--";p = P->next;} cout << "Nul" << Endl;} /*-----------------Tail Delete ———————————— */bool pop_back (list *list) {if (IsEmpty (list)) return false; node* cur = (*list); while (cur->next! = NULL && Cur->next->next! = null) {cur = cur->next;} Free (cur->next); cur->next = NULL; (*list)->data--;return true;} /*-----------------Header Delete ———————————— */bool pop_front (list *list) {if (IsEmpty (list)) return false; Node *p = (*list)->next; (*list)->next = P->next;free (p);p = NULL; (*list)->data--;} /*-------------------Insert--------------*/bool insert_pos (list *list, int pos, Elemtype x) by location {Node *p = pfind (list, pos-1); Find I-1 node if (p = = NULL)//i<1 or i>n+1 insert position I is wrong {cout << "ERROR POS" << Endl;} node* s = (node *) malloc (sizeof (node)); s->data = X;s->next = p->next; P->next = S;returnfalse;} /*--------Location Delete---------------*/bool delete_pos (List *list, elemtype pos) {int count = 1; node* cur = NULL; node* pre = *list;while (Pre->next! = NULL && Pre->next->next! = null && count < POS)//Find precursor {PR e = pre->next;count++;} if (count! = pos) {cout << "Erroe pos" << Endl;return false;} Cur = Pre->next;pre->next = cur->next;free (cur); cur = NULL; (*list)->data--;return true;} /*----------Delete by value-------------*/bool delete_val (List *list, Elemtype x) {node* pre = *list; node* cur = null;while (pre->next! = NULL && Pre->next->next! = null)//Find precursor {if (x = = Pre->next->dat a) {cur = Pre->next;pre->next = cur->next;free (cur); cur = Null;return true;} Pre = Pre->next;} (*list)->data--;return true;} /*----------------query returns pointer ———————————— */node* find (List *list, Elemtype x) {Node *p = (*list)->next;while (p) {if (P >data! = x) p = p->next;elsebreak;} return p;} /*----------Enter an ordinal return pointer —————— */node* pfind (List *list, Size_t Pos) {size_t count = 1; Node *p = (*list)->next;while (Count < pos) {p = p->next;count++;} if (count! = pos) return Null;return p;} /*-----------------query returns the ordinal ———————————— */intlocatepos (List *list, Elemtype x) {node* cur = (*list)->next;int count = 0;    while (cur) {count++;if (x = = Cur->data) return count; Returns the number of elements cur = cur->next;} return 0;} /*--------------Modify---------------*/bool Modify (List *list,int pos, elemtype x) {int count = 1;if (pos < 0 | | pos > (*l IST)->data) return false;  node* cur = (*list)->next;while (cur! = NULL && Cur->next! = null) {if (count = = pos) {cur->data = X;return true;} cur = cur->next;count++;} return false;} /*-------Destroy-------------*/bool destroylist (List *list) {Node *p = (*list); Node *q = Null;while (p) {q = P->next;free (p);p = q;}  (*list)->next = null;cout << "The list is destroyed!" << endl;cout << "after destroy, any operation Is invalid! "<< endl;cout <<" quit_system! "&LT;&LT Endl;return true;} /*----------Sort--------------*/bool sort (list *list) {if (IsEmpty (list)) return false; node* p = NULL; node* q = null;for (P = (*list)->next; P! = null; p = p->next) {for (q = p->next; Q! = null; q = q->next) {if (P ->data > Q->data) {p->data = p->data ^ Q->data;q->data = p->data ^ q->data;p->data = P->data ^ Q->data;}}} return true;} /*-------Reverse-----------*/node* resver (List *list) {Node *p = (*list)->next; Node *q = NULL; Node *r = Null;while (p) {q = P->next;p->next = R;r = P;p = q;} return r;}    void reverse_2 (List *list) {Node *p, *q;p = (*list)->next; P points to the first element of the list (*list)->next = NULL;  Break the opening node with the linked list while (P! = NULL) {q = p;p = P->next;q->next = (*list)->next; Equivalent to the pre-interpolation method to construct a new linked list, and the original opposite (*list)->next = q;}} /*----------------return length ———————————— */void length (list *list) {cout << "the length of list is:>" << (*list)- >data << Endl;} /*-----------------return to successor ———————————— */node* NEXT (List *list, elemtype key)//input node ordinal returns its successor {node* cur = (*list)->next;int count = 1;if (isEmpty (list) | | Key < 0 | | Key > (*list)->data) return False;while (Count < key) {cur = cur->next;count++;} if (count = = key) {cur = Cur->next;return cur;} Else{cout << "Erroe" << Endl;return false;}} /*-----------------return to precursor ———————————— */node* prio (List *list, Elemtype key) {node* pre = (*list);  node* cur = (*list)->next;int count = 1;if (isEmpty (list) | | Key < 1 | | key > (*list)->data) return False;while (Count < key) {pre = Pre->next;cur = cur->next;count++;} if (count = = key) {return pre;} Else{cout << "Erroe" << Endl;return false;}}

#include "List.h" int main (void) {List mylist;initlist (&mylist); Elemtype item;int pos = 0;int select = 1;int length = 0;while (select) {menu (); Cin >> Select;switch (SELECT) {Case 100 : cout << "Please input the length:>", Cin >> length;cout << "Please input the data:>"; CreateList (&mylist, length); Break;case 1:cout << "Please enter data:>"; Cin >> Item;push_back (& MyList, item); Break;case 2:cout << "Please enter data:>"; Cin >> Item;push_front (&mylist, item); ; case 3:showlist (mylist); Break;case 4:pop_back (&mylist); Break;case 5:pop_front (&mylist); Break;case 6:cout << "Please enter Position:>", cin >> pos;cout << "Please enter insert data:>"; CIN >> Item;ins Ert_pos (&mylist, POS, item) break;case 8:cout << "Please enter position:>"; Cin >> Pos;delete_pos ( &mylist, POS); break;case 9:cout << "Please enter delete data:>"; Cin >> Item;delete_val (&mylist, item); Break;case 10:cout << "Please enter data:>"; Cin >> item;cout << "The Piont of POS is:> << Find (&mylist, item) << endl;break;case 11:cout << "Please enter data:>"; Cin >> Item;co UT << "The number of POS is>" << locatepos (&mylist, item) << endl;break;case 12:cout << "ple ASE enter position:> "; Cin >> pos;cout <<" Please enter the Modify data:> "; Cin >> Item;modify (& MyList, POS, item); Break;case 13:destroylist (&mylist); Break;case 14:sort (&mylist); cout << "The changes of Seqlist'll be incremental! "<< Endl;  Showlist (mylist); Break;case: (mylist)->next = Resver (&mylist); cout << "The changes of List would be resver! "<< Endl; Showlist (mylist); reverse_2 (&mylist) cout << "The changes of List would be resver!" << Endl; Showlist (mylist); Break;case 16:length (&mylist) break;case 17:cout << "Please enter PositiOn:> "; Cin >> pos;if (pos <= 0 | | pos >= (mylist)->data)//Ensure the input location is correct {cout <<" ERROR pos "<< End L;break;} cout << "The Nextposdata is:>" << Next (&mylist, POS)->data;break;case 18:cout << "please Ente R position:> "; Cin >> Pos;if (pos <= 1 | | pos > (mylist)->data)//Guaranteed input location correct {cout <<" ERROR pos "<& Lt Endl;break;} cout << "The Preposdata is:>" << prio (&mylist, POS)->data;break;}} return 0;}







Implementation of single linked list of algorithm data structure + operation and comparison with sequential tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.