Linear chain-type storage implementations

Source: Internet
Author: User

Basic operations

node* Applynode (); Assigning nodes
node* Addnodeh (node* head,node* insertnode); Adding nodes to the head
node* Addnodet (node* head,node* insertnode); Add a node at the tail
node* Addnodesort (node* Head, node* insertnode); Add nodes in ascending order

node* createlist (int n,int choose); Construct linked list
void Printlist (Node*head); Print linked list
void FreeList (node*& Head); Release linked list
int Numofnodes (node* Head); Table length (number of nodes)

node* Locatenodei (Node*head,int i); Positioning
int SearchList (node*head,int value); Find
BOOL Insertnodei (node* Head, int i); Insert
BOOL Deletenodei (Node*&head,int i); Delete
void Sortlist (node*& Head); Sort

1. Construction node

Defining node types

struct node{    int value;    Node*next;};

2. Assigning nodes

Allocate memory and initialize the node in a function node* Applynode () {  node* newNode;  if ((NewNode = (node*) malloc (sizeof (Node))) ==null)  {    cout << "failed to allocate memory!" << Endl;    Exit (0);  }  CIN >> newnode->value;  Newnode->next = NULL;  return newNode;}

3. Adding nodes to the head

node* Addnodeh (node* Head) {     node* Insertnode = Applynode ();   if (head==null)      Head = Insertnode;   else   {      insertnode->next = NULL;      Head->next = Insertnode;   }   return Head;}

4. Adding nodes at the tail

node* Addnodet (node* Head) {   node* Insertnode = Applynode ();   if (head==null)      Head=insertnode;   else   {      node* p=head;      while (p->next!=null)         p=p->next;      P->next = Insertnode;   }   return Head;}

5. Add nodes in ascending order

node* Addnodesort (node* Head) {   node* Insertnode = Applynode ();//Allocation node   if (head==null)   {      head= Insertnode;   }   else   {      node* p=head;      while ((P->value) < (insertnode->value) && p->next!=null)         p=p->next;      if ((P->value) >= (insertnode->value))      {         Insertnode->next = p->next;//First add node after P         p-> Next = Insertnode;         Swap (P->value, insertnode->value); Swap the value of P and Insertnode      }      else//Because (P->next==null) and exit the loop! Represents the increment node at the tail      {         p->next = Insertnode;      }   }   return Head;}

6. Construction Chain List

Create n-node linked list//choose=0: Add in table header,  choose=1: Add at Footer,  choose=2: Add node* createlist (int n, int choose) in ascending order of value values {   Node *head=null, *p=null;   for (int i=0; i<n; i++)   {        p = applynode ();//Allocation node        cin >> Choose;        Switch (choose)        {case        0: Head             = Addnodeh (head, p);//head plug break             ;        Case 1:             head = Addnodet (head, p);//end plug break             ;        Case 2:             head = Addnodesort (head, p);//Insert break in ascending order             ;        Default:             printf ("Default");             break;   }   return Head;}

7. Print Chain list

Iterate through the list and output void Printlist (node* Head) {   node* p=head;   while (P!=null)   {      cout << p->value << ",";      p=p->next;   }   cout << "NULL" << Endl;}

8. Release the linked list

void FreeList (node* Head) {   node* tmp=head;   while (Tmp!=null)   {      Head = head->next;      Free (TMP);      TMP = Head;   }   Head=null; }

9, the table length (number of nodes)

Find the number of nodes int numofnodes (node* head) {        node* temp = Head;   int count=0;   while (Temp!=null)   {      count++;      temp=temp->next;   }   return count;}

10. Positioning

Position (point) I node, I starting from 1 node* locatenodei (node* Head, int i) {   node* pos=null;   int count = Numofnodes (Head);   if (i<=0 | | i>count)   {      cout << "position out of bounds! "<< Endl;   }   else    {      pos=head;      for (int j=1; j<i; j + +)          pos=pos->next;   }   return POS;}

11. Find

1) Search by ordinal: findkth

List *findkth (int K, list *ptrl) {    list *p = Ptrl;    int i = 1;    while (P!=null && i< k)    {        p = p->next;        i++;    }    if (i==k)        return p;//Find First k, return pointer    else return        NULL;}

2) Search by value: Find

List *find (ElementType X, list *ptrl) {    list *p = Ptrl;    while (P!=null && p->data!=x)        p = p->next;    return p;}

3) Search by value, return its ordinal number: SearchList

Finds the value and returns the first occurrence of the value//if you need to reference its pointer, you can then locate the position int searchlist (node* Head, int value) {   node* p=head;   int pos=0;   BOOL Find=false;   while (P!=null)   {      pos++;      if (P->value==value)      {         find=true;         break;      }      p=p->next;   }   if (find)      return POS;   else       return-1;}

12. Insert

The new node is inserted into a location Ibool Insertnodei (node* Head, int i) {   node* s = Applynode ();   if (i==1)//new node inserted in table header   {      s->next = head;      Head = s;      return true;   }   node* p = Locatenodei (Head, i-1);   if (p==null)   {      return false;   }   else   {      S->next = p->next;      P->next = s;      return true;   }}

13. Delete

Delete a node of position i bool Deletenodei (node* head, int i) {   node* s = Locatenodei (Head, i);   if (s==null)   {      return false;   }   else   {      if (s==head)//The head node to be removed      {head         = s->next;         Free (s);      }      else      {         node* p = Locatenodei (Head, i-1);//positioning the previous node, must exist         P->next = s->next;         Free (s);      }      return true;   }}

14. Sorting

List Sort//method: Only Exchange the value, do not break the list structure void Sortlist (node* head) {   int count = Numofnodes (head);   if (count==0 | | count==1)      return;      Bubble sort   bool Exchange;   for (int i=2; i<=count; i++)   {         exchange=false;      for (int j=count;j>=i; j--)      {         node* p1 = Locatenodei (Head, j);         node* P2 = Locatenodei (Head, j-1);         if (P1->value < P2->value)         {            exchange=true;            Swap (P1->value, p2->value); Exchange Data         }      }      if (!exchange) break         ;}   }

Linear chain-type storage implementations

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.