C language singly linked list creation, output, insert node, delete node

Linked lists are a big difficulty for learners of C language, but linked lists are the basis for learning data structures well. Here, the creation, output, node insertion, and node deletion of a singly linked list are briefly introduced.

1. First define an array of structures

#define LEN sizeof(struct student)
struct student
{
	int num;
	char name[20];
	float score;
	struct student *next;
};
int n;

2, the creation of a singly linked list

 

struct student *creat(void)//Create linked list
{
	n=0;
	struct student *p1,*p2,*head;
	head=NULL;
	p1=p2=(struct student *)malloc(LEN);//Open up a new unit
	scanf("%d,%s,%f",&p1->num,&p1->name,p1->score);//input data
	while(p1->num!=0)//Limited when num is 0, the linked list input ends
	{
		n++;
		if(n==1)head=p1;
		else
		p2->next=p1;
		p2=p1;
		p1=(struct student *)malloc(LEN);
	    scanf("%d,%s,%f",&p1->num,&p1->name,p1->score);
	 }
	 p2->next=NULL;
	 return head;
}
3. Output of singly linked list

     

void print(struct student *head)//Output linked list
{
	printf("\nthere are %d records\n",n);
	struct student *p;
	p=head;
	if(head!=NULL)
		do
		{
			printf("%d,%s,%.0f\n",p->num,p->name,p->score);
			p=p->next;
			
		}
		while(p!=NULL);
}

4, singly linked list node deletion

struct student *del(struct student *head,int num)//Delete a node
{
	struct student *p,*p1;
	p=head;
	if(head==NULL)//When the linked list is empty
	{
		printf("this table is null\n");
		goto end;
	}
	while(num!=p->num&&p->next!=NULL)//Determine whether the node exists in the linked list
	{
		p1=p;
		p=p->next;
	}
	if(num==p->num)//When the node exists
	{
		if(p==head)head=p->next;
		else
		p1->next=p->next;
		printf("delete\n");
		n--;//Number of nodes in the linked list
	}
	else
	printf("not find");//When the node is not found
end: return head;
}

5. Insertion of singly linked list

struct student *insert(struct student *head,struct student *stud)//Insert a node
{
	struct student *p0,*p1,*p2;
	p1=head;
	p0=stud;
	if(head==NULL)
	{
		head=p0;
		p0->next=NULL;
	}
	else
	{
		while((p0->num>p1->num)&&(p1->next!=NULL))//Determine the position of the linked list
		{
			p2=p1;
			p1=p1->next;
		}
		if(p0->num<=p1->num)
		{
			if(head==p1)head=p0;
			else p2->next=p0;
			     p0->next=p1;
		}
		else
		{
			p1->next=p0;
			p0->next=NULL;
		}	
	}
	n++;
	return(head);
 }

6. main main function

int main(int argc, char *argv[]) {
struct student *head,*stu;
int del_num;
printf("input record:\n");//Create linked list
head=creat();
print(head);//Output linked list
printf("input the deleted number:\n");//Delete the linked list
scanf("%d",&del_num);//Enter the record to delete
while(del_num!=0)
{
	head=del(head,del_num);
	print(head);
    printf("input the deleted number:\n");
    scanf("%d",&del_num);
}
printf("input the inserted record:\n");//Insert node
stu=(struct student *)malloc(LEN);
scanf("%d,%s,%f",&stu->num,&stu->name,&stu->score);
while(stu->num!=0)
{
	head=insert(head,stu);
	print(head);
	printf("input the inserted record:\n");
    stu=(struct student *)malloc(LEN);
    scanf("%d,%s,%f",&stu->num,&stu->name,&stu->score);
}
}
Note: The content comes from the second edition of Tan Haoqiang's C language, with minor corrections made

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324529836&siteId=291194637