Data Structure lab

Program 7

Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo

a. Create a SLL of N Students Data by using front insertion.

b. Display the status of SLL and count the number of nodes in it

c. Perform Insertion / Deletion at End of SLL

d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)

e. Exit



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

struct Student {
    char usn[20], name[30], prog[30], phno[15];
    int sem;
    struct Student* next;
};

typedef struct Student* NODE;
NODE head = NULL;

// Create node
NODE createNode() {
    NODE temp = (NODE)malloc(sizeof(struct Student));
    printf("Enter USN, Name, Programme, Sem, PhNo:\n");
    scanf("%s %s %s %d %s", temp->usn, temp->name, temp->prog, &temp->sem, temp->phno);
    temp->next = NULL;
    return temp;
}

// a. Front Insertion
void insertFront() {
    NODE newNode = createNode();
    newNode->next = head;
    head = newNode;
}

// c. Insert at End
void insertEnd() {
    NODE newNode = createNode();
    if (!head) head = newNode;
    else {
        NODE temp = head;
        while (temp->next) temp = temp->next;
        temp->next = newNode;
    }
}

// c. Delete at End
void deleteEnd() {
    if (!head) {
        printf("List Empty!\n"); return;
    }
    if (!head->next) {
        free(head); head = NULL;
    } else {
        NODE temp = head;
        while (temp->next->next) temp = temp->next;
        free(temp->next); temp->next = NULL;
    }
    printf("Deleted from End\n");
}

// d. Delete at Front
void deleteFront() {
    if (!head) {
        printf("List Empty!\n"); return;
    }
    NODE temp = head;
    head = head->next;
    free(temp);
    printf("Deleted from Front\n");
}

// b. Display and count
void display() {
    NODE temp = head;
    int count = 0;
    if (!temp) {
        printf("List Empty\n"); return;
    }
    printf("\nStudent List:\n");
    while (temp) {
        printf("%s %s %s %d %s\n", temp->usn, temp->name, temp->prog, temp->sem, temp->phno);
        count++;
        temp = temp->next;
    }
    printf("Total Students: %d\n", count);
}

// Menu
int main() {
    int ch;
    do {
        printf("\n1.Insert Front \n2.Insert End \n3.Delete Front \n4.Delete End \n5.Display \n6.Exit\nEnter choice: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1: insertFront(); break;
            case 2: insertEnd(); break;
            case 3: deleteFront(); break;
            case 4: deleteEnd(); break;
            case 5: display(); break;
            case 6: printf("Exiting...\n"); break;
            default: printf("Invalid choice\n");
        }
    } while (ch != 6);
    return 0;
}

               


  

Output :-


1.Insert Front 
2.Insert End 
3.Delete Front 
4.Delete End 
5.Display 
6.Exit
Enter choice: 1
Enter USN, Name, Programme, Sem, PhNo:
1XX22CS001
Anushka
CSE
7
0123456789

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 2
Enter USN, Name, Programme, Sem, PhNo:
1XX22CS001
Virat
CSE
7
1234567890

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 5

Student List:
1XX22CS001 Anushka CSE 7 0123456789
1XX22CS001 Virat CSE 7 1234567890
Total Students: 2

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 3
Deleted from Front

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 5

Student List:
1XX22CS001 Virat CSE 7 1234567890
Total Students: 1

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 4
Deleted from End

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 5
List Empty

1.Insert Front
2.Insert End
3.Delete Front
4.Delete End
5.Display
6.Exit
Enter choice: 6
Exiting...
  
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.