Single-linked list initialization, build, insert, find, delete.

Source: Internet
Author: User

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

typedef int ELEMTYPE;

Defining node types
typedef struct NODE
{
Elemtype data; Data fields in a single-linked list
struct Node *next;Single-linked list of pointer fields
}node,*linkedlist;


Single-linked list initialization

LinkedList Linkedlistinit ()
{
Node *l;
L = (node *) malloc (sizeof (node));Application node Space
if (L = = NULL)Determine if there is enough memory space
printf ("Application memory space failed \ n");
L->next = NULL;Set next to null with a single linked list with an initial length of 0
return L;
}


The establishment of single-linked list 1, the head interpolation method to establish a single linked list

LinkedList Linkedlistcreath ()
{
Node *l;
L = (node *) malloc (sizeof (node));Apply for head node space
L->next = NULL; Initialize a list of empty lists

Elemtype x; X is the data in the Linked List data field
while (scanf ("%d", &x)! = EOF)
{
Node *p;
p = (node *) malloc (sizeof (node));Apply for a new node
P->data = x; Node data field Assignment
P->next = l->next; Inserting nodes into a table header L-->|2|-->|1|-->null
L->next = p;
}
return L;
}


The establishment of single-linked list 2, the tail interpolation method to establish a single-linked list

LinkedList Linkedlistcreatt ()
{
Node *l;
L = (node *) malloc (sizeof (node));Apply for head node space
L->next = NULL;Initialize a list of empty lists
Node *r;
R = L; R always points to the terminal node and starts with the head node.
Elemtype x; X is the data in the Linked List data field
while (scanf ("%d", &x)! = EOF)
{
Node *p;
p = (node *) malloc (sizeof (node));Apply for a new node
P->data = x; Node data field Assignment
R->next = p;Inserting nodes into a table header L-->|1|-->|2|-->null
r = P;
}
R->next = NULL;

return L;
}


Insertion of a single-linked list, inserting elements of X in the first position of the linked list

LinkedList Linkedlistinsert (LinkedList l,int i,elemtype x)
{
Node *pre; Pre is the precursor node.
Pre = L;
int tempi = 0;
for (tempi = 1; tempi < i; tempi++)
Pre = pre->next;Find the first node of the I position
Node *p; The inserted node is p.
p = (node *) malloc (sizeof (node));
P->data = x;
P->next = pre->next;
Pre->next = p;

return L;
}


Delete the single-linked list and delete the element with the value x in the linked list

LinkedList linkedlistdelete (linkedlist l,elemtype x)
{
Node *p,*pre;The pre is the precursor node and P is the node to find.
p = l->next;
while (P->data! = x) Find an element with a value of X
{
Pre = P;
p = p->next;
}
Pre->next = p->next;Delete the operation and point to its predecessor next to the subsequent relay.
Free (p);
return L;
}


int main ()
{
LinkedList List,start;
printf ("Please enter data for a single linked list:");
List = Linkedlistcreath ();
for (start = list->next; start! = NULL; start = Start->next)
printf ("%d", start->data);
printf ("\ n");

int i;
Elemtype x;
printf ("Please enter location for inserting data:");
scanf ("%d", &i);
printf ("Please enter values for inserting data:");
scanf ("%d", &x);
Linkedlistinsert (LIST,I,X);
for (start = list->next; start! = NULL; start = Start->next)
printf ("%d", start->data);
printf ("\ n");
printf ("Please enter the value of the element to be deleted:");
scanf ("%d", &x);
Linkedlistdelete (LIST,X);
for (start = list->next; start! = NULL; start = Start->next)
printf ("%d", start->data);
printf ("\ n");

return 0;
}

Single-linked list initialization, build, insert, find, delete.

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.