The insertion, deletion and lookup _c language of the single linked list in C language

Source: Internet
Author: User

A

Single linked list is a chain-access data structure that holds data elements from a linear table in a set of storage units with arbitrary addresses. In order to realize the function of inserting, deleting and searching the nodes in the single linked list, we should initialize, create and traverse the single linked list, and then realize the function, the following is the concrete realization of inserting, deleting and searching function of the single linked table node:

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef int ELEMTYPE;
 /** * List Universal type *elemtype represents a custom data type *struct Node *next represents the structure body pointer (pointing to the next structure, completing the linked list action)/typedef struct node{data;
struct Node *next; 

}node; /*========== single Linked list initialization ================*/* * Header node pointer data field set to null/void Initlist (Node **pnode) {*pnode=null;}/*=========== single linked list
 Create =================*//* Function implementation: Through the user input data, create a linked list * using the cursor two pointers (P1,P2), the requested data block (into the user input data), link Up/Node *create (node *phead) {
 Node *p1;
 Node *P2;     P1=p2= (node *) malloc (sizeof (node));       Application memory Space memset (p1,0,sizeof (Node));
 Empties scanf ("%d", &p1->data) into the data field;          
 p1->next=null;
  while (p1->data>0) {//Enter negative End if (phead==null) phead=p1;
  else p2->next=p1;
  P2=P1;
  p1= (node *) malloc (sizeof (node));
  memset (p1,0,sizeof (Node));
  scanf ("%d", &p1->data);
 p1->next=null;
return phead; /*================= the traversal of the linked list ==================*//** * Start from the beginning, constantly traversing the contents of the data field to traverse the table/voidPrintlist (Node *phead) {if (null==phead) printf ("list is empty \ n");
   else{while (phead!=null) {printf ("%d", phead->data);
  phead=phead->next;
printf ("\ n"); /*=============== Insert node ==================*//** *node **pnode Incoming header node space address *int I incoming node location to insert/void Insert_data (node **pn
 Ode,int i) {Node *temp;
 Node *target;
 Node *p;
 int item;
 int j=1;
 printf ("Enter the node value to insert:");
 scanf ("%d", &item);             
 Target=*pnode;  for (; j<i-1;target=target->next,++j);   Move the target position constantly, to the point where you want to insert the node, temp= (node *) malloc (sizeof node);       Request Memory Space temp->data=item;        
 Save in the data location you want to deposit p=target->next;
 target->next=temp; 
temp->next=p; /*=============== Delete Node ====================*//** * Release memory Free (temp) */void Delete_data (node **pnode,int i) {Nod
 e *target;
 Node *temp;
 int j=1;
 Target=*pnode;
 for (; j<i-1;target=target->next,++j);
 temp=target->next;
 target->next=temp->next;
Free (temp);
/*=============== Lookup Node ====================*/int search_data (node *pnode,int elem) {node *target;
 int i=1;
 For (Target=pnode;target->data!=elem && target->next!=null;++i,target=target->next);
 if (Target->next==null) return 0;
 
else return i;
 int main () {int i;
 Node *phead=null;
 Initlist (&phead);
 Phead=create (Phead);
 Printlist (Phead);
 printf ("Input insert node location \ n");
 scanf ("%d", &i);
 Insert_data (&phead,i);
 Printlist (Phead);
 printf ("Enter delete node location \ n");
 scanf ("%d", &i);
 Delete_data (&phead,i);
 Printlist (Phead);
 printf ("Input lookup node \ n");
 scanf ("%d", &i);
 printf ("Node location:%d", Search_data (Phead,i));
return 0;
 }

Through the implementation of the above functions, I hope that the single linked list of learning to help.

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.