Home > Software design >  There is problem in my linked list. The program cannot read other position, it only read position 1
There is problem in my linked list. The program cannot read other position, it only read position 1

Time:11-19

The problem with my code is that it cannot read the linked list when I want to update the info of position 2 or 3 and so on. It only reads position 1. Here is my code.

  #include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

typedef struct Student{
    int studno; // student number
    char name[20]; // Student name
    float per; // GWA
    struct Student *next; // next student
}Student;

Student *head;

int count(Student *h);
void create(Student *h); // Add Student info
void deleteByPos(Student *h); // Delete Student info
void updateByPos(Student *h); // Edit Student info
void searchByPos(Student *h);// Search student on the list
void display(Student *h); // View all student's info
void printline();

int main(){
    int ch;
    do {
        system("cls");
        printline();
        printf("\t   STUDENT MANAGEMENT SYSTEM\n");
        printline();
        printf("1. ADD LIST\n");
        printf("2. DELETE LIST\n");
        printf("3. EDIT BY POSITION\n");
        printf("4. SEARCH BY POSITION\n");
        printf("5. VIEW ALL\n");
        printf("0. EXIT\n");
         printline();
        printf("Enter you choice: ");
        scanf("%d", &ch);

        switch(ch){
            case 1: create(head);
                    break;
            case 2: deleteByPos(head);
                    break;
            case 3: updateByPos(head);
                    break;
            case 4: searchByPos(head);
                    break;
            case 5: display(head);
                    break;
            case 0: printf("Thank you \nHave a Nice Day!");
                    break;
        }
        printf("\n\n");
        system("pause");
    }while(ch !=0);
}

void create(Student *h){
    int studno;
    char name[20];
    float per;
    printline();
    printf("Enter Student Number : ");
    scanf("%d", &studno);
    fflush(stdin);
    printf("Enter Student Name : ");
    scanf("%[^\n]s", &name);
    printf("Enter Student GWA : ");
    scanf("%f", &per);
    if(h==NULL){
        head = (Student*) malloc(sizeof(Student));
        head -> studno = studno;
        head -> per = per;
        strcpy(head -> name,name);
        head -> next = NULL;
    }
    else {
        while(h->next != NULL)
            h = h-> next;
            h-> next =(Student*) malloc(sizeof(Student));
            h-> next -> studno = studno;
            strcpy(h-> next ->name, name);
             h-> next -> per = per;
            h-> next -> next = NULL;
    }
}
int count(Student *h){
    int cnt=0;
    while (h != NULL){
      h = h ->next;
      cnt  ;
    }
    return cnt;
}

void deleteByPos(Student *h){
    int n = count(h);
    int pos, i;
    Student *tmp;
    printf("Enter Position to Delete: ");
    scanf("%d", &pos);

    if(pos>n){
        printf("\nINDEX OUT OF BOUNDS");
    }else if(pos==1){ // To delete from first position
        tmp = h;
        head = h->next;
        free(tmp);
        printf("\nRECORD SUCCESSFULLY DELETED");
    } else if (pos>0){
        for (i=1; 1<pos-1; i  )
            h = h ->next;
        tmp = h -> next;
        h -> next = h -> next -> next;
        free(tmp);
         printf("\nRECORD SUCCESSFULLY DELETED");
    }
}

void updateByPos(Student *h){
    int n = count(h);
    int pos, i;
    printf("Enter Position to Edit: ");
    scanf("%d", &pos);

    if(pos>n){
        printf("\nINDEX OUT OF BOUNDS");
    }else if(pos>0){
        char name[20];

        for(i=1; 1<pos;i  )
           h = h -> next;
            printline();
            fflush(stdin);
            printf("Enter New Name: ");
            scanf("%[^\n]s",h->name);
            printf("Record Updated Successfully");
    }
}

void searchByPos(Student *h){
    int n = count(h);
    int pos, i;
    printf("Enter Position to Search: ");
    scanf("%d", &pos);

    if(pos>n){
        printf("\nINDEX OUT OF BOUNDS");
    }else if(pos>0){
        for(i=1; 1<pos;i  )
            h= h -> next;
            printline();
            printf("\t STUDENT RECORD FOUND \n");
            printline();
            printf("%-10d%-20s%f\n", h->studno, h->name, h->per);
            printline();
    }
}

void display(Student *h){
    printline();
    printf("%-10s%-20s%s\n", "Stud No.", " Name", "Percentage");
     printline();
    while(h != NULL){
        printf("%-10d%-20s%.2f\n", h->studno, h->name, h->per);
        h = h ->next;
    }
     printline();
}

void printline(){
    int i;
    for(i=0; i<50; i  )
        printf("-");
        printf("\n");
}

For example:

Let us say, The user inputs 4 students


       STUDENT MANAGEMENT SYSTEM

  1. ADD LIST
  2. DELETE LIST
  3. EDIT BY POSITION
  4. SEARCH BY POSITION
  5. VIEW ALL
  6. EXIT

Enter your choice: 5

--------------------------------------------------
Stud No.   Name               Percentage
--------------------------------------------------
1         lisa                97.00
2         jisoo               98.00
3         jennie              99.00
4         rose                100.00
--------------------------------------------------

Press any key to continue . . .

The user wants to edit position 2

it becomes like this, the program should ask "Enter New Name:" and print "Record Updated Successfully"

--------------------------------------------------
           STUDENT MANAGEMENT SYSTEM
--------------------------------------------------
1. ADD LIST
2. DELETE LIST
3. EDIT BY POSITION
4. SEARCH BY POSITION
5. VIEW ALL
0. EXIT
--------------------------------------------------
Enter you choice: 3
Enter Position to Edit: 2

Process returned -1073741819 (0xC0000005)   execution time : 82.339 s
Press any key to continue.

It is the same problem in search function. I don't have a problem with other functions aside from the update and search functions. How to fix this?

CodePudding user response:

You have a typo 1 instead of i in your loop condition (both in updateByPos() and searchByPos()). I also moved the int i declaration into the loop:

        for(int i=1; i<pos; i  )
            h = h->next;

If you move the UI functionality, say, to main() then you could reuse the searchByPos() in deletebyPos() and updateByPos() instead of duplicating essentially the same code. Also consider make Student *head a local variable in main() instead of a global variable. Your code already does the right thing by passing it around.

CodePudding user response:

 for(int i=1; i<pos; i  )
            h = h->next;
  • Related