0% found this document useful (0 votes)
41 views59 pages

DS Lab Assignments Akhilesh Jagadale - Odt

Lab assignment for data structures

Uploaded by

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

DS Lab Assignments Akhilesh Jagadale - Odt

Lab assignment for data structures

Uploaded by

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

Akhilesh Jagadale

Prn-2232210504
Roll no-119
Div A

Data Structure and Algorithm

Practical Assignments
BCA-1

ASSIGNMENT 1

Q1) Write a program to implement stack operatio


using function.

#include <stdio.h>

int MAXSIZE = 8;
int stack[8];
int top = -1;
int isempty() {

if(top == -1)
return 1;
else
return 0;
}

int isfull() {

if(top == MAXSIZE)
return 1;
else
return 0;
}

int peek() {
return stack[top];
}
int pop() {
int data;

if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is
empty.\n");
}
}

int push(int data) {

if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}

int main() {
// push items on to the stack
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);

printf("Element at top of the stack: %d\n" ,peek());


printf("Elements: \n");

// print stack data


while(!isempty()) {
int data = pop();
printf("%d\n",data);
}

printf("Stack full: %s\n" , isfull()?"true":"false");


printf("Stack empty: %s\n" ,
isempty()?"true":"false");

return 0;
}
OUTPUT:-

Q2) Write a program to print reverse strings using stack


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

#define max 100


int top,stack[max];
void push(char x){

// Push(Inserting Element in stack) operation


if(top == max-1){
printf("stack overflow");
} else {
stack[++top]=x;
}

void pop(){
// Pop (Removing element from stack)
printf("%c",stack[top--]);
}

main()
{
char str[]="sri lanka";
int len = strlen(str);
int i;

for(i=0;i<len;i++)
push(str[i]);

for(i=0;i<len;i++)

pop();
}

OUTPUT:-
Q3) Write a program to check the validity of an
expression.

/*
* C Program to Check if Expression is correctly
Parenthesized
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top = -1;


char stack[100];

// function prototypes
void push(char);
void pop();
void find_top();

void main()
{
int i;
char a[100];
printf("enter expression\n");
scanf("%s", &a);
for (i = 0; a[i] != '\0';i++)
{
if (a[i] == '(')
{
push(a[i]);
}
else if (a[i] == ')')
{
pop();
}
}
find_top();
}

// to push elements in stack


void push(char a)
{
stack[top] = a;
top++;
}

// to pop elements from stack


void pop()
{
if (top == -1)
{
printf("expression is invalid\n");
exit(0);
}
else
{
top--;
}
}

// to find top element of stack


void find_top()
{
if (top == -1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}

OUTPUT:-

ASSIGNMENT:2

Q1) Write a program to create a singly linked


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

struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next
node
}*stnode;

void createNodeList(int n); // function to create the


list
void displayList(); // function to display the list

int main()
{
int n;
printf("\n\n Linked List : To create and
display Singly Linked List :\n");
printf("-----------------------------------------------
--------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct
node));

if(stnode == NULL) //check whether the fnnode is


NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard

printf(" Input data for node 1 : ");


scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL; // links the address
field to NULL
tmp = stnode;
// Creating n nodes and adding to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct
node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

fnNode->num = num; // links the num


field of fnNode with num
fnNode->nextptr = NULL; // links the
address field of fnNode with NULL

tmp->nextptr = fnNode; // links previous


node i.e. tmp to the fnNode
tmp = tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num); //
prints the data of current node
tmp = tmp->nextptr; // advances
the position of current node
}
}
}

OUTPUT:-
Q2) write a program to find the element in the linked
list.

// Iterative C program to search an


// element in linked list
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

// Link list node


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

/* Given a reference (pointer to pointer) to


the head of a list and an int, push a new
node on the front of the list. */
void push(struct Node** head_ref,
int new_key)
{
// Allocate node
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

// Put in the key


new_node->key = new_key;

// Link the old list off the new node


new_node->next = (*head_ref);

// Move the head to point to the new node


(*head_ref) = new_node;
}

// Checks whether the value x is present


// in linked list
bool search(struct Node* head, int x)
{
// Initialize current
struct Node* current = head;
while (current != NULL)
{
if (current->key == x)
return true;
current = current->next;
}
return false;
}

// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
int x = 21;

// Use push() to construct list


// 14->21->11->30->10
push(&head, 10);
push(&head, 30);
push(&head, 11);
push(&head, 21);
push(&head, 14);

search(head, 21)? printf("Yes") : printf("No");


