0% found this document useful (0 votes)
2 views

DS File

The document contains practical exercises on data structures, including swapping numbers using call by value and call by reference, various sorting algorithms (bubble, insertion, selection, quick sort), and operations on 1D and 2D arrays. It also covers linked lists, detailing the creation, traversal, and insertion methods for both singly and doubly linked lists. Each section includes source code examples and expected outputs for better understanding.

Uploaded by

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

DS File

The document contains practical exercises on data structures, including swapping numbers using call by value and call by reference, various sorting algorithms (bubble, insertion, selection, quick sort), and operations on 1D and 2D arrays. It also covers linked lists, detailing the creation, traversal, and insertion methods for both singly and doubly linked lists. Each section includes source code examples and expected outputs for better understanding.

Uploaded by

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

Practical File

DATA STRUCTURES

Lab1- To swap two numbers by 1) Call by value 2) Call by reference DATE:09/09/24

Source Code ➖
// WAP to swap 2 numbers using call by value method.
#include<stdio.h>
void swap (int a, int b){
int temp ;
temp = a ;
a=b;
b = temp ;
printf("Inside function:- a = %d , b = %d\n",a,b);
}
int main(){
int a, b ;
printf("Enter values of a and b:- ");
scanf("%d %d",&a,&b);

printf("Before swapping:- a = %d , b = %d\n",a,b);


swap(a,b) ;
printf("After swapping:- a = %d , b = %d\n",a,b);
}

// WAP to swap 2 numbers using a call by reference method.


#include<stdio.h>
void swap2(int *a, int *b);

void main(){
int a, b;
printf("Enter values of a and b:- ");
scanf("%d %d",&a,&b);
printf("Before swapping:- a = %d , b = %d\n",a,b);
swap2(&a,&b) ;
printf("After swapping:- a = %d , b = %d\n",a,b);
}

void swap2(int *a, int *b){


int temp;
temp = *a ;
*a = *b ;
*b = temp ;
printf("Inside function:- a = %d , b = %d\n",*a,*b);
}
Output:-
Lab 2- To apply different sorting techniques(bubble, insertion, selection and quick sort)
DATE:23/09/24


BUBBLE SORTING
Source Code
#include<stdio.h>
int main()
{
//Bubble Sort
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
//Bubble Sort Algorithm
int flag = 0 ;
for (int i=0 ; i<n ; i++){
for (int j=0 ; j<n-i-1 ; j++){
if (a[j] > a[j+1])
{
int t = a[j] ;
a[j] = a[j+1] ;
a[j+1] = t ;
flag = 1 ;
}
}
if (flag == 0)
break ;
}
printf("\nArray after bubble sort:- ") ;
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
return 0 ;
}
Output –
INSERTION SORTING

Source Code–
#include<stdio.h>
int main()
{
//Insertion Sort
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);

int a[n] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
//Insertion Sort Algorithm
for (int i=1 ; i<=n ; i++)
{
int temp = a[i] ;
int j ;
for (j=i-1 ; j>=0 && a[j] > temp ; j--)
{
a[j+1] = a[j] ;
}
a[j+1] = temp ;
}
printf("\nArray after Insertion sort:- ") ;
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
return 0 ;

Output–
SELECTION SORT

Source Code–

#include<stdio.h>
int main()
{
//Selection Sort
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);

int a[n] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
//Selection Sort Algorithm
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
printf("\nArray after selection sort:- ") ;
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
return 0 ;
}

Output-
QUICK SORT

Source Code –
#include <stdio.h>
// Quick Sort
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Quick Sort Algorithm
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
int pi = i + 1;

quicksort(arr, low, pi - 1);


