Exp-4 & 5
Exp-4 & 5
i) Implement a doubly linked list and perform various operations to understand its properties and
applications.
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
i) Implement a doubly linked list and perform various operations to understand its properties and
applications.
Program:
#include <stdio.h>
#include <stdlib.h>
int i = 0;
typedef struct DLIST
{
int key;
struct DLIST *prev;
struct DLIST *next;
}node;
node* head = NULL;
node* first = NULL;
node* temp = NULL;
node* tail = NULL;
void addnode(int k)
{
node* ptr= (node*)malloc(sizeof(node));
ptr->key = k;
ptr->next = NULL;
ptr->prev = NULL;
if (head == NULL)
{
head = ptr;
first = head;
tail = head;
}
else
{
temp=ptr;
first->next=temp;
temp->prev = first;
first = temp;
tail = temp;
}
i++;
}
void traverse()
{
node* ptr = head;
while (ptr != NULL)
{
printf("%d ", ptr->key);
ptr = ptr->next;
}
printf("\n");
}
void insertatbegin(int k)
{
node* ptr
= (node*)malloc(sizeof(node));
ptr->key = k;
ptr->next = NULL;
ptr->prev = NULL;
if (head == NULL)
{
first = ptr;
first = head;
tail = head;
}
else
{
temp = ptr;
temp->next = head;
head->prev = temp;
head = temp;
}
i++;
}
void insertatend(int k)
{
node* ptr= (node*)malloc(sizeof(node));
ptr->key = k;
ptr->next = NULL;
ptr->prev = NULL;
if (head == NULL)
{
first = ptr;
first = head;
tail = head;
}
else
{
temp = ptr;
temp->prev = tail;
tail->next = temp;
tail = temp;
}
i++;
}
void insertatpos(int k, int pos)
{
node **da, **ba,*ptr;
else if (pos == i + 1)
{
insertatend(k);
}
else
{
node* src = head;
while (pos--)
{
src = src->next;
}
ptr= (node*)malloc(sizeof(node));
ptr->next = NULL;
ptr->prev = NULL;
ptr->key = k;
ba = &src;
da = &(src->prev);
ptr->next = (*ba);
ptr->prev = (*da);
(*da)->next = ptr;
(*ba)->prev = ptr;
i++;
}
}
void delatbegin()
{
head = head->next;
i--;
}
void delatend()
{
tail = tail->prev;
tail->next = NULL;
i--;
}
else if (pos == 1)
{
delatbegin();
}
else if (pos == i)
{
delatend();
}
else
{
// Src node to find which
// node to be deleted
node* src = head;
pos--;
pre = &(src->prev);
aft = &(src->next);
// Driver Code
int main()
{
// Adding node to the linked List
addnode(2);
addnode(4);
addnode(9);
addnode(1);
addnode(21);
addnode(22);
printf("\n");
printf("\n");
return 0;
}
Output:
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
Program :-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
}
}
void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
}
. Exercise 5: Stack Operations
i) Implement a stack using arrays and linked lists.
ii) Write a program to evaluate a postfix expression using a stack.
iii) Implement a program to check for balanced parentheses using a stack.
Program:-
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct stack
{
int data;
struct stack *next;
};
struct stack *top = NULL;
struct stack *push(struct stack *, int);
struct stack *display(struct stack *);
struct stack *pop(struct stack *);
int main()
{
int val, option;
clrscr();
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
top = push(top, val);
break;
case 2:
top = pop(top);
break;
case 3:
top = display(top);
break;
}
}while(option <=3);
return 0;
}
struct stack *push(struct stack *top, int val)
{
struct stack *ptr;
ptr = (struct stack*)malloc(sizeof(struct stack));
ptr -> data = val;
if(top == NULL)
{
ptr -> next = NULL;
top = ptr;
}
else
{
ptr -> next = top;
top = ptr;
}
return top;
}
struct stack *display(struct stack *top)
{
struct stack *ptr;
ptr = top;
if(top == NULL)
printf("\n STACK IS EMPTY");
else
{
while(ptr != NULL)
{
printf("\n %d", ptr -> data);
ptr = ptr -> next;
}
}
return top;
}
struct stack *pop(struct stack *top)
{
struct stack *ptr;
ptr = top;
if(top == NULL)
printf("\n STACK UNDERFLOW");
else
{
top = top -> next;
printf("\n The value being deleted is: %d", ptr -> data);
free(ptr);
}
return top;
}
Output:-
ii) Write a program to evaluate a postfix expression using a stack
Program:-
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
char st[MAX];
int top=-1;
void push(char st[], char);
char pop(char st[]);
void InfixtoPostfix(char source[], char target[]);
int getPriority(char);
int main()
{
char infix[100], postfix[100];
clrscr();
printf("\n Enter any infix expression : ");
gets(infix);
strcpy(postfix, "");
InfixtoPostfix(infix, postfix);
printf("\n The corresponding postfix expression is : ");
puts(postfix);
getch();
return 0;
}
void InfixtoPostfix(char source[], char target[])
{
int i=0, j=0;
char temp;
strcpy(target, "");
while(source[i]!='\0')
{
if(source[i]=='(')
{
push(st, source[i]);
i++;
}
else if(source[i] == ')')
{
while((top!=-1) && (st[top]!='('))
{
target[j] = pop(st);
j++;
}
if(top==-1)
{
printf("\n INCORRECT EXPRESSION");
exit(1);
}
temp = pop(st);//remove left parenthesis
i++;
}
else if(isdigit(source[i]) || isalpha(source[i]))
{
target[j] = source[i];
j++;
i++;
}
else if (source[i] == '+' || source[i] == '-' || source[i] == '*' ||
source[i] == '/' || source[i] == '%')
{
while( (top!=-1) && (st[top]!= '(') && (getPriority(st[top])
> getPriority(source[i])))
{
target[j] = pop(st);
j++;
}
push(st, source[i]);
i++;
}
else
{
printf("\n INCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=-1) && (st[top]!='('))
{
target[j] = pop(st);
j++;
}
target[j]='\0';
}
int getPriority(char op)
{
if(op=='/' || op == '*' || op=='%')
return 1;
else if(op=='+' || op=='-')
return 0;
}
void push(char st[], char val)
{
if(top==MAX-1)
printf("\n STACK OVERFLOW");
else
{
top++;
st[top]=val;
}
}
char pop(char st[])
{
char val=' ';
if(top==-1)
printf("\n STACK UNDERFLOW");
else
{
val=st[top];
top--;
}
return val;
}
Output:-
.
iii) Implement a program to check for balanced parentheses using a stack.
Program :
#include <stdio.h>
#include <stdlib.h>
#define bool int
struct sNode {
char data;
struct sNode* next;
};
if (stack == NULL)
return 0;
if (stack == NULL)
return 1; // balanced
else
return 0; // not balanced
}
int main()
{
char exp[100];
printf("Enter the expression :");
gets(exp);
// Function call
if (areBracketsBalanced(exp))
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}
if (new_node == NULL) {
printf("Stack overflow n");
getch();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
if (*top_ref == NULL) {
printf("Stack overflow n");
getche();
exit(0);
}
else {
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
return 0;
}