C language linked list basic operation of with the creation of linked list delete print insert

  • 2020-04-02 02:05:24
  • OfStack


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
    long num;
    float score;
    struct Student*next;
};
int n;
int main()
{
    
//Function declaration
    struct Student* create();// For creating dynamic linked lists Function declaration , the function type is student Struct type, returns a header pointer 
    struct Student* del(struct  Student* ,long);// Deletes a node at the specified location Function declaration, Parameter: chain header node + Delete node location + Return header pointer 
    struct Student* insert(struct Student*,struct Student*);// Insert a Student Type data Function declaration
    void   print(struct Student*);// Output the data in the linked list Function declaration , the parameter is the header pointer of the linked list 
//Define variables
    struct Student *head,*stu;//Defines a header pointer to a dynamic linked list with a new node
    long del_num;
//Establish a linked list operation
    printf("Input records:n");
    head = create();//Creates a linked list and returns a header pointer
    print(head);//Output all nodes
//Delete node operation
    printf("nInput the deleted number:");
    scanf("%ld",&del_num);
    while(del_num!=0)//The loop ends when the student number is 0
    {
        head = del(head,del_num);//Returns the header address of the list after the node is deleted
        print(head);//Output all nodes
        printf("Input the deleted number:");
        scanf("%ld",&del_num);
    }
//Insert node operation
    printf("nInput the inserted number:");
    stu=(struct Student*)malloc(LEN);//Every time a node is inserted, a new node needs to be created
    scanf("%ld %f",&stu->num,&stu->score);
    while(stu->num!=0)//The loop ends when the student number entered is 0
    {
        head = insert(head,stu);//Returns the header address of the linked list
        print(head);
        printf("nInput the inserted number:");
        stu = (struct Student*)malloc(LEN);
        scanf("%ld %f",&stu->num,&stu->score);
    }
    return 0;
}
//A function that creates a linked list
struct  Student* create()
{
    struct Student *head;
    struct Student *p1,*p2;
    n=0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%ld %f",&p1->num,&p1->score);
    head=NULL;
    while(p1->num!=0)
    {
        n++;
        if(n==1)head=p1;
        else p2->next=p1;//When first executed, this step is to point the header pointer to itself, and when n starts at 2, this step is used to point p2 to the next element
        p2=p1;//Point p2 and p1 to the same storage area
        p1=(struct Student*)malloc(LEN);//Open up dynamic storage and force a pointer of type struct Student to be returned
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return (head);
}
//Delete node function
struct Student* del(struct  Student* head,long num)
{
    struct Student *p1,*p2;
    if(head==NULL)
    {
        printf("List null!n");
        return (head);
    }
    p1=head;
    while(num!=p1->num && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
        {
            head=p1->next;
        }
        else
        {
            p2->next=p1->next;
        }
        printf("Delete:%ldn",num);
        n=n-1;
    }
    else
    {
        printf("%ld not been found!",num);
    }
    return (head);
}
//The function that inserts a node
struct   Student* insert(struct Student* head,struct Student * stud)
{
    struct Student *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)//The original linked list was an empty table
    {
        head=p0;p0->next=NULL;//Empty table when the inserted node as the header node
    }
    else//If it is not an empty table, it iterates to find the appropriate insertion location
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))//Insert in student number order. If the number of student number inserted is large, it should be pushed backward
        {
            p2=p1;
            p1=p1->next;//Move backward
        }
    }
    if(p0->num<=p1->num)//I'm going to find the insertion position, and the insertion position is before the position that p1 is pointing to, which is the position that p2 is pointing to
    {
        if(head==p1)head=p0;//If the insertion position is before the head position, make the head point to p0
        else p2->next=p0;//If not before the header position, make the next pointer to p2 point to the inserted data address, or p0
        p0->next=p1;//Make the next pointer of p0 point to p1 to complete the data addition
    }
    else//Insert the student number in the last position
    {
        p1->next=p0;
        p0->next=NULL;
    }
    n=n+1;//Number of records plus one
    return(head);
}
//A function that outputs a linked list
void print(struct Student * head)
{
    struct Student * p;
    printf("Now,These %d records are:n",n);
    p=head;
    if(head!=NULL)
    do
    {
        printf("%ld %5.1fn",p->num,p->score);
        p=p->next;
    }while(p!=NULL);
}


Related articles: