# Basic operation of linked list (complete code)

2022-05-15 07:26:26

# Linked list basic operation （ Complete code ）

First, summarize the basic operations of single linked list （ Leading node ）

2. Printing of single linked list
4. Node insertion
（1） Head insertion （2） Insert at any position （3） Tail insertion
5. Node delete

For more details, please see Linked list basic operation （ Detailed explanation ）.

Complete code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{

int number;    // Student number
char name[20];  // full name
struct student *next;
};

struct student *init()  // Head node initialization
{

head = (struct student *)malloc(sizeof(struct student));

}

void Creat(struct student *head)  // Creation of linked list （ The tail interpolation ）
{

struct student *s,*r;
int num;
char name[20];
while(1)
{

scanf("%s",name);
scanf("%d",&num);
if(num <= 0)
{

break;
}
s = (struct student *)malloc(sizeof(struct student));
strcpy(s->name ,name);
s->number = num;
// The tail interpolation
r->next = s;
r = s;
}
s->next = NULL;
}

//void Creat(struct student *head) // Linked list creation ( The first interpolation )
//{

// struct student *s;
// int num;
// char name[20];
// while(1)
// {

// scanf("%s",name);
// scanf("%d",&num);
// if(num <= 0)
// {

// break;
// }
// s = (struct student *)malloc(sizeof(struct student));
// strcpy(s->name ,name);
// s->number = num;
// //
// }
//}

struct student *search(struct student *head, int pos)  // Inquire about  pos Query location
{

int n;
struct student *temp = head  ;
int i=0;
while(temp && i < pos)
{

temp = temp->next ;
i++;
}
if(temp == NULL)
{

printf("error\n");
}
return temp;
}

void print(struct student *head)  // Print
{

struct student *temp = head->next ;
printf("****** The information is as follows :******\n");
while(temp)
{

printf("%s\n",temp->name);
printf("%d\n",temp->number );
printf("\n");
temp = temp->next ;
}
}

void insert(struct student *head)   // Insert at any position
{

int n;
scanf("%d",&n);
struct student *s = p->next;
s = (struct student *)malloc(sizeof(struct student));
scanf("%s",&s->name );
scanf("%d",&s->number );
//
s->next = p->next ;
p->next = s;
}

//{

// struct student *s;
// s = (struct student *)malloc(sizeof(struct student));
// printf("please input the insert name:\n");
// scanf("%s",&s->name );
// printf("please input the insert number:\n");
// scanf("%d",&s->number );
// //
//}

//void insert (struct student *head) // Tail insertion
//{

// int i = 0;
// struct student *s,*r;
// while(r->next && r )
// {

// r = r->next ;
// i++;
// }
// s = (struct student *)malloc(sizeof(struct student));
// printf("please input the insert name:\n");
// scanf("%s",&s->name );
// printf("please input the insert number:\n");
// scanf("%d",&s->number);
// //
// r->next = s;
// s->next = NULL;
//}

{

struct student *temp = head->next ;
int cnt = 0;
while(temp)
{

temp = temp->next ;
cnt++;
}
printf(" altogether %d Members \n",cnt);
}

void Delete(struct student *head)  // Delete
{

struct student *p,*s;
int n;
printf("please input the delete the number:\n");
scanf("%d",&n);
s = p->next ;
if(p ==NULL || p->next ==NULL)
{

printf("error\n");
}
else
//
p->next = s->next ;
free(s);
}

int main()
{

int n;
struct student *head = init();   // initialization