Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views45 pages

Data Structures Using C Lab

The document contains three C programs demonstrating data structure operations: an array manipulation program that allows adding, inserting, updating, and deleting elements; a singly linked list implementation with insertion, deletion, and traversal; and a circular doubly linked list with similar functionalities. Each program includes a menu-driven interface for user interaction and showcases various operations on the respective data structures. The outputs illustrate the results of the operations performed on the data structures.
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)
24 views45 pages

Data Structures Using C Lab

The document contains three C programs demonstrating data structure operations: an array manipulation program that allows adding, inserting, updating, and deleting elements; a singly linked list implementation with insertion, deletion, and traversal; and a circular doubly linked list with similar functionalities. Each program includes a menu-driven interface for user interaction and showcases various operations on the respective data structures. The outputs illustrate the results of the operations performed on the data structures.
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/ 45

1.

Write a program to read ‘N’ numbers of elements into an array and also
perform the following operation on an array
a. Add an element at the beginning of an array
b. Insert an element at given index of array
c. Update an element using a values and index
d. Delete an existing element
Program:
#include<stdio.h>
void CreateArray();
void ShowArray();
void AddatBeg();
void AddatPosition();
void UpdateatPos();
void DelEle();
int arr[100],n;

void CreateArray(){
int i;
printf("\n Enter Number of Elements in Array: ");
scanf("%d",&n);
for (i=0;i<n;i++) {
printf("Enter Element for Cell arr[%d]: ",i);
scanf("%d",&arr[i]);
}
}

void ShowArray() {
int i;
printf("\n Array Contents are: \n");
for (i=0; i<n;i++) {
printf("%d",arr[i]);
}
}

void AddatBeg() {
int i, no, k;
printf("\n Enter Element to Insert: ");
scanf("%d",&no);
for (i = 0;i < n+1;i++) {
k=arr[i];
arr[i]=no;
no=k;
}
n++;
}

void AddatPosition() {
int i, no, pos, k;
printf("\n Enter Position: ");
scanf("%d",&pos);
if (pos - 1 > n) {
printf("\n Invalid Position\n");
} else {
printf("\n Enter Element to Insert: ");
scanf("%d",&no);
for (i=pos-1;i<n+1;i++) {
k=arr[i];
arr[i]=no;
no=k;
}
n++;
}
}

void UpdateatPos() {
int no,pos;
printf("\n Enter Position: ");
scanf("%d",&pos);
if (pos-1>= n ) {
printf("\nInvalid Position\n");
} else {
printf("\nEnter Element to Update: ");
scanf("%d",&no);
arr[pos - 1]=no;
}
}

void DelEle() {
int i,pos;
printf("\nEnter Position: ");
scanf("%d", &pos);
if (pos< 0 || pos >n) {
printf("\nInvalid Position\n");
} else {
for (i=pos-1; i<n-1;i++) {
arr[i]=arr[i+1];
}
n=n-1;
}
}

void main() {
int ch;
do {
printf("\n \t \t MENU");
printf("\n 1. Create Array");
printf("\n 2. Insert Element at Beginning");
printf("\n 3. Insert Element at Given Position");
printf("\n 4. Update Element at Given Position");
printf("\n 5. Delete Element at Given Position");
printf("\n 6. Show Array");
printf("\n 7. Exit");
printf("\n Enter Your Choice (1-7): ");
scanf("%d", &ch);
switch (ch) {
case 1: CreateArray();
break;
case 2: AddatBeg();
break;
case 3: AddatPosition();
break;
case 4: UpdateatPos();
break;
case 5: DelEle();
break;
case 6: ShowArray();
break;
case 7: printf("Exiting...\n");
break;
default: printf("\n Invalid Choice \n");
}
} while (ch != 7);
}
Output:
MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 1

Enter Number of Elements in Array: 3


Enter Element for Cell arr[0]: 10
Enter Element for Cell arr[1]: 20
Enter Element for Cell arr[2]: 30

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 6

Array Contents are:


102030

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 2

Enter Element to Insert: 5


MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 6

Array Contents are:


5102030

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 3

Enter Position: 3
Enter Element to Insert: 15
MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 6

Array Contents are:


510152030

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 4

Enter Position: 2
Enter Element to Update: 8

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 6

Array Contents are:


580152030

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 5

Enter Position: 4

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 6

Array Contents are:


5802030

MENU
1. Create Array
2. Insert Element at Beginning
3. Insert Element at Given Position
4. Update Element at Given Position
5. Delete Element at Given Position
6. Show Array
7. Exit
Enter Your Choice (1-7): 7
Exiting...
2. Write Program to implement Single Linked List with insertion, deletion
and traversal operations.
Program:
#include<stdio.h>
#include<stdlib.h>
// Define the node structure
struct Node
{
int data;
struct Node*next;
};
// Head pointer for the list
struct Node*head=NULL;

// Function to create a new node


struct Node*createNode(int value) {
struct Node*newNode=(struct Node*)malloc(sizeof(struct Node));
if(newNode==NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data=value;
newNode->next=NULL;
return newNode;
}

// Insert at the beginning


void insertAtBeginning(int value) {
struct Node*newNode=createNode(value);
newNode->next=head;
head=newNode;
printf("%d inserted at the beginning.\n",value);
}

// Insert at the end


void insertAtEnd(int value) {
struct Node*newNode=createNode(value);
if(head==NULL) {
head=newNode;
} else {
struct Node*temp=head;
while(temp->next!=NULL) {
temp=temp->next;
}
temp->next=newNode;
}
printf("%d inserted at the end.\n",value);
}

// Insert at a specific position


void insertAtPosition(int value,int position) {
struct Node*newNode=createNode(value);
if(position==1) {
newNode->next=head;
head=newNode;
printf("%d inserted at position %d.\n",value,position);
return;
}
struct Node*temp=head;
int i;
for(i=1;i<position-1 && temp!=NULL;i++) {
temp=temp->next;
}

if(temp==NULL) {
printf("Invalid position %d.\n",position);
free(newNode);
return;
}

newNode->next=temp->next;
temp->next=newNode;
printf("%d inserted at position %d.\n",value,position);
}

// Delete node at a specific position


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

struct Node*temp=head;

if(position==1) {
head=head->next;
printf("%d deleted from position 1.\n",temp->data);
free(temp);
return;
}

struct Node*prev=NULL;
int i;
for(i=1;i<position && temp!=NULL;i++) {
prev=temp;
temp=temp->next;
}

if(temp==NULL) {
printf("Invalid position %d.\n",position);
return;
}

prev->next=temp->next;
printf("%d deleted from position %d.\n",temp->data,position);
free(temp);
}

// Display the list


void display() {
struct Node*temp=head;

if(head==NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while(temp != NULL) {
printf("%d -> ",temp->data);
temp=temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
int choice,value,position;

while(1) {
printf("\n--- MENU ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete at Position\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d",&value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value: ");
scanf("%d",&value);
insertAtEnd(value);
break;
case 3:
printf("Enter value: ");
scanf("%d",&value);
printf("Enter position: ");
scanf("%d",&position);
insertAtPosition(value, position);
break;
case 4:
printf("Enter position to delete: ");
scanf("%d",&position);
deleteAtPosition(position);
break;
case 5:
display();
break;
case 6:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Output:
--- MENU ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 1
Enter value: 10
10 inserted at the beginning.

--- MENU ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 2
Enter value: 20
20 inserted at the end.
--- MENU ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 2
Enter value: 30
30 inserted at the end.

--- MENU ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 3
Enter value: 15
Enter position: 2
15 inserted at position 2.

--- MENU ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 5
Linked List: 10 -> 15 -> 20 -> 30 -> NULL

--- MENU ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 4
Enter position to delete: 3
20 deleted from position 3.

--- MENU ---


1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 5
Linked List: 10 -> 15 -> 30 -> NULL
--- MENU ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete at Position
5. Display
6. Exit
Enter your choice: 6
Exiting program.
3.Write Program to Implement Circular Doubly Linked List with insertion,
deletion and traversal operations.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* head = NULL;
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = newNode->prev = newNode;
return newNode;
}
int getLength() {
if (head == NULL) return 0;
int count = 1;
struct Node* temp = head->next;
while (temp != head) {
count++;
temp = temp->next;
}
return count;
}
void insert(int value, int position) {
int len = getLength();
int i;
if (position < 1 || position > len + 1) {
printf("Can't insert at that position.\n");
return;
}
struct Node* newNode = createNode(value);
if (head == NULL) {
head = newNode;
return;
}
if (position == 1) {
struct Node* last = head->prev;
newNode->next = head;
newNode->prev = last;
last->next = newNode;
head->prev = newNode;
head = newNode;
return;
}
struct Node* current = head;
for (i = 1; i < position - 1; i++) {
current = current->next;
}
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
}
void deleteNode(int position) {
int len = getLength();
int i;
if (position < 1 || position > len) {
printf("Can't delete at that position.\n");
return;
}
if (position == 1) {
if (head->next == head) {
free(head);
head = NULL;
}
else {
struct Node* last = head->prev;
struct Node* temp = head;
head = head->next;
last->next = head;
head->prev = last;
free(temp);
}
return;
}
struct Node* current = head;
for (i = 1; i < position; i++) {
current = current->next;
}
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
}
void traverseForward() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("List (Forward): ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
void traverseBackward() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head->prev;
printf("List (Backward): ");
do {
printf("%d ", temp->data);
temp = temp->prev;
} while (temp != head->prev);
printf("\n");
}
int main() {
int choice, value, position;

do {
printf("\n1. Insert\n2. Delete\n3. Traverse Forward\n4. Traverse
Backward\n5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
printf("Enter position: ");
scanf("%d", &position);
insert(value, position);
break;
case 2:
printf("Enter position: ");
scanf("%d", &position);
deleteNode(position);
break;
case 3:
traverseForward();
break;
case 4:
traverseBackward();
break;
case 5:
printf("Exiting.\n");
break;

default:
printf("Invalid choice.\n");
}
} while (choice != 5);
return 0;
}
OUTPUT:
1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 1
Enter value: 10
Enter position: 1

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 1
Enter value: 20
Enter position: 2

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 1
Enter value: 30
Enter position: 3

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 1
Enter value: 15
Enter position: 2

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 3
List (Forward): 10 15 20 30

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 4
List (Backward): 30 20 15 10

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 2
Enter position: 3

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 3
List (Forward): 10 15 30

1. Insert
2. Delete
3. Traverse Forward
4. Traverse Backward
5. Exit
Enter choice: 5
Exiting.

You might also like