quicksort(arr, pi + 1, high);
}}
int main() {
int n ;
printf("Enter how many elements u want to enter:- : ");
scanf("%d", &n);

int arr[n];
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

quicksort(arr, 0, n - 1);

printf("Sorted array: \n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output –
Lab 3- Traversing and inserting elements in 1D and 2D array

Q1- Print total number of elements in an array


Source Code-

#include<stdio.h>
void main()
{
//Printing total number of elements in an array.
int n;
printf("Enter total number of elements u want to enter:- ");
scanf("%d",&n);

int a[n];
for (int i=0 ; i<n ; i++)
{
printf("Enter element %d :- ",i+1);
scanf("%d",&a[i]);
}

int ttl = 0 ;
for (int i=0 ; i<n ; i++)
{
ttl += 1 ;
}
printf("Total number of elements in array is :- %d",ttl);
}

Output –
Q2- To count total number of elements in 2D array
Source Code-

#include<stdio.h>
void main()
{
int r, c ;
printf("Enter number of rows and columns for array:- ") ;
scanf("%d %d",&r,&c) ;

int a[r][c] ;
printf("Enter elements for array:- ") ;
for (int i=0 ; i<r ; i++)
{
for (int j=0 ; j<c ; j++)
{
scanf("%d",&a[i][j]) ;
}
}

int count = 0 ;
for (int i=0 ; i<r ; i++)
{
for (int j=0 ; j<c ; j++)
{
count ++ ;
}
}

printf("Total number of elements in arrray is :-%d", count);


}

Output –
Q.3- Insert an element in the beginning, middle and at end of an 1D array. DATE:30/09/24
Source Code:--

Beginning

#include<stdio.h>
void main(){
// Insertion at beginning of 1D array
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);

int a[n+1] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]);
}
//Insertion at beginning of 1D array
int elm ;
printf("\nEnter the element which u want to enter:- ");
scanf("%d",&elm);
for (int i=n-1 ; i>=0 ; i--){
a[i+1] = a[i];
}
a[0] = elm ;
printf("\nArray after Insertion:- ");
for (int i=0 ; i<=n ; i++){
printf("%d\t",a[i]);
}
}

Output –
End

Source Code:-
#include<stdio.h>
void main(){
// Insertion at end of 1D array
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);

int a[n+1] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]);
}
//Insertion at end of 1D array
int elm ;
printf("\nEnter the element which u want to enter:- ");
scanf("%d",&elm);

a[n] = elm ;

printf("\nArray after Insertion:- ");


for (int i=0 ; i<=n ; i++){
printf("%d\t",a[i]);
}
}

Output:-
Desired Location

Source Code:--
#include<stdio.h>
void main(){
// Insertion at Desired location of 1D array
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n+1] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]);
}
// Insertion at Desired location of 1D array
int elm ,pos ;
printf("\nEnter the element which u want to enter:- ");
scanf("%d",&elm);
printf("\nEnter the position where u want to enter:- ");
scanf("%d",&pos);
for (int i=n-1 ; i>=pos-1 ; i--){
a[i+1] = a[i];
}
a[pos-1] = elm;
printf("\nArray after Insertion:- ");
for (int i=0 ; i<=n ; i++){
printf("%d\t",a[i]);
}
}

Output :--
LAB 4- Make and Traverse singly and doubly linked list and do insertion DATE:07/10/24
in singly linked list

Source Code-
Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
int data, n;
printf("How many nodes do you want to add? ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter data for node %d: ", i + 1);
scanf("%d", &data);
insertNode(&head, data);
}
printList(head);
return 0;}
Output :--
Doubly Linked List
#include <stdio.h>
#include <stdlib.h>

// Define doubly linked list


struct Node {
int data;
struct Node* prev;
struct Node* next;
};

// Function to create new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to print elements of doubly linked list


void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Create doubly linked list


struct Node* createList(int n) {
if (n <= 0) {
return NULL;
}

int data;
printf("Enter data for node 1: ");
scanf("%d", &data);
struct Node* head = createNode(data);
struct Node* temp = head;

for (int i = 2; i <= n; ++i) {


printf("Enter data for node %d: ", i);
scanf("%d", &data);
struct Node* newNode = createNode(data);
temp->next = newNode;
newNode->prev = temp;
temp = newNode;
}

return head;
}

int main() {
int n;
printf("Enter the number of nodes you want to enter: ");
scanf("%d", &n);

struct Node* head = createList(n);

printf("Doubly Linked List: ");


traverseList(head);

return 0;
}

Output :--
Insertion at beginning of singly linked list
Source Code-
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a singly linked list node


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));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
// Function to insert a node at the beginning
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
newNode->next = head;
return newNode;
}
// Function to traverse the singly linked list and print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;
int n, data;
// Ask for the number of elements
printf("Enter the number of nodes u want to enter: ");
scanf("%d", &n);
// Ask for data for each node
for (int i = 1; i <= n; i++) {
printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtEnd(head, data);
}
// Print the list after all insertions
printf("Linked List before inserting new element: ");
traverseList(head);
// Ask for another element to insert at the beginning
printf("Enter data to insert: ");
scanf("%d", &data);
// Insert the new element at the beginning
head = insertAtBeginning(head, data);
// Print the list after the new insertion
printf("Linked List after inserting new element: ");
traverseList(head);
return 0;
}

Output :--
Insertion at end of singly linked list

Source Code-

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

// Define the structure for a singly linked list node


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));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end


struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}

// Function to traverse the singly linked list and print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;
int n, data;

// Ask for the number of elements


printf("Enter the number of nodes u want to enter: ");
scanf("%d", &n);

// Ask for data for each node


for (int i = 1; i <= n; i++) {
printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtEnd(head, data);
}

// Print the list after all insertions


printf("Linked List before inserting new element: ");
traverseList(head);

// Ask for another element to insert at the end


printf("Enter data to insert: ");
scanf("%d", &data);

// Insert the new element at the end


head = insertAtEnd(head, data);

// Print the list after the new insertion


printf("Linked List after inserting new element: ");
traverseList(head);

return 0;
}

Output :--
Insertion at specific position of singly linked list

Source Code-
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a singly linked list node


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));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at a specific position


struct Node* insertAtPosition(struct Node* head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 1) {
newNode->next = head;
return newNode;
}
struct Node* temp = head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("!!!Out of Range ERROR!!!\n");
free(newNode);
return head;
}
newNode->next = temp->next;
temp->next = newNode;
return head;
}

// Function to traverse the singly linked list and print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;
int n, data, position;

// Ask for the number of elements


printf("Enter the number of nodes u want to enter: ");
scanf("%d", &n);

// Ask for data for each node


for (int i = 1; i <= n; i++) {
printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtPosition(head, data, i);
}
// Print the list after all insertions
printf("Linked list before inserting new nodes: ");
traverseList(head);
// Ask for another element to insert at a specific position
printf("Enter data which u want to insert: ");
scanf("%d", &data);
printf("Enter position where u want to insert: ");
scanf("%d", &position);
// Insert the new element at the specified position
head = insertAtPosition(head, data, position);

// Print the list after the new insertion


printf("Linked List after inserting new element: ");
traverseList(head);
return 0;
}

Output :--
LAB 5- Deletion in singly linked list and insertion in doubly linked list. DATE:14/10/24

Source Code-

Deletion At Beginning
// Deletion at the Beginning in Singly Linked List:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void traverse(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
struct Node* deleteAtBeginning(struct Node* head) {
if (head == NULL) {
printf("List is empty, nothing to delete.\n");
return NULL;
}
struct Node* temp = head;
head = head->next;
free(temp);
return head;
}

int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
head->next = second;
second->next = third;
printf("\nBefore Deletion: ");
traverse(head);
head = deleteAtBeginning(head);
printf("After Deletion at Beginning: ");
traverse(head);
return 0;
}

