0% found this document useful (0 votes)
13 views8 pages

Ds Exp Single Linklist

Ds exp Sppu

Uploaded by

harshalmane2910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Ds Exp Single Linklist

Ds exp Sppu

Uploaded by

harshalmane2910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PUNE INSTITUTE OF COMPUTER TECHNOLOGY

PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

Title: Linklist operations

Write as per experiment list.

Write a program in C to create a single linked list. Perform following


operation

1. Insert element at front

2. Insert element at end

Problem 3. Insert element in middle


Statement
4. Delete an element

5. Display

6. Display reverse

7. Revert the SLL

Programmer Name: Harshal Ajay Mane


Batch: G8

1. Without Pointer:

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

// Node structure
Struct Node {
Int data;
Struct Node* next;
};

// Function to create a new node


Struct Node* createNode(int data) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

DS_LAB_2024-25: Program input output 1


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Insert an element at the front


Void insertFront(struct Node** head, int data) {
Struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Insert an element at the end


Void insertEnd(struct Node** head, int data) {
Struct Node* newNode = createNode(data);
If (*head == NULL) {
*head = newNode;
Return;
}
Struct Node* temp = *head;
While (temp->next != NULL) {
Temp = temp->next;
}
Temp->next = newNode;
}

// Insert element in the middle


Void insertMiddle(struct Node** head, int data, int position) {
Struct Node* newNode = createNode(data);
If (position == 1) {
newNode->next = *head;
*head = newNode;
Return;
}
Struct Node* temp = *head;

DS_LAB_2024-25: Program input output 2


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

For (int I = 1; I < position – 1 && temp != NULL; i++) {


Temp = temp->next;
}
If (temp == NULL) {
Printf(“Position out of range\n”);
Return;
}
newNode->next = temp->next;
temp->next = newNode;
}

// Delete an element
Void deleteNode(struct Node** head, int key) {
Struct Node* temp = *head;
Struct Node* prev = NULL;

If (temp != NULL && temp->data == key) {


*head = temp->next;
Free(temp);
Return;
}
While (temp != NULL && temp->data != key) {
Prev = temp;
Temp = temp->next;
}
If (temp == NULL) {
Printf(“Element not found\n”);
Return;
}
Prev->next = temp->next;
Free(temp);
}

// Display the list


Void display(struct Node* head) {

DS_LAB_2024-25: Program input output 3


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

Struct Node* temp = head;


While (temp != NULL) {
Printf(“%d -> “, temp->data);
Temp = temp->next;
}
Printf(“NULL\n”);
}

// Display the list in reverse


Void displayReverse(struct Node* head) {
If (head == NULL) return;
displayReverse(head->next);
printf(“%d -> “, head->data);
}

// Reverse the linked list


Void reverseList(struct Node** head) {
Struct Node* prev = NULL;
Struct Node* curr = *head;
Struct Node* next = NULL;

While (curr != NULL) {


Next = curr->next;
Curr->next = prev;
Prev = curr;
Curr = next;
}
*head = prev;
}

// Main function
Int main() {
Struct Node* head = NULL;

Int choice, data, position;

DS_LAB_2024-25: Program input output 4


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

While (1) {
Printf(“\nMenu:\n”);
Printf(“1. Insert element at front\n”);
Printf(“2. Insert element at end\n”);
Printf(“3. Insert element in middle\n”);
Printf(“4. Delete an element\n”);
Printf(“5. Display\n”);
Printf(“6. Display reverse\n”);
Printf(“7. Reverse the list\n”);
Printf(“8. Exit\n”);
Printf(“Enter your choice: “);
Scanf(“%d”, &choice);

Switch (choice) {
Case 1:
Printf(“Enter data to insert at front: “);
Scanf(“%d”, &data);
insertFront(&head, data);
break;
case 2:
printf(“Enter data to insert at end: “);
scanf(“%d”, &data);
insertEnd(&head, data);
break;
case 3:
printf(“Enter data to insert: “);
scanf(“%d”, &data);
printf(“Enter position to insert: “);
scanf(“%d”, &position);
insertMiddle(&head, data, position);
break;
case 4:
printf(“Enter element to delete: “);
scanf(“%d”, &data);

DS_LAB_2024-25: Program input output 5


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

deleteNode(&head, data);
break;
case 5:
printf(“Linked list: “);
display(head);
break;
case 6:
printf(“Linked list in reverse: “);
displayReverse(head);
printf(“NULL\n”);
break;
case 7:
reverseList(&head);
printf(“Linked list reversed.\n”);
break;
case 8:
exit(0);
default:
printf(“Invalid choice!\n”);
}
}

Return 0;
}

Output :
1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 1

DS_LAB_2024-25: Program input output 6


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

Enter the element to insert at front: 10

1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 2
Enter the element to insert at end: 30

1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 5
Current List: 10 -> 30 -> NULL

1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 3
Enter the element to insert: 20
Enter the position to insert: 2

DS_LAB_2024-25: Program input output 7


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2024-2025 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2024-25/ Starting date:
Roll No:22447 Submission date:

1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 5
Current List: 10 -> 20 -> 30 -> NULL

1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 7
List Reverted.

1. Insert at Front
2. Insert at End
3. Insert in Middle
4. Delete an Element
5. Display
6. Display Reverse
7. Revert the List
8. Exit
Enter your choice: 5
Current List: 30 -> 20 -> 10 -> NULL

DS_LAB_2024-25: Program input output 8

You might also like