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

Practical DS 2024

The document contains multiple C programs demonstrating different data structures and algorithms, including stack implementation using arrays and dynamic memory allocation, Fibonacci series calculation, and inserting a node in the middle of a singly linked list. Each program includes code snippets, explanations, and sample outputs. The programs illustrate fundamental concepts in data structures such as stack operations and linked list manipulation.

Uploaded by

agkhandvd
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

Practical DS 2024

The document contains multiple C programs demonstrating different data structures and algorithms, including stack implementation using arrays and dynamic memory allocation, Fibonacci series calculation, and inserting a node in the middle of a singly linked list. Each program includes code snippets, explanations, and sample outputs. The programs illustrate fundamental concepts in data structures such as stack operations and linked list manipulation.

Uploaded by

agkhandvd
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/ 7

1. Write a program in C to implement Stack using Array.

Solution:
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Output
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit

Enter choice: 1
Enter value to be pushed: 2
Enter choice: 1
Enter value to be pushed: 6
Enter choice: 1
Enter value to be pushed: 8
Enter choice: 1
Enter value to be pushed: 7
Enter choice: 2
The popped element is 7
Enter choice: 3
Stack elements are:8 6 2
Enter choice: 5
Invalid Choice
Enter choice: 4
Exit

2. Write a program in C using dynamic memory allocation by which stack can be


implemented.
#include <iostream.h> // Including the Input/Output Stream Library

class Stack {
private:
int * array; // Dynamic array to store the stack elements
int top; // Index of the top element
int capacity; // Maximum capacity of the stack

public:
// Constructor to initialize the stack with a given size
Stack(int size) {
capacity = size;
array = new int[capacity]; // Allocate memory for the stack
top = -1; // Initialize top as -1 to indicate an empty stack
}

// Destructor to deallocate memory when the object is destroyed


~Stack() {
delete[] array; // Free the dynamically allocated memory for the stack
}

// Function to push an element onto the stack


void push(int value) {
if (top == capacity - 1) { // Check for stack overflow
cout<< "Stack Overflow. Cannot push element: " << value << endl;
return;
}
array[++top] = value; // Increment top and add the element to the stack
cout << "Pushed element: " << value << endl; // Print the pushed element
}

// Function to pop an element from the stack


void pop() {
if (top == -1) { // Check for stack underflow
cout<< "Stack Underflow. Cannot pop from an empty stack." << endl;
return;
}
int value = array[top--]; // Get the top element and decrement top
cout << "Popped element: " << value << endl; // Print the popped element
}
};

int main() {
int size = 5; // Size of the stack
cout<< "Size of the stack: " << size << "\n" << endl; // Print the stack size
Stack stack(size); // Create an instance of the Stack class with the specified size

// Push elements onto the stack


stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);

// Pop elements from the stack


cout<< "\nPopped 6 elements from the above stack:" << endl;
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop(); // Attempting to pop from an empty stack

return 0; // Returning 0 to indicate successful execution of the program


}
Sample Output:

Size of the stack: 5


Pushed element: 1
Pushed element: 2
Pushed element: 3
Pushed element: 4
Pushed element: 5

Popped 6 elements from the above stack:


Popped element: 5
Popped element: 4
Popped element: 3
Popped element: 2
Popped element: 1
Stack Underflow. Cannot pop from an empty stack.

3. Write a program in C using stack to determine nth element of a Fibonacci


series.
#include<iostream.h>
//Using recursive function
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}

int main()
{
int n;
cout<<”enter the number of nth term of Fibonacci series””;
cin<<n
cout << n << "th Fibonacci Number: " << fib(n);
return 0;
}
Sample output
enter the number of nth term of Fibonacci series 3
3th Fibonacci Number: 2

enter the number of nth term of Fibonacci series 7


7th Fibonacci Number:13
4. Write a program in C to insert a node in the middle of a Singly Linked
List.
Test Data and Expected Output :
Input the number of nodes (3 or more) : 4
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Input data for node 4 : 4
Data entered in the list are :
Data = 1
Data = 2
Data = 3
Data = 4
Input data to insert in the middle of the list : 5
Input the position to insert new node : 3
Insertion completed successfully.
The new list are :
Data = 1
Data = 2
Data = 5
Data = 3
Data = 4
Solution
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the node
}*stnode;
void createNodeList(int n); //function to create the list
void insertNodeAtMiddle(int num, int pos); //function to insert node at the
middle
void displayList(); //function to display the list
int main()
{
int n,num,pos;
printf("\n\n Linked List : Insert a new node at the middle of the
Linked List :\n");
printf("-----------------------------------------------------------------------\n");
printf(" Input the number of nodes (3 or more) : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert in the middle of the list : ");
scanf("%d", &num);
printf(" Input the position to insert new node : " );
scanf("%d", &pos);
if(pos<=1 || pos>=n)
{
printf("\n Insertion can not be possible in that position.\n ");
}
if(pos>1 && pos<n)
{
insertNodeAtMiddle(num, pos);
printf("\n Insertion completed successfully.\n ");
}
printf("\n The new list are : \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 stnode 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;
//Creates n nodes and adds to linked list
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL) //check whether the fnnode is NULL and if so no
memory allocation
{
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 insertNodeAtMiddle(int num, int pos)
{
int i;
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num; //Links the data part
fnNode->nextptr = NULL;
tmp = stnode;
for(i=2; i<=pos-1; i++)
{
tmp = tmp->nextptr;
if(tmp == NULL)
break;
}
if(tmp != NULL)
{
fnNode->nextptr = tmp->nextptr; //Links the address part of new node
tmp->nextptr = fnNode;
}
else
{
printf(" Insert is not possible to the given position.\n");
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the empty list.");
}
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
}
}
}

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