Output :--
Deletion At Specific Location
Source Code-
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void traverse(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

struct Node* deleteAtPosition(struct Node* head, int position) {


if (head == NULL) return NULL;
struct Node* temp = head;
if (position == 1) {
head = head->next;
free(temp);
return head;
}
for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) return head;

struct Node* next = temp->next->next;


free(temp->next);
temp->next = next;
return head;
}

int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);

head->next = second;
second->next = third;
third->next = fourth;

printf("Before Deletion: ");


traverse(head);

int position;
printf("Enter the position to delete: ");
scanf("%d", &position);

head = deleteAtPosition(head, position);

printf("After Deletion at Position %d: ", position);


traverse(head);

return 0;
}

Output:-
Deletion At End Location

Source Code:--
//3. Deletion at the End:
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void traverse(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

struct Node* deleteAtEnd(struct Node* head) {


if (head == NULL) {
return NULL;
}
if (head->next == NULL) {
free(head);
return NULL;
}

struct Node* temp = head;


while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
return head;
}

int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);

head->next = second;
second->next = third;

printf("Before Deletion: ");


traverse(head);

head = deleteAtEnd(head);

printf("After Deletion at End: ");


traverse(head);

return 0;
}

Output:--
Insertion in Doubly Linked List At Beginning
Source Code:--

#include <stdio.h>
#include <stdlib.h>
// Define the structure for a doubly linked list node
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
return head;
}
// Function to insert a node at the beginning
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head != NULL) {
head->prev = newNode;
newNode->next = head;
}
return newNode;
}
// Function to print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data;
// Ask for the number of elements
printf("Enter the number of nodes: ");
scanf("%d", &n);
// Ask for data for each node
for (int i = 1; i <= n; i++) {
printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtEnd(head, data);
}
// Print the list after all insertions
printf("Doubly Linked List after initial insertion: ");
traverseList(head);
// Ask for another element to insert at the beginning
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
// Insert the new element at the beginning
head = insertAtBeginning(head, data);
// Print the list after the new insertion
printf("Doubly Linked List after inserting at beginning: ");
traverseList(head);
return 0;
}

Output:--
Insertion in Doubly Linked List At Specific Position
Source Code:--
#include <stdio.h>
#include <stdlib.h>

// Define the structure


struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
struct Node* insertAtPosition(struct Node* head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 1) {
if (head != NULL) {
head->prev = newNode;
newNode->next = head;
}
return newNode;
}
struct Node* temp = head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return head;
}
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
newNode->prev = temp;
return head;
}
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;
int n, data, position;

printf("Enter the number of nodes: ");


scanf("%d", &n);

for (int i = 1; i <= n; i++) {


printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtPosition(head, data, i);
}
printf("Doubly Linked List after initial insertion: ");
traverseList(head);

printf("Enter data to insert: ");


scanf("%d", &data);
printf("Enter position to insert: ");
scanf("%d", &position);

head = insertAtPosition(head, data, position);

printf("Doubly Linked List after inserting at specific position: ");


traverseList(head);

return 0;
}

Output:--
Insertion in Doubly Linked List At End

Source Code:-
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("!!!ERROR!!!\n");
exit(1);
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

struct Node* insertAtEnd(struct Node* head, int data) {


struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
return head;
}

void traverseList(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data, position;

// Ask for the number of elements


printf("Enter the number of nodes: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("!!!ERROR!!!\nPlease enter a positive integer.\n");
return 1;
}

for (int i = 1; i <= n; i++) {


printf("Enter data for node %d: ", i);
if (scanf("%d", &data) != 1) {
printf("Invalid input\n");
return 1;
}
head = insertAtEnd(head, data);
}

printf("Linked list before inserting new element: ");


traverseList(head);

// Ask for the position and value to insert at the end


printf("Enter data : ");
if (scanf("%d", &data) != 1) {
printf("Invalid input\n");
return 1;
}

// Insert the new element at the end


head = insertAtEnd(head, data);

// Print the list after the new insertion


printf("Linked list after inserting new element: ");
traverseList(head);
return 0;
}
Output:---
Lab 6: Deletion in Doubly Linked List And Insertion in Circular Linked List. DATE:04/11/24

Program 6.1: Deletion at the beginning in Doubly Linked List


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

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

void traverseForward(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

struct Node* deleteAtBeginning(struct Node* head) {


if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return NULL;
}

struct Node* temp = head;


head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);

return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);
struct Node* fifth = createNode(50);

head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = fourth;
fourth->prev = third;
fourth->next = fifth;
fifth->prev = fourth;

printf("Doubly Linked List (Before Deletion): ");


traverseForward(head);

head = deleteAtBeginning(head);

printf("Doubly Linked List (After Deletion at Beginning): ");


traverseForward(head);

return 0;
}

Output 6.1: Deletion at the beginning in Doubly Linked List


Program 6.2: Deletion at a specific location in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

void traverse(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

struct Node* deleteAtPosition(struct Node* head, int position) {


if (head == NULL || position <= 0) {
printf("Invalid position or empty list.\n");
return head;
}

struct Node* temp = head;

if (position == 1) {
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
return head;
}

for (int i = 1; temp != NULL && i < position; i++) {


temp = temp->next;
}
if (temp == NULL) {
printf("Position out of bounds.\n");
return head;
}

if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);

return head;
}

int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);

head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = fourth;
fourth->prev = third;

printf("Doubly Linked List (Before Deletion): ");


traverse(head);

int position;
printf("Enter the position to delete: ");
scanf("%d", &position);

head = deleteAtPosition(head, position);

printf("Doubly Linked List (After Deletion at Position %d): ", position);


traverse(head);
return 0;}

Output 6.2: Deletion at a specific location in Doubly Linked List


Program 6.3: Deletion at the End in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* prev;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

void traverse(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

struct Node* deleteAtEnd(struct Node* head) {


if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return NULL;
}

struct Node* temp = head;

if (temp->next == NULL) {
free(temp);
return NULL;
}

while (temp->next != NULL) {


temp = temp->next;
}

temp->prev->next = NULL;
free(temp);
return head;
}

int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);

head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = fourth;
fourth->prev = third;

printf("Doubly Linked List (Before Deletion at End): ");


traverse(head);

head = deleteAtEnd(head);

printf("Doubly Linked List (After Deletion at End): ");


traverse(head);

return 0;
}

Output 6.3: Deletion at the End in Doubly Linked List


Program 6.4: Insertion in the Singly Circular Linked List at the beginning
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};

struct Node *createNode(int data)


{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode;
return newNode;
}

void traverse(struct Node *head)


{
if (head == NULL)
{
printf("List is empty.\n");
return;
}

struct Node *temp = head;


do
{
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
}

struct Node *insertAtBeginning(struct Node *head, int data)


{
struct Node *newNode = createNode(data);

if (head == NULL)
{
return newNode;
}

struct Node *temp = head;

while (temp->next != head)


{
temp = temp->next;
}

temp->next = newNode;
newNode->next = head;
head = newNode;

return head;
}

int main()
{
struct Node *head = createNode(10);
struct Node *second = createNode(20);
struct Node *third = createNode(30);

head->next = second;
second->next = third;
third->next = head;

printf("Circular Linked List (Before Insertion): ");


traverse(head);

printf("Element to be inserted: ");


int elt;
scanf("%d", &elt);

head = insertAtBeginning(head, elt);

printf("Circular Linked List (After Insertion at Beginning): ");


traverse(head);

return 0;
}

Output 6.4: Insertion in the Singly Circular Linked List at the beginning
Program 6.5: Insertion in the Singly Circular Linked List at a Specific Location
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode;
return newNode;
}

void traverse(struct Node* head) {


if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
}

struct Node* insertAtPosition(struct Node* head, int data, int position) {


struct Node* newNode = createNode(data);

if (position == 1) {
if (head == NULL) {
return newNode;
}

struct Node* temp = head;

while (temp->next != head) {


temp = temp->next;
}

newNode->next = head;
temp->next = newNode;
head = newNode;
return head;
}

struct Node* temp = head;


for (int i = 1; i < position - 1; i++) {
if (temp->next == head) {
printf("Position out of bounds. Node inserted at the end.\n");
break;
}
temp = temp->next;
}

newNode->next = temp->next;
temp->next = newNode;

return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);

head->next = second;
second->next = third;
third->next = head;

printf("Circular Linked List (Before Insertion): ");


traverse(head);

int position, data;


printf("Enter the data to insert: ");
scanf("%d", &data);
printf("Enter the position to insert: ");
scanf("%d", &position);

head = insertAtPosition(head, data, position);

printf("Circular Linked List (After Insertion at Position %d): ", position);


traverse(head);

return 0;
}

Output 6.5: Insertion in the Singly Circular Linked List at a Specific Location
Program 6.6: Insertion in the Singly Circular Linked List at the End
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = newNode;
return newNode;
}

void traverse(struct Node* head) {


if (head == NULL) {
printf("List is empty.\n");
return;
}

struct Node* temp = head;


do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(back to head)\n");
}

struct Node* insertAtEnd(struct Node* head, int data) {


struct Node* newNode = createNode(data);

if (head == NULL) {
return newNode;
}

struct Node* temp = head;


while (temp->next != head) {
temp = temp->next;
}

temp->next = newNode;
newNode->next = head;

return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);

head->next = second;
second->next = third;
third->next = head;

printf("Circular Linked List (Before Insertion at End): ");


traverse(head);

head = insertAtEnd(head, 40);

printf("Circular Linked List (After Insertion at End): ");


traverse(head);

return 0;
}
Output 6.6: Insertion in the Singly Circular Linked List at the End
Lab 7: Perform Traverse , Insertion , Deletion on Stack using Array DATE:11/11/24

Program 7.1: Traverse in the Stack using Array


#include <stdio.h>
#define MAX 100

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("Stack overflow! Cannot push %d\n", value);
} else {
top++;
stack[top] = value;
printf("Pushed %d onto the stack.\n", value);
}
}

int pop() {
if (top == -1) {
printf("Stack underflow! Cannot pop.\n");
return -1;
} else {
int value = stack[top];
top--;
return value;
}
}

void traverse() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

int main() {
push(10);
push(20);
push(30);
push(40);

printf("Traversing the stack:\n");


traverse();

pop();
printf("After popping an element:\n");
traverse();

return 0;
}

Output 7.1: Traverse in the Stack using Array


Program 7.2: Insertion in the Stack using Array (PUSH)
#include <stdio.h>
#define MAX 100

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("Stack overflow! Cannot push %d\n", value);
} else {
top++;
stack[top] = value;
printf("Pushed %d onto the stack.\n", value);
}
}

void display() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
printf("Current stack after insertions:\n");
display();
return 0;
}

Output 7.2: Insertion in the Stack using Array (PUSH)


Program 7.3: Deletion in the Stack using Array (POP)
#include <stdio.h>
#define MAX 100

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("Stack overflow! Cannot push %d\n", value);
} else {
top++;
stack[top] = value;
printf("Pushed %d onto the stack.\n", value);
}
}

int pop() {
if (top == -1) {
printf("Stack underflow! Cannot pop.\n");
return -1;
} else {
int value = stack[top];
top--;
printf("Popped %d from the stack.\n", value);
return value;
}
}

void display() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

int main() {
push(10);
push(20);
push(30);
push(40);

printf("Stack before deletion:\n");

display();

pop();
pop();

printf("Stack after deleting two elements:\n");


display();

return 0;
}

Output 7.3: Deletion in the Stack using Array (POP)

You might also like