c program void addLast

closed account (yC4jURfi)
Hello, does anyone know how to use the void addLast operation in C? I cant use it with my current problem because I'm confuse on how to code it. the program i'm doing is about student records and here is my code, I would like to add the addLast operation in the choices

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  #include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct Student
{
    int number;
    char name[24];
    char crsyr[10];
    float GWA;
    struct Student *next;

}* head;
void insert(int number, char* name, char* crsyr, float GWA)
{

    struct Student * student = (struct Student *) malloc(sizeof(struct Student));
    student->number = number;
    strcpy(student->name, name);
    strcpy(student->crsyr, crsyr);
    student->GWA = GWA;
    student->next = NULL;

    if(head==NULL){

        head = student;
    }
    else{

        student->next = head;
        head = student;
    }

}
void search(int number)
{
    struct Student * temp = head;
    while(temp!=NULL){
        if(temp->number==number){
            printf("Student Number: %d\n", temp->number);
            printf("Student Name: %s\n", temp->name);
            printf("Student Course & Year: %s\n", temp->crsyr);
            printf("GWA: %0.4f\n", temp->GWA);
            return;
        }
        temp = temp->next;
    }
    printf("Student with number %d is not found !!!\n", number);
}
void update(int number)
{

    struct Student * temp = head;
    while(temp!=NULL){

        if(temp->number==number){
            printf("Record with student number %d Found !!!\n", number);
            printf("Enter new name: ");
            scanf("%s", temp->name);
            printf("Enter new course & year: ");
            scanf("%s", temp->crsyr);
            printf("Enter new GWA: ");
            scanf("%f",&temp->GWA);
            printf("Updation Successful!!!\n");
            return;
        }
        temp = temp->next;

    }
    printf("Student with number %d is not found !!!\n",number);

}
void Delete(int number)
{
    struct Student * temp1 = head;
    struct Student * temp2 = head;
    while(temp1!=NULL){

        if(temp1->number==number){

            printf("Record with student number %d Found !!!\n", number);

            if(temp1==temp2){

                head = head->next;
                free(temp1);
            }
            else{

                temp2->next = temp1->next;
                free(temp1);
            }

            printf("Record Successfully Deleted !!!\n");
            return;

        }
        temp2 = temp1;
        temp1 = temp1->next;

    }
    printf("Student with number %d is not found !!!\n", number);

}

int main()
{
    head = NULL;
    int choice;
    char name[24];
    char crsyr[10];
    int number;
    float GWA;
    printf("1 to create student record\n2 to traverse for student record\n3 to delete student record\n4 to add student record");
    do
    {
        printf("\nEnter Choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
                printf("Enter Student number: ");
                scanf("%d", &number);
                printf("Enter Student name: ");
                scanf("%s", name);
                printf("Enter Student Course & Year: ");
                scanf("%s", crsyr);
                printf("Enter GWA: ");
                scanf("%f", &GWA);
                insert(number, name, crsyr, GWA);
                break;
            case 2:
                printf("Enter Student number to search: ");
                scanf("%d", &number);
                search(number);
                break;
            case 3:
                printf("Enter Student number to delete: ");
                scanf("%d", &number);
                Delete(number);
                break;
            case 4:
                printf("Enter Student number to update: ");
                scanf("%d", &number);
                update(number);
                break;
        }

} while (choice != 0);

}
justinejerome wrote:
here is my code


So you made minuscule changes to this code:
https://slaystudy.com/student-management-system-using-linked-list-in-c/

Look at the function insert() and amend that so that it adds at the end rather than the start.
Last edited on
Topic archived. No new replies allowed.