DS Lab Assignments Akhilesh Jagadale - Odt
DS Lab Assignments Akhilesh Jagadale - Odt
Prn-2232210504
Roll no-119
Div A
Practical Assignments
BCA-1
ASSIGNMENT 1
#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");
}
}
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);
return 0;
}
OUTPUT:-
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>
// 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();
}
OUTPUT:-
ASSIGNMENT:2
struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next
node
}*stnode;
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));
OUTPUT:-
Q2) write a program to find the element in the linked
list.
// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
int x = 21;
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
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);
return 0;
}
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
/*
* 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);
temp = head;
/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
/*
* 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>
struct node{
int data;
struct node *previous;
struct node *next;
};
int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);
addNode(5);
return 0;
}
OUTPUT:-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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;
}
/**
* 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));
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));
newNode->data = data;
newNode->prev = last; // Link new
node with the previous node
newNode->next = NULL;
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++;
/**
* 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;
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;
if (last != NULL)
last->next = NULL; // Remove link to of
2nd last node with last node
/**
* 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;
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;
}
int Top()
{
return top->data;
}
int isempty()
{
return top==NULL;
}
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:-
#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