Unit-I DS Introduction to Data Structures - Programs
Unit-I DS Introduction to Data Structures - Programs
#include<stdio.h>
#include<stdlib.h>
//node declaration
struct node
{
int data;
struct node* next;
};
//head declaration
struct node* head;
//function declarations
void insertAtBegining(int item);
void insertAtEnd(int item);
void insertRandom(int item,int loc);
void deleteFromBegining();
void deleteFromEnd();
void deleteRandom(int loc);
void display();
void search(int item);
//Driver code
int main(void)
{
//Inserting Nodes
insertAtBegining(23); //23->NULL
insertAtEnd(32); //23->32->NULL
insertRandom(68,0); //23->68->32->NULL
//Searching for 32
search(32);
//Deleting Nodes
deleteFromBegining(); //68->32->NULL
deleteRandom(0); //68->NULL
deleteFromEnd(); //SLL is Empty
//Deleting node from any random location of the Singly Linked List
void deleteRandom(int loc)
{
struct node* temp1;
struct node* temp2;
int i;
if(head == NULL)
printf("\nSLL is empty...:(");
else
{
temp1=head;
for(i=0;i<=loc;i++)
{
temp2 = temp1;
temp1 = temp1->next;
if(temp1 == NULL)
{
printf("\nCan't be deleted (Select different location)");
return;
}
}
temp2->next = temp1->next;
printf("\n%d Node deleted from location %d", temp1->data, loc+1);
free(temp1);
}
}
//Searching for any specific data item into the Singly Linked List
void search(int item)
{
struct node* temp;
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
int i=0,flag;
temp = head;
if(temp == NULL)
printf("\nSLL is Empty...:(");
else
{
while (temp!=NULL)
{
if(temp->data == item)
{
printf("\n%d found at location %d", item, i);
flag=0;
break;
}
else
flag=1;
i++;
temp = temp->next;
}
if(flag==1)
printf("\n%d not found", item);
}
}
//Traversal Operation
void display()
{
struct node* temp;
temp = head;
if(temp == NULL)
printf("\nSLL is Empty...:(");
else
{
printf("\nSingly Linked List(SLL) is:\n");
while (temp!=NULL)
{
printf("%d",temp->data);
temp = temp -> next;
if(temp!=NULL)
printf(" -> ");
else
printf(" -> NULL ");
}
}
}
//Global Declarations
int stack[100],top=-1,max=5,choice=0,i;
//Function Declarations
bool isEmpty();
bool isFull();
void push(int item);
void pop();
void display();
void peek();
//Driver code
int main(void)
{
//Push items until the stack is full
push(20);
push(32);
push(45);
push(19);
push(7);
push(2);
//node declaration
struct node
{
int data;
struct node *next;
};
//Driver code
void main(void)
{
//Pushing items onto the stack
push(32);
push(82);
push(21);
push(26);
Stack Applications
Infix Expression to Postfix Expression Conversion
#include <stdio.h>
#include<stdbool.h>
#include <stdlib.h>
//Global declarations
char stack[30];
char postfixExp[30];
int max=30, top=-1;
//Function declarations
bool isEmpty();
bool isFull();
char peek();
void push(char);
char pop();
bool checkIfOperand(char);
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
int precedence(char);
int infixToPostfix(char*);
//Driver Code
int main(void)
{
char infixExp[] = "a+b*(c^d-e)^(f+g*h)-i";
printf("Given Infix Expression: %s", infixExp);
infixToPostfix(infixExp);
return 0;
}
bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}
bool isFull()
{
if(top==max-1)
return true;
else
return false;
}
char peek()
{
return stack[top];
}
char pop()
{
if(isEmpty())
return -1;
char ch=stack[top];
top=top-1;
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
return(ch);
}
case '*':
case '/':
case '%': return 2;
//Global Declarations
int stack[30], max=30, top=-1;
//Function Declarations
bool isEmpty();
bool isFull();
void push(int);
int pop();
int isOperator(char);
int evaluate(char*);
//Driver Code
int main(void)
{
char expression[] = "5 6 7 + * 8 -";
int result = evaluate(expression);
printf("Result= %d\n", result);
return 0;
}
int pop()
{
int item;
if (isEmpty())
printf("Underflow...:(\n");
else
{
item = stack[top];
top=top-1;
return item;
}
}
//Global Declarations
int queue[30], front=-1, rear=-1, max=5, choice=0;
//Function Declarations
bool isEmpty();
bool isFull();
void enQueue(int item);
void deQueue();
void peekFront();
void peekRear();
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
void display();
//Driver Code
int main(void)
{
//enQueue items till Queue becomes full
enQueue(34);
enQueue(12);
enQueue(32);
enQueue(76);
enQueue(23);
enQueue(67);
//Function Definitions
bool isEmpty()
{
if(front==-1 || front>rear)
return true;
else
return false;
}
bool isFull()
{
if(rear==max-1)
return true;
else
return false;
}
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
void enQueue(int item)
{
if(isFull())
printf("\nOverflow...:(\n");
else
{
if(front==-1 && rear==-1)
front=rear=0;
else
rear=rear+1;
queue[rear] = item;
printf("\n%d is successfully enQueued...:)",item);
}
}
void deQueue()
{
int item;
if (isEmpty())
printf("\nUnderflow...:(\n");
else
{
item = queue[front];
if(front==rear)
front = rear= -1;
else
front = front + 1;
printf("\n%d is successfully deQueued...:)",item);
}
}
void peekFront()
{
if(isEmpty())
printf("\nQueue is Empty...:(\n");
else
printf("\n%d is at the front of the queue",queue[front]);
}
void peekRear()
{
if(isEmpty())
printf("\nQueue is Empty...:(\n");
else
printf("\n%d is at the rear of the queue",queue[rear]);
}
void display()
{
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
int i;
if(isEmpty())
printf("\nQueue is Empty...:(\n");
else
{
printf("\nQueue Elements Are:\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}
//node declaration
struct node
{
int data;
struct node* next;
};
//Global declarations
struct node* front=NULL;
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
struct node* rear=NULL;
int choice=0;
//function declarations
bool isEmpty();
void enQueue(int item);
void deQueue();
void peekFront();
void peekRear();
void display();
//Driver Code
int main(void)
{
//enQueue items
enQueue(35);
enQueue(89);
enQueue(56);
enQueue(48);
//Display Queue
display();
//function defintion
bool isEmpty()
{
if(front==NULL)
return true;
else
return false;
}
void deQueue()
{
struct node* temp;
if(isEmpty())
printf("\nUnderflow...:(");
else
{
temp = front;
front = front->next;
printf("\n%d successfully deQueued...:)", temp->data);
free(temp);
}
}
void peekFront()
{
if(isEmpty())
printf("\nQueue is Empty...:(");
else
printf("\n%d is at the front of the Queue",front->data);
}
void peekRear()
{
if(isEmpty())
printf("\nQueue is Empty...:(");
else
printf("\n%d is at the rear of the Queue",rear->data);
}