return 0;
}
OUTPUT:-

Q3) Write a program to count the number of elements in the


singly linked list.
/**
* C program to count number of nodes of Singly Linked List
*/

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

/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;

void createList(int n);


int countNodes();
void displayList();

int main()
{
int n, total;

/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);

printf("\nData in the list \n");


displayList();

/* Count number of nodes in list */


total = countNodes();

printf("\nTotal number of nodes = %d\n", total);

return 0;
}

/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*
* Read data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);

head->data = data; // Link data field with data


head->next = NULL; // Link address field to NULL

temp = head;

/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with


data
newNode->next = NULL; // Link the address field of newNode
with NULL

temp->next = newNode; // Link previous node i.e. temp to the


newNode
temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}

/*
* Counts total number of nodes in the list
*/
int countNodes()
{
int count = 0;
struct node *temp;

temp = head;

while(temp != NULL)
{
count++;
temp = temp->next;
}

return count;
}

/*
* Displays the entire list
*/
void displayList()
{
struct node *temp;

/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
}

OUTPUT:-

ASSIGNMENT-3
Q1)Write a program to create a doubly-linked list.

#include <stdio.h>

//Represent a node of the doubly linked list

struct node{
int data;
struct node *previous;
struct node *next;
};

//Represent the head and tail of the doubly


linked list
struct node *head, *tail = NULL;

//addNode() will add a node to the list


void addNode(int data) {
//Create a new node
struct node *newNode = (struct
node*)malloc(sizeof(struct node));
newNode->data = data;

//If list is empty


if(head == NULL) {
//Both head and tail will point to
newNode
head = tail = newNode;
//head's previous will point to NULL
head->previous = NULL;
//tail's next will point to NULL, as it is
the last node of the list
tail->next = NULL;
}
else {
//newNode will be added after tail such
that tail's next will point to newNode
tail->next = newNode;
//newNode's previous will point to tail
newNode->previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point
to NULL
tail->next = NULL;
}
}

//display() will print out the nodes of the list


void display() {
//Node current will point to head
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
//Prints each node by incrementing
pointer.
printf("%d ", current->data);
current = current->next;
}
}

int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);
addNode(5);

//Displays the nodes present in the list


display();

return 0;
}

OUTPUT:-

Q2)Write a program to create a singly circular linked

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

//Represents the node of list.


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

//Declaring head and tail pointer as null.


struct node *head = NULL;
struct node *tail = NULL;

//This function will add the new node at the


