C language, data structure, linked list of some operations summary

Source: Internet
Author: User

The following are some of their own learning operations and summary, can use I will be very happy, there are shortcomings, welcome you to put forward valuable comments!

C language Linked list is a basic data structure, and the order table belongs to the linear table, but the sequential table in memory of the storage unit is continuous, so the memory requirements are relatively high, and the list is not the same, it can be a good solution to these shortcomings, the only disadvantage is that the efficiency of the access element is not as high as the order table, But it's only relative! Moreover, the linked list for the back of the two-tree, the figure is very important, the relationship between them is very close, I will give a list of some operations.

1, the storage structure of the list is composed of data and pointers to a node, each pointer to the next node, so that the interlocking form of the linked list

typedef int T;
Linked list nodes

typedef struct NODE
{
T data;
struct node* next;
}node,*link;

This is the node definition for the linked list.

2.

Creates a linked list that returns the head pointer.
Link Createlink ()
{
node* link= (node*) malloc (sizeof (Node));
link->next=null;
return link;

}

3.

Determine if the linked list is empty
BOOL IsEmpty (link)
{
Return link->next==null;
}

4.

Get the number of linked list elements
size_t size (link)
{
size_t num=0;
Link p=link->next;
while (P!=null)
{
num++;
p=p->next;
}
return num;
}

5.

Gets the previous node of index, where this function obtains a node that is the previous node in the specified position
node* getnode (Link link,int index)
{

Link P=link;

for (int i=1;i<=index;i++)
{
p=p->next;
}
return p;

}

6.

Insert element to specified position index starting at 0 to size ()
BOOL Insert (Link link,int index,t value)
{
if (link) | | index>size INDEX<0)
{
return false;
}
Link s= (node*) malloc (sizeof (Node)), q;

Q=getnode (Link,index);//Call the above function to get the node.
s->data=value;
s->next=q->next;
q->next=s;
return true;

}

7,//The following are inserted in the chain head or the end of the insertion element, you can also use the other index above 0 or for the list length can be!

BOOL Insertback (Link link,t value)
{
Link s= (node*) malloc (sizeof (Node));
s->data=value;
s->next=link->next;
link->next=s;
}
BOOL Insertfront (Link link,t value)
{
Link s= (node*) malloc (sizeof (Node));
Link R=link;
while (R->next!=null)
{
r=r->next;
}
s->data=value;
r->next=s;
R=s;
r->next=null;

}

8.

Update the node value under index for the linked list
BOOL Update (Link link,int index,t value)
{

if (index>size link) -1| | INDEX<0)
{
return false;
}
Link S=getnode (Link,index);
node* q=s->next;
q->data=value;
Free (q);
return true;
}

9.

Delete the element that specifies the subscript
BOOL Deletelink (Link Link,int index)
{
if (index>size link) -1| | INDEX<0)
{
return false;
}
node* S=getnode (Link,index);
node* q=s->next;
s->next=q->next;
Free (q);


}

10.

Remove all nodes of value
BOOL Deletedatas (Link link,t value)
{
node* node=link->next;
node* Prenode=link;
BOOL Flag=false;
while (Node!=null)
{
if (Node->data==value)
{
prenode->next=node->next;
node* Tmp=node;
node=node->next;
Free (TMP);
Flag=true;
Continue
}
Prenode=node;
node=node->next;

}
return true;
}

11.

Remove the first element of value
BOOL DeleteData (Link link,t value)
{

/*int Index=indexof (Link,value);
if ( -1==index)
{
return false;
}

Link S=getnode (Link,index);
node* q=s->next;
s->next=q->next;
Free (q);
Return true;*/
node* node=link->next;
node* Prenode=link;
while (Node!=null)
{
if (Node->data==value)
{
prenode->next=node->next;
Free (node);
return true;
}
Prenode=node;
node=node->next;
}
return false;


}

Find out if the value element exists
BOOL Isexists (Link link,t value)
{
if (IsEmpty (link)) return false;
node* node=link->next;
while (Node!=null)
{
if (Node->data==value)
{
return true;
}
node=node->next;
}
return false;

}
int indexof (Link link,t value)
{
node* node=link->next;
int index=0;
while (Node!=null)
{
if (Node->data==value)
{
return index;
}
index++;
node=node->next;
}
return-1;
}

12.

Traverse output
void Travel (link)
{
node* node=link->next;
while (Node!=null)
{
printf ("%d", node->data);
node=node->next;
}
printf ("\ n");
}

13.

Reverse
void reverse (link link)
{
node* Pprevnode=link;
if (link==null| | Link->next==null)
{
Return
}
Record a previous node
node* prevnode=link->next;
Record current node
node* node=prevnode->next;
As long as the current node exists
while (Node!=null)
{//Record the next node of the current node first
node* nextnode=node->next;
To point the next node of the current node to a previous node
node->next=prevnode;
Prevnode=node;
Node=nextnode;


}
Let the original first element become the tail element, the next empty of the tail element
link->next->next=null;
Point the head node of the list to the original tail element
link->next=prevnode;
}

14.

Empty list
void Clear (link)
{
node* node=link->next;
while (Node!=null)
{
node* Tmp=node;
node=node->next;
Free (TMP);
}
link->next=null;
}

Destroyed
void Destroy (link link)
{
Clear (link);
Free (link);
Link=null;
}

15, need to join the header file

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

Finally, what questions are welcome to ask questions and suggestions, but also welcome to add!

C language, data structure, linked list of some operations summary

Related Article

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.