0% found this document useful (0 votes)
2 views22 pages

Unit-I DS Introduction to Data Structures - Programs

The document provides implementations of data structures including a Singly Linked List and Stack using both arrays and linked lists in C. It includes functions for inserting, deleting, displaying, and searching elements in the linked list, as well as pushing, popping, and peeking elements in the stack. Additionally, it covers applications such as converting infix expressions to postfix and evaluating postfix expressions.

Uploaded by

mdafzal.aiml
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 views22 pages

Unit-I DS Introduction to Data Structures - Programs

The document provides implementations of data structures including a Singly Linked List and Stack using both arrays and linked lists in C. It includes functions for inserting, deleting, displaying, and searching elements in the linked list, as well as pushing, popping, and peeking elements in the stack. Additionally, it covers applications such as converting infix expressions to postfix and evaluating postfix expressions.

Uploaded by

mdafzal.aiml
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/ 22

Singly Linked List Implementation

#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

//Display the SLL 23->68->32->NULL


display();

//Searching for 32
search(32);

//Deleting Nodes
deleteFromBegining(); //68->32->NULL
deleteRandom(0); //68->NULL
deleteFromEnd(); //SLL is Empty

//Display the SLL


display();
}

//Inserting node at the begining of the Singly Linked List


void insertAtBegining(int item)
{
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
struct node* newNode;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("\nMemory Overflow...:(");
else
{
newNode->data = item;
newNode->next = head;
head = newNode;
printf("\n%d Node Inserted Successfully at the begining of SLL...:)",item);
}
}

//Inserting node at the end of the Singly Linked List


void insertAtEnd(int item)
{
struct node* newNode;
struct node* temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("\nMemory Overflow...:(");
else
{
newNode->data = item;

//When the list is empty


if(head == NULL)
{
newNode->next = NULL;
head = newNode;
printf("\n%d Node Inserted Successfully at the end of SLL...:)",item);
}
else
{
temp = head;
while (temp->next!=NULL)
temp = temp->next;
temp->next = newNode;
newNode->next = NULL;
printf("\n%d Node Successfully Inserted at the end of SLL...:)",item);
}
}
}

//Inserting node at any random location of the Singly Linked List


void insertRandom(int item, int loc)
{
int i;
struct node* newNode;
struct node* temp;
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
printf("\nMemory Overflow...:(");
else
{
newNode->data = item;
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\nCan't be inserted (Select different location)\n");
return;
}
}
newNode->next = temp->next;
temp->next = newNode;
printf("\n%d Node Inserted after %d Successfully...:)",item,loc);
}
}

//Deleting node from the begining of the Singly Linked List


void deleteFromBegining()
{
struct node* temp;
if(head == NULL)
printf("\nList is empty...:(");
else
{
temp = head;
head = temp->next;
printf("\n%d Node deleted from the begining of SLL...:)", temp->data);
free(temp);
}
}

//Deleting node from the end of the Singly Linked List


void deleteFromEnd()
{
struct node* temp1;
struct node* temp2;
//When the list is empty
if(head == NULL)
printf("\nList is empty...:(");

//When there is only one node present in the list


else if(head->next == NULL)
{
temp1 = head;
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
head = NULL;
printf("\n%d Only node deleted from SLL...:)", temp1->data);
free(temp1);
}
else
{
temp1 = head;
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1 ->next;
}
temp2->next = NULL;
printf("\n%d Node deleted from the end of the SLL...:)", temp1->data);
free(temp1);
}
}

//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 ");
}
}
}

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN


Email: [email protected], Mob: 8179700193
Output:

Stack Implementation Using Array


#include<stdio.h>
#include<stdbool.h>

//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);

//Display the Stack items


display();

//Peeking the top of the stack


peek();

//Pop items until the stack is empty


Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
pop();
pop();
pop();
pop();
pop();
pop();
}

//Checking the emptiness of the stack


bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}

//Checking the fullness of the stack


bool isFull()
{
if(top==max-1)
return true;
else
return false;
}

//Pushing an item onto the stack


void push(int item)
{
if (isFull())
printf("\nOverflow...:(\n");
else
{
top = top+1;
stack[top] = item;
printf("\n%d pushed successfully onto the stack", item);
}
}

//Poping an item from the stack