end of the list.
void add(int data){
//Create new node
struct node *newNode = (struct
node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
if(head == NULL){
//If list is empty, both head and tail
would point to new node.
head = newNode;
tail = newNode;
newNode->next = head;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will
point to head.
tail->next = head;
}
}

//This function will display the nodes of


circular linked list
void display(){
struct node *current = head;
if(head == NULL){
printf("List is empty");
}
else{
printf("Nodes of the circular linked list:
\n");
do{
//Prints each node by incrementing
pointer.
printf("%d ", current->data);
current = current->next;
}while(current != head);
printf("\n");
}
}

int main()
{
//Adds data to the list
add(1);
add(2);
add(3);
add(4);
//Displays all the nodes present in the list
display();

return 0;
}

Q3) Write a program to delete elements from the


doubly linked list.

/**
* C program to delete a node from Doubly
linked list
*/

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

/*
* Basic structure of Node
*/
struct node {
int data;
struct node * prev;
struct node * next;
}*head, *last;

/*
* Functions used in this program
*/
void createList(int n);
void displayList();
void deleteFromBeginning();
void deleteFromEnd();
void deleteFromN(int position);

int main()
{
int n, data, choice=1;

head = NULL;
last = NULL;

/*
* Run forever until user chooses 0
*/
while(choice != 0)
{

printf("==============================
============\n");
printf("DOUBLY LINKED LIST
PROGRAM\n");

printf("==============================
============\n");
printf("1. Create List\n");
printf("2. Delete node - from
beginning\n");
printf("3. Delete node - from end\n");
printf("4. Delete node - from N\n");
printf("5. Display list\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter the total number of
nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
deleteFromBeginning();
break;
case 3:
deleteFromEnd();
break;
case 4:
printf("Enter the node position
which you want to delete: ");
scanf("%d", &n);
deleteFromN(n);
break;
case 5:
displayList();
break;
case 0:
break;
default:
printf("Error! Invalid choice. Please
choose between 0-5");
}
printf("\n\n\n\n\n");
}

return 0;
}

/**
* Creates a doubly linked list of n nodes.
* @n Number of nodes to be created
*/
void createList(int n)
{
int i, data;
struct node *newNode;

if(n >= 1)
{
/*
* Creates and links the head node
*/
head = (struct node
*)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

head->data = data;
head->prev = NULL;
head->next = NULL;

last = head;

/*
* Create and link rest of the n-1 nodes
*/
for(i=2; i<=n; i++)
{
newNode = (struct node
*)malloc(sizeof(struct node));

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


scanf("%d", &data);

newNode->data = data;
newNode->prev = last; // Link new
node with the previous node
newNode->next = NULL;

last->next = newNode; // Link previous


node with the new node
last = newNode; // Make new node as
last node
}

printf("\nDOUBLY LINKED LIST


CREATED SUCCESSFULLY\n");
}
}
/**
* Display the content of the list from
beginning to end
*/
void displayList()
{
struct node * temp;
int n = 1;

if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of %d node = %d\n", n,
temp->data);

n++;

/* Move the current pointer to next


node */
temp = temp->next;
}
}
}

/**
* Delete or remove the first node of the
doubly linked list
*/
void deleteFromBeginning()
{
struct node * toDelete;

if(head == NULL)
{
printf("Unable to delete. List is
empty.\n");
}
else
{
toDelete = head;

head = head->next; // Move head pointer


to 2 node

if (head != NULL)
head->prev = NULL; // Remove the link
to previous node
free(toDelete); // Delete the first node
from memory
printf("SUCCESSFULLY DELETED NODE
FROM BEGINNING OF THE LIST.\n");
}
}

/**
* Delete or remove the last node of the
doubly linked list
*/
void deleteFromEnd()
{
struct node * toDelete;

if(last == NULL)
{
printf("Unable to delete. List is
empty.\n");
}
else
{
toDelete = last;

last = last->prev; // Move last pointer to


2nd last node

if (last != NULL)
last->next = NULL; // Remove link to of
2nd last node with last node

free(toDelete); // Delete the last node


printf("SUCCESSFULLY DELETED NODE
FROM END OF THE LIST.\n");
}
}

/**
* Delete node from any position in the doubly
linked list
*/
void deleteFromN(int position)
{
struct node *current;
int i;

current = head;
for(i=1; i<position && current!=NULL; i++)
{
current = current->next;
}

if(position == 1)
{
deleteFromBeginning();
}
else if(current == last)
{
deleteFromEnd();
}
else if(current != NULL)
{
current->prev->next = current->next;
current->next->prev = current->prev;

free(current); // Delete the n node

printf("SUCCESSFULLY DELETED NODE


FROM %d POSITION.\n", position);
}
else
{
printf("Invalid position!\n");
}
}

OUTPUT:-
ASSIGNMENT:4
Q1) Write a program to create a stack using a
linked list.

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

struct node
{
int data;
struct node *next;
};
typedef struct node node;

node *top;

void initialize()
{
top = NULL;
}

void push(int value)


{
node *tmp;
tmp = malloc(sizeof(node));
tmp -> data = value;
tmp -> next = top;
top = tmp;
}
int pop()
{
node *tmp;
int n;
tmp = top;
n = tmp->data;
top = top->next;
free(tmp);
return n;
}

int Top()
{
return top->data;
}

int isempty()
{
return top==NULL;
}

void display(node *head)


{
if(head == NULL)
{
printf("NULL\n");
}
else
{
printf("%d\n", head -> data);
display(head->next);
}
}

int main()
{
initialize();
push(10);
push(20);
push(30);
printf("The top is %d\n",Top());
pop();
printf("The top after pop is %d\n",Top());
display(top);
return 0;
}

OUTPUT:-

Q2)Write a program to create the queue


using the array.
/*
* C Program to Implement a Queue using an
Array
*/
#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue
\n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is :
%d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */

OUTPUT:-
Q3)Write a program to create a queue using a
linked list.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d) //Insert elements in Queue
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
}
void dequeue() // Delete an element from Queue
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
free(t);
}
else{
f = f->next;
r->next = f;
free(t);
}

}
void print(){ // Print the elements of Queue
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
}
int main()
{
int opt,n,i,data;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data
in Queue \n3 for Delete the data from the Queue\n0 for Exit");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");

}
}while(opt!=0);
return 0;
}

OUTPUT:-
\
ASXCSSXSS

Akhilesh Jagadale
Prn-2232210504
Roll no-119
Div A

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy