Realization of Student Information System with Linked List

#include<stdio.h>
#include<string.h>
typedef struct student
{
    int num;
    char name[10];
    float score[3];
}DataType;
typedef struct LNode
{
    DataType data;
    struct LNode *next;
}LNode,*LinkList;
//Create a linked list with a header - header insertion method 1
LNode* Creat_LinkList1()
{
    LinkList L;//Create a linked list
    LNode *p;//p as the node to be inserted
    int flag=0;
    printf("Please enter the student number, name and grades of the three subjects of the five students:\n");
    L->next=NULL;
    while(flag!=5)
    {
        flag++;
        p=new LNode;
        scanf("%d %s %f %f %f",&p->data.num,p->data.name,&p->data.score[0],&p->data.score[1],&p->data.score[2]);
        p->next=L->next;
        L->next=p;
    }
    return L;
}
//Create a linked list with head - tail insertion method 1
LNode* Creat_LinkList2()
{
    LinkList L=new LNode;//Create a linked list
    LNode *s,*p;//p as the node to be inserted, s as the tail node
    int flag=0;
    printf("Please enter the student number, name and grades of the three subjects of the five students:\n");
    L->next=NULL;
    s=L;
    while(flag!=2)
    {
        flag++;
        p=new LNode;
        scanf("%d %s %f %f %f",&p->data.num,p->data.name,&p->data.score[0],&p->data.score[1],&p->data.score[2]);
        s->next=p;
        s=p;
    }
    s->next=NULL;
    return L;
}
// find the length of the linked list 1
int Length_LinkList(LinkList L)
{
    LNode *p;
    p=L;
    int len ​​= 0;
    while(p->next)
    {
        p=p->next;
        len ++;
    }
    return len;
}
//find 1 by serial number
LNode* Get_LinkList(LinkList L,int i)
{
    LNode *p;
    int num=0;
    p=L;
    while((p->next)&& num<i)
    {
        p=p->next;
        num++;
    }
    return p;
}
// find 1 by value
LNode* Locate_LinkList(LinkList L,DataType x)
{
    LNode *p;
    p=L;
    while(p->next&&p->data.num!=x.num&&p->data.name!=x.name)
        p=p->next;
    return p;
}
//insert element 1
int Insert_LinkList(LinkList L,int i,DataType x)
{

    LNode *p,*s;//s is the node to be inserted
    p=Get_LinkList(L,i-1);//Find the predecessor of the i-th element
    if(p==NULL)
        return 0;//The i-1th node does not exist
    else
    {
        s=new LNode;
        s->data=x;
        s->next=p->next;
        p->next=s;
        return 1;
    }
}
// delete element 1
int Delete_LinkList(LinkList L,int i)
{
    LNode *p,*s;
    p=Get_LinkList(L,i-1);
    if(p==NULL)
        return -1;
    else
    {
        if(p->next==NULL)
            return 0;
        else
        {
            s=p->next;
            p->next=s->next;//Delete logically
            delete s;//Delete physically
            return 1;
        }
    }
}
//The linked list is inverted by 1
void Reverse_LinkList(LinkList L)
{
    LNode *p,*q;
    p=L->next;
    L->next=NULL;
    while(p)
    {
        q=p
        p=p->next;
        q->next=L->next;
        L->next=q;
    }
}
//delete the duplicate node 1 in the linked list
void Pur_LinkList(LinkList L)
{
    LNode *p,*q,*r;
    p=L->next;
    while(p)
    {
        q=p
        while(q->next)
        {
            if(p->data.num==q->next->data.num&&p->data.name==q->data.name)
            {
                r=q->next;
                q->next=r->next;
                delete r;
            }
            else
                q=q->next;
        }
        p=p->next;
    }
}
//Display all information in the linked list and all student information 1
void Print_LinkList(LinkList L)
{
    printf("Student number name, A, B, C, C\n");
    LNode *p;
    p=L->next;
    while(p)
    {
        printf("%d %7s %8.2f %9.2f %9.2f\n",p->data.num,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
        p=p->next;
    }
}
//Display the information of the nodes in the linked list and the information of a classmate 1
void Print_LNode(LNode *p)
{
    printf("%d %7s %8.2f %9.2f %9.2f\n",p->data.num,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
}
intmain()
{
    LinkList L;
    L=Creat_LinkList1();
    Print_LinkList(L);
    int flag;
    printf("Please enter what you need to do:\n");
    printf("1. Count the total number of students\n");
    printf("2. Find student information by location\n");
    printf("3. Find student information by student number and name\n");
    printf("4. Insert student information\n");
	printf("5. Delete student information\n");
	printf("6. Student information in reverse order\n");
	printf("7. Delete duplicate student information\n");
	scanf("%d",&flag);
	if(flag==1)
    {
        printf("The information of %d students is stored in the linked list.\n",Length_LinkList(L));
    }
    if(flag==2)
    {
        printf("Please enter the location where you want to find student information:\n");
        int site;
        scanf("%d",&site);
        LNode *p;
        p=Get_LinkList(L,site);
        if(p==NULL)
            printf("No information found for this student!");
        else
        {
            printf("The student number, name and grades of each class of the %d student are:\n",site);
            Print_LNode(p);
        }
    }
    if(flag==3)
    {
        LNode *p;
        DataType d1;
        printf("Please enter student number and name:\n");
        scanf("%d %s",&d1.num,d1.name);
        p=Locate_LinkList(L,d1);
        if(p==NULL)
            printf("No information found for this student!");
        else
        {
            printf("The student number, name and grades of each class are:\n");
            Print_LNode(p);
        }
    }
    if(flag==4)
    {
        DataType d2;
        int location,flag1=0;
        printf("Please enter the location and information of the student you want to insert:\n");
        scanf("%d %d %s %f %f %f",&location,&d2.num,d2.name,&d2.score[0],&d2.score[1],&d2.score[2]);
        //flag1=Insert_LinkList(L,location,d2);
        if(flag1==0)
            printf("Invalid insertion position!");
        else
            printf("Insert successfully!");
        Print_LinkList(L);
    }
    if(flag==5)
    {
        int flag2,site1;
        printf("Please enter the location where you want to delete student information:\n");
        scanf("%d",&site1);
        flag2=Delete_LinkList(L,site1);
        if(flag2==-1||flag2==0)
            printf("The deleted location is invalid.\n");
        else
            printf("Deleted successfully!\n");
        Print_LinkList(L);
    }
    if(flag==6)
    {
        Reverse_LinkList(L);
        Print_LinkList(L);
    }
    if(flag==7)
    {
        Pur_LinkList(L);
        Print_LinkList(L);
    }
    return 0;
}

Guess you like

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