void pop()
{
if(isEmpty())
printf("\nUnderflow...:(\n");
else
{
printf("\n%d poped successfully from the stack", stack[top]);
top = top-1;
}
}
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
//Displaying the items from the stack
void display()
{
if(isEmpty())
printf("\nStack is Empty...:(\n");
else
{
printf("\nStack items are:\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}

//Peeking the item from top of the stack


void peek()
{
if(isEmpty())
printf("\nStack is Empty...:(\n");
else
printf("Item at top of the stack is: %d", stack[top]);
}
Output:

Stack Implementation Using Linked List (Pointer)


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

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN


Email: [email protected], Mob: 8179700193
//function declarations
bool isEmpty();
void push(int item);
void pop();
void display();
void peek();

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

//top of the stack


struct node* top=NULL;

//Driver code
void main(void)
{
//Pushing items onto the stack
push(32);
push(82);
push(21);
push(26);

//Display the stack items


display();

//Peeking the top of the stack


peek();

//Poping items from the stack


pop();
pop();
pop();
pop();
pop();

//Display the stack items


display();
}

//Checking the emptiness of the stack


bool isEmpty()
{
if(top==NULL)
return true;
else
return false;
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
}

//Pusing an Item onto the stack


void push(int item)
{
struct node* newNode=(struct node*)malloc(sizeof(struct node));
if(newNode==NULL)
printf("\nMemory Overflow...:(");
else
{
newNode->data=item;
newNode->next=top;
top=newNode;
printf("\n%d is pushed successfully onto the stack...:)",item);
}
}

//Poping an item from the stack


void pop()
{
int item;
struct node* temp;
if (isEmpty())
printf("\nUnderflow...:(\n");
else
{
item=top->data;
temp=top;
top=top->next;
free(temp);
printf("\n%d is poped successfully from the stack...:)",item);
}
}

//Displaying items from the stack


void display()
{
struct node* temp;
temp=top;
if(isEmpty())
printf("\nStack is empty...:(\n");
else
{
printf("\nStack elements are:\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
}

//Peeking the item from top of the stack


void peek()
{
if(isEmpty())
printf("\nStack is Empty...:(\n");
else
printf("\nItem at top of the stack is: %d", top->data);
}
Output:

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];
}

void push(char ch)


{
if(isFull())
printf("Stack Full...:(\n");
else
{
top=top+1;
stack[top]=ch;
}
}

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);
}

bool checkIfOperand(char ch)


{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}

int precedence(char ch)


{
switch (ch)
{
case '+':
case '-': return 1;

case '*':
case '/':
case '%': return 2;

case '^': return 3;


}
return -1;
}

int infixToPostfix(char* infixExp)


{
int i, j;
for (i = 0, j = -1; infixExp[i]; ++i)
{
if(checkIfOperand(infixExp[i]))
postfixExp[++j] = infixExp[i];
else if(infixExp[i] == '(')
push(infixExp[i]);
else if(infixExp[i] == ')')
{
while(!isEmpty() && peek() != '(')
postfixExp[++j] = pop();
if(!isEmpty() && peek() != '(')
return -1;
else
pop();
}
else
{
while(!isEmpty() && precedence(infixExp[i]) <= precedence(peek()))
postfixExp[++j] = pop();
push(infixExp[i]);
}
}
while(!isEmpty())
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
postfixExp[++j] = pop();
postfixExp[++j] = '\0';
printf("\nPostfix Expression: %s", postfixExp);
}

Postfix Expression Evaluation


#include<stdio.h>
#include<stdbool.h>

//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;
}

//Checking the emptiness of the stack


bool isEmpty()
{
if(top==-1)
return true;
else
return false;
}

//Checking the fullness of the stack


bool isFull()
{
if(top==max-1)
return true;

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN


Email: [email protected], Mob: 8179700193
else
return false;
}

void push(int item)


{
if(isFull())
printf("Overflow...:(\n");
else
{
top=top+1;
stack[top]=item;
}
}

int pop()
{
int item;
if (isEmpty())
printf("Underflow...:(\n");
else
{
item = stack[top];
top=top-1;
return item;
}
}

int isOperator(char symbol)


{
if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/')
return 1;
return 0;
}

int evaluate(char* expression)


{
int i = 0;
char symbol = expression[i];
int operand1, operand2, result;

while (symbol != '\0')


{
if (symbol >= '0' && symbol <= '9')
{
int num = symbol - '0';
push(num);
}
else if (isOperator(symbol))
{
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
operand2 = pop();
operand1 = pop();
switch(symbol)
{
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
push(result);
}
i++;
symbol = expression[i];
}
result = pop();
return result;
}

Queue Implementation Using Arrays


//Queue Implementation Using Arrays
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//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);

//Display the Queue


display();

//Peek the front item from the Queue


peekFront();

//Peek the rear item from the Queue


peekRear();

//deQueue items till Queue becomes empty


deQueue();
deQueue();
deQueue();
deQueue();
deQueue();
deQueue();

//Display the Queue


display();
}

//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]);
}
}

Queue Implementation Using Linked List (Pointers)


//Queue Implementation Using Linked list(Pointer)
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

//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();

//Peek the front item from the Queue


peekFront();

//Peek the rear item from the Queue


peekRear();

//deQueue items till Queue becomes empty.


deQueue();
deQueue();
deQueue();
deQueue();
deQueue();
}

//function defintion
bool isEmpty()
{
if(front==NULL)
return true;
else
return false;
}

void enQueue(int item)


{
struct node* newNode;
newNode = (struct node*)malloc(sizeof(struct node));
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN
Email: [email protected], Mob: 8179700193
if(newNode == NULL)
printf("\nMemory Overflow...:(\n");
else
{
newNode->data = item;
if(isEmpty())
{
front = rear = newNode;
rear->next = NULL;
}
else
{
rear -> next = newNode;
rear = newNode;
rear->next = NULL;
}
printf("\n%d successfully enQueued...:)", item);
}
}

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);
}

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN


Email: [email protected], Mob: 8179700193
void display()
{
struct node* temp=front;
if(isEmpty())
printf("\nQueue is Empty...:(");
else
{
printf("\nQueue Elements are: \n");
while(temp != NULL)
{
printf("%d\n",temp->data);
temp = temp->next;
}
}
}

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) @ SPHN


Email: [email protected], Mob: 8179700193

You might also like