current position:Home>Basic operation of linked list (complete code)
Basic operation of linked list (complete code)
2022-05-15 07:26:26【Dream chasing Jenny turtle】
Linked list basic operation ( Complete code )
First, summarize the basic operations of single linked list ( Leading node ):
1. Single linked list creation
(1) Create linked list by head insertion (2) Tail interpolation method to create linked list
2. Printing of single linked list
3. Single linked list query
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
{
struct student *head;
head = (struct student *)malloc(sizeof(struct student));
head->next = NULL;
return head;
}
void Creat(struct student *head) // Creation of linked list ( The tail interpolation )
{
struct student *s,*r;
r = head;
int num;
char name[20];
while(1)
{
printf("please input the name:\n");
scanf("%s",name);
printf("please input the number:\n");
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)
// {
// printf("please input the name:\n");
// scanf("%s",name);
// printf("please input the number:\n");
// scanf("%d",&num);
// if(num <= 0)
// {
// break;
// }
// s = (struct student *)malloc(sizeof(struct student));
// strcpy(s->name ,name);
// s->number = num;
// //
// s->next = head->next ;
// head->next = s;
// }
//}
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;
printf("please input the insert number:\n");
scanf("%d",&n);
struct student *p = search(head,n-1);
struct student *s = p->next;
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 );
//
s->next = p->next ;
p->next = s;
}
//void insert (struct student *head) // Head insertion
//{
// 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 );
// //
// s->next = head->next ;
// head->next = s;
//}
//void insert (struct student *head) // Tail insertion
//{
// int i = 0;
// struct student *s,*r;
// r = head;
// 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;
//}
void length(struct student *head)
{
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);
p = search(head,n-1);
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
Creat(head); // Linked list creation (2 Kind of )
print(head); // Print
length(head); // Chain length
printf("please input the search number:\n");
scanf("%d",&n);
struct student *p = search(head,n);
printf("%s %d\n",p->name ,p->number );
insert(head); // List insertion (3 Kind of )
print(head);
Delete(head); // Delete
print(head);
}
copyright notice
author[Dream chasing Jenny turtle],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102142242075.html
The sidebar is recommended
- Redis -- realize the like function
- Redisson
- What are the problems with NoSQL?
- What aspects does the class database of real-time data warehouse include?
- [MQ] achieve mq-08- configuration optimization from scratch fluent
- Rongyun x dsport: win the first drop of blood of game social "real-time community"
- Druid source code reading 5 -- discuss why Druid doesn't use atomiclong
- Construction practice camp - graduation summary
- [module] environment variable management tool module installation and use
- Kernel heap bypass SMEP, SMAP & modprobe_ path
guess what you like
Getting started with mqtt: online debugging and connecting to alicloud
Leetcode deletes the penultimate node of the linked list
Design of Web rights management (1) -- Analysis of key points of design (4)
Tencent cloud database growth index ranked first
Three ways! Go error handling best practices
Live broadcast at 20:00 tomorrow night | open the box Tencent cloud: play lighthouse, novice friendly and light-weight cloud!
New study conclusion: sleeping more than 2 hours on weekends reduces the risk of depression by 48%
Retinal vessel segmentation based on u-net model
Applet X-axis fade in animation component
Explanation of wechat applet interface call (accesstoken, applet code login)
Random recommended
- Single cell column - how to give orig Ident, change your name
- Fonts best practices
- Wonderful express | April issue of Tencent cloud database
- Illustration: what is the difference between layer 2 and layer 3 switches?
- Activity Notice | timing adjustment of 2022 deterministic network technology and Innovation Summit
- In order to seize the capacity of 5nm chips, AMD will pay an advance payment of US $6.5 billion to TSMC, grofangde and other suppliers; Germany will adopt stricter antitrust rules for Google meta
- It is reported that TSMC will promote the 1.4 nm process next month; Taobaoyuan universe trademark rejected
- Online binary 8-hexadecimal conversion tool
- [paper notes] epsanet: an efficient pyramid sequence attention block on revolutionary neural network
- IndexError: shape mismatch: indexing tensors could not be broadcast together with shapes [2], [3]
- What are the development stages of time series database in recent years?
- What are the shortcomings of the data model processed in the first stage of time series database?
- What are the shortcomings of the data model processed in the second stage of time series database?
- What are the development trends of time series database?
- What are the characteristics of cloud native multimode database lindorm?
- What are the functions of cloud native multimode database lindorm?
- Variance, standard deviation, mathematical expectation
- Two dimensional Gaussian distribution
- Collaborative process and channels (CSP: kotlin, golang)
- SQLite3 custom function (UDF)
- SQLite3 minimalist Tutorial & go operating data structures using SQLite memory mode
- Penetration test - DNS rebinding
- The pytoch loading model only imports some layer weights, that is, it skips the method of specifying the network layer
- Parameter and buffer in pytoch model
- torch. nn. functional. Interpolate function
- Specify the graphics card during pytorch training
- [paper notes] Dr TANet: dynamic receptive temporary attention network for street scene change detection
- [MQ] achieve mq-08- configuration optimization from scratch fluent
- New signs are taking place in the Internet industry, and a new transformation has begun
- ACL 2022 | visual language pre training for multimodal attribute level emotion analysis
- Cvpr2022 | latest progress in small sample behavior recognition strm framework, spatio-temporal relationship modeling is still the top priority
- Hallucinations in large models
- Is it safe to open an account online? Which of the top ten securities companies are state-owned enterprises?
- [encapsulation tips] encapsulation of list processing function
- Start with Google sea entrepreneurship accelerator - recruitment and start
- Hard core preview in May! Lecture tomorrow night: virtio virtualization technology trend and DPU practice | issue 16
- Druid source code reading 1 - get connection and release connection
- Graduation summary of actual combat training camp
- Public offering "imported products" temporarily hit the reef? The first foreign-funded public offering BlackRock fund has a lot of bad thoughts or a lot of things. It is acclimatized and the performance of the two products is poor
- Introduction and installation of selenium module, use of coding platform, use of XPath, use of selenium to crawl JD product information, and introduction and installation of sketch framework