0% found this document useful (0 votes)
11 views23 pages

Exp-4 & 5

The document outlines exercises for implementing data structures including doubly linked lists and circular linked lists, with detailed C code for various operations such as insertion, deletion, and traversal. It also covers stack operations using arrays and linked lists, evaluating postfix expressions, and checking for balanced parentheses. Each section includes code snippets and explanations for the respective data structure implementations.

Uploaded by

hodcsdm
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)
11 views23 pages

Exp-4 & 5

The document outlines exercises for implementing data structures including doubly linked lists and circular linked lists, with detailed C code for various operations such as insertion, deletion, and traversal. It also covers stack operations using arrays and linked lists, evaluating postfix expressions, and checking for balanced parentheses. Each section includes code snippets and explanations for the respective data structure implementations.

Uploaded by

hodcsdm
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/ 23

Exercise 4: Double Linked List Implementation

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;

if (pos < 1 || pos > i + 1)


{
printf("Please enter a valid position\n");
}
else if (pos == 1) {
insertatbegin(k);
}

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

void delatpos(int pos)


{
node **pre, **aft;
if (pos < 1 || pos > i + 1)
{
printf("Please enter a valid position\n");
}

else if (pos == 1)
{
delatbegin();
}
else if (pos == i)
{
delatend();
}
else
{
// Src node to find which
// node to be deleted
node* src = head;
pos--;

// Traverse node till pos


while (pos--) {
src = src->next;
}

// previous and after node


// of the src node

pre = &(src->prev);
aft = &(src->next);

// Change the next and prev


// pointer of pre and aft node
(*pre)->next = (*aft);
(*aft)->prev = (*pre);

// Decrease the length of the


// Linked List
i--;
}
}

// Driver Code
int main()
{
// Adding node to the linked List
addnode(2);
addnode(4);
addnode(9);
addnode(1);
addnode(21);
addnode(22);

// To print the linked List


printf("Linked List: ");
traverse();

printf("\n");

// To insert node at the beginning


insertatbegin(1);
printf("Linked List after"
" inserting 1 "
"at beginning: ");
traverse();

// To insert at the end


insertatend(0);
printf("Linked List after "
"inserting 0 at end: ");
traverse();

// To insert Node with


// value 44 after 3rd Node
insertatpos(44, 3);
printf("Linked List after "
"inserting 44 "
"after 3rd Node: ");
traverse();

printf("\n");

// To delete node at the beginning


delatbegin();
printf("Linked List after "
"deleting node "
"at beginning: ");
traverse();

// To delete at the end


delatend();
printf("Linked List after "
"deleting node at end: ");
traverse();

// To delete Node at position 5


printf("Linked List after "
"deleting node "
"at position 5: ");
delatpos(5);
traverse();

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


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\n5.Search for an
element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node 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;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}

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

while(ptr -> next != head)


{

printf("%d\n", ptr -> data);


ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}

}
. 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.

i) Implement a stack using arrays and linked lists.

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

void push(struct sNode** top_ref, int new_data);


int pop(struct sNode** top_ref);
bool isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
bool areBracketsBalanced(char exp[])
{
int i = 0;
struct sNode* stack = NULL;
while (exp[i])
{
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
push(&stack, exp[i]);

if (exp[i] == '}' || exp[i] == ')'


|| exp[i] == ']') {

if (stack == NULL)
return 0;

else if (!isMatchingPair(pop(&stack), exp[i]))


return 0;
}
i++;
}

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

void push(struct sNode** top_ref, int new_data)


{
struct sNode* new_node
= (struct sNode*)malloc(sizeof(struct sNode));

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

int pop(struct sNode** top_ref)


{
char res;
struct sNode* top;

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

You might also like