0% found this document useful (0 votes)
4 views42 pages

Array and Linked List Operations in C

The document outlines a series of programming experiments focused on array and linked list operations in C. Each experiment includes an aim, code implementation, and expected output, covering topics such as array creation, insertion, deletion, matrix operations, searching algorithms, and linked list management. The experiments demonstrate fundamental data structure manipulations and algorithm implementations in C programming.

Uploaded by

bhati.shop.print
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)
4 views42 pages

Array and Linked List Operations in C

The document outlines a series of programming experiments focused on array and linked list operations in C. Each experiment includes an aim, code implementation, and expected output, covering topics such as array creation, insertion, deletion, matrix operations, searching algorithms, and linked list management. The experiments demonstrate fundamental data structure manipulations and algorithm implementations in C programming.

Uploaded by

bhati.shop.print
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

EXPERIMENT NO.

:- 1

AIM: TO WAP FOR THE CREATION AND TRAVERSAL OF ELEMENTS IN


ARRAY.
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();

//program to create an array//

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

//program for traversing or printing of array//

printf("The array so formed is:\n");


for(i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 2

AIM: WAP FOR THE INSERTION OF ELEMENTS AT ANY POSITION IN


ARRAY.
CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int arr[100];
int n, i, element, position;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to insert: ");
scanf("%d", &element);
printf("Enter the position (1 to %d) to insert the element: ", n + 1);
scanf("%d", &position);
// Validate the position //
if (position < 1 || position > n + 1)
{
printf("Invalid position!\n");
}
else
{
// Shift elements to the right to make space for the new element
for (i = n; i >= position; i--)
{
arr[i] = arr[i - 1];
}

// Insert the new element at the specified position


arr[position - 1] = element;
n++; // Increment the number of elements
printf("Array after insertion:\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 3

AIM: TO WAP FOR THE DELETION OF ELEMENTS IN ARRAY AT


SPECIFIC POSITION.
CODE:
#include <stdio.h>
#include<conio.h>

void main()
{
int arr[100];
int size, position, i;

// Get the size of the array from the user


printf("Enter the number of elements in the array: ");
scanf("%d", &size);

// Get array elements from the user


printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

// Get the position to delete from the user


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

// Validate the position


if (position < 0 || position >= size)
{
printf("Invalid position for deletion.\n");
}
else
{
// Shift elements to the left
for (i = position; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
size--; // Decrement the size of the array

// Print the array after deletion


printf("Array after deletion:\n");
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

getch();
}
OUTPUT:-
EXPERIMENT NO.:- 4

AIM: TO WAP FOR THE MATRIX MULTIPLICATION OF 3X3 ORDER IN


ARRAY.
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3];
int i,j,k;
clrscr();
printf("Enter the array elements:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("First matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the array elements:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Second matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("Product of matrices are:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 5

AIM: TO WAP TO FIND THE TRANSPOSE OF MATRIX OF ORDER 3X3 IN


ARRAY.
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("Enter the array elements:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Print the matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Transpose of the matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 6

AIM: TO WAP TO FIND THE SUM OF DIAGONAL ELEMENTS OF MATRIX


OF ORDER 3X3 IN ARRAY.
CODE:
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int matrix[3][3],i,j;
int sum = 0;
printf("Enter elements for the 3x3 matrix:\n");
for (i= 0; i < 3; i++)
{
for (j= 0; j < 3; j++)
{
printf("Enter element matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("The given matrix so formedis:\n");
for (i= 0; i < 3; i++)
{
for (j= 0; j < 3; j++)
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
for (i=0; i < 3; i++)
{
sum += matrix[i][i];
}
printf("\nSum of the main diagonal elements: %d\n", sum);
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 7

AIM: WAP FOR IMPLEMENTATION OF LINEAR SEARCH OPERATION IN


ARRAY.
CODE:
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,j,search,found=-1;
printf("Enter the array elements:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("The given array is:\n");
for(i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
printf("Enter the element you want to search:\n");
scanf("%d",&search);
for (i= 0; i <10 ; i++)
{
if (a[i] == search)
{
found=i;
break;
}
}
if(found!= -1)
{
printf("Element %d found at index %d.\n", search, found);
}
else
{
printf("Element %d not found in the array.\n", search);
}
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 8

AIM: WAP FOR IMPLEMENTATION OF BINARY SEARCH OPERATION IN


ARRAY.
CODE:
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, key, low, high, mid, found = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[100] ;
printf("Enter the elements of the array in sorted order:\n");
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("\nThe array formed is: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter the element to search: ");
scanf("%d", &key);
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key)
{
printf("Element %d found at index %d.\n", key, mid);
found = 1;
break;
}
else if (arr[mid] < key)
{
low = mid + 1;
} else
{
high = mid - 1;
}
}
if (!found)
{
printf("Element %d not found in the array.\n", key);
}
getch();
}
OUTPUT:-
EXPERIMENT NO.:- 9

AIM: WAP TO PERFORM VARIOUS OPERATIONS ON SINGLY LINKED


LIST DATA STRUCTURE.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node {
int data;
struct Node *next;
};
struct Node *head = NULL;
void createList(int n);
void insertAtPos(int data, int position);
void deleteNode(int position);
void traverseList();
int getListSize();
void main() {
int choice, n, data, position;
clrscr();
do
{
printf("\n\n*** LINKED LIST OPERATIONS ***\n");
printf("1. Create List\n");
printf("2. Insert at Any Position\n");
printf("3. Delete Node\n");
printf("4. Traverse (Print) List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the number of nodes to create: ");
scanf("%d", &n);
createList(n);
break;
case 2:
printf("Enter the data to insert: ");
scanf("%d", &data);
printf("Enter the position to insert at (1 for start): ");
scanf("%d", &position);
insertAtPos(data, position);
break;
case 3:
printf("Enter the position of the node to delete (1 for start): ");
scanf("%d", &position);
deleteNode(position);
break;
case 4:
traverseList();
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
// getch(); // Uncomment this for Turbo C to pause before loop restart
} while (choice != 5);
}
int getListSize() {
struct Node *temp = head;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}

// --- 1. Creation of Linked List


void createList(int n) {
if (n <= 0) {
printf("Invalid number of nodes.\n");
return;
}

struct Node *newNode, *temp;


int i, data;

// Clear existing list if any


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

printf("Creating a new list...\n");


for (i = 1; i <= n; i++) {
newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(0);
}

printf("Enter data for node %d: ", i);


scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;

if (head == NULL) {
head = newNode; // First node is the head
temp = head;
} else {
temp->next = newNode; // Link the previous node to the new node
temp = newNode; // Move temp to the new last node
}
}
printf("Linked List created successfully with %d nodes.\n", n);
}

// --- 2. Insertion at Any Position ---


void insertAtPos(int data, int position) {
int i;
struct Node *newNode, *temp;
int size = getListSize();

// Check for valid position


if (position < 1 || position > size + 1) {
printf("Invalid position. List has %d nodes. Valid positions are 1 to %d.\n", size, size +
1);
return;
}

newNode = (struct Node *)malloc(sizeof(struct Node));


if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(0);
}
newNode->data = data;
newNode->next = NULL;

// Insertion at the beginning (position 1)


if (position == 1) {
newNode->next = head;
head = newNode;
printf("Node with data %d inserted at position 1.\n", data);
return;
}

// Insertion at any other position


temp = head;
for (i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next; // Traverse to the (position - 1) node
}

// temp is now pointing to the node BEFORE the desired insertion point
newNode->next = temp->next;
temp->next = newNode;
printf("Node with data %d inserted at position %d.\n", data, position);
}

// --- 3. Deletion in Linked List ---


void deleteNode(int position) {
struct Node *temp, *prev;
int i;
int size = getListSize();

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

// Check for valid position


if (position < 1 || position > size) {
printf("Invalid position. List has %d nodes. Valid positions are 1 to %d.\n", size, size);
return;
}

int deleted_data;

// Deletion from the beginning (position 1)


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

// Deletion from any other position


temp = head;
prev = NULL;
for (i = 1; i < position && temp != NULL; i++) {
prev = temp;
temp = temp->next; // Traverse until temp is the node to be deleted
}

// temp is the node to be deleted, prev is the node before it


if (temp != NULL) {
prev->next = temp->next;
deleted_data = temp->data;
free(temp);
printf("Node with data %d deleted from position %d.\n", deleted_data, position);
}
}

// --- 4. Traversing (Printing) of Linked List ---


void traverseList() {
struct Node *temp;

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

printf("\nLinked List (Traversal): \n");


temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next; // Move to the next node
}
printf("NULL\n");
}
OUTPUT:-
EXPERIMENT NO.:- 10

AIM: WAP TO PERFORM VARIOUS OPERATIONS ON STACK DATA


STRUCTURE USING ARRAY .
CODE:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX_SIZE 10
int stack[MAX_SIZE];
int top = -1,i;
void push(int data);
int pop();
void display();
void createStack();
void main() {
int choice, data;
clrscr();
createStack();

do {
printf("\n\n*** STACK OPERATIONS (Array) ***\n");
printf("1. Push (Insertion)\n");
printf("2. Pop (Deletion)\n");
printf("3. Display (Traversal)\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to push onto the stack: ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
if (data != -1) { // -1 is used as an error signal
printf("Popped element is: %d\n", data);
}
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
// getch(); // Uncomment for Turbo C to pause before loop restart
} while (choice != 4);
}

// --- 1. Creation/Initialization of Stack ---


// Since we use an array, creation is mainly about initializing 'top'.
void createStack() {
top = -1;
printf("\nStack created and initialized with MAX_SIZE %d.\n", MAX_SIZE);
}
// --- 2. Push (Insertion) Operation ---
void push(int data) {
if (top >= MAX_SIZE - 1) {
printf("\n*** STACK OVERFLOW ***: Cannot push %d. Stack is full.\n", data);
} else {
top++; // Increment top
stack[top] = data; // Insert the element at the new top
printf("Pushed %d successfully.\n", data);
}
}

// --- 3. Pop (Deletion) Operation ---


int pop() {
if (top == -1) {
printf("\n*** STACK UNDERFLOW ***: Cannot pop. Stack is empty.\n");
return -1; // Return a sentinel value for error
} else {
int deleted_data = stack[top]; // Get the data at the top
top--; // Decrement top (logical deletion)
return deleted_data;
}
}

// --- 4. Display (Traversal) Operation ---


void display() {
if (top == -1) {
printf("\nStack is **Empty**.\n");
return;
}
printf("\nStack elements (Top to Bottom):\n");

// Traverse from the current 'top' down to index 0


for (i = top; i >= 0; i--) {
printf("| %d |\n", stack[i]);
if (i == top) {
printf("------ (TOP)\n");
}
}
printf("------\n");

getch();
}
OUTPUT:-
EXPERIMENT NO.:- 11

AIM: WAP TO PERFORM BUBBLE SORT TECHNIQUE.


CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int A[5]={8,5,3,7,2};
int i,j,temp,n=5;
printf("The original array is:\n");
for(i=0;i<n;i++)
{
printf("\t%d", A[i]);
}
printf("\n");
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
printf("\nThe Bubble Sorted array is:\n");
for(i=0;i<n;i++)
{
printf("\t%d",A[i]);
}
getch();
}
OUTPUT:-

You might also like