Haris Dsa
Haris Dsa
1
IMPLEMENTATION OF ARRAY TO FIND PAIR OF ELEMENT
Date:
AIM :
PSEUDOCODE :
BEGIN
LOOP for(i=0;i<=n-2;i++)
LOOP for(j=i+1;j<=n-1;j++)
CONDITION(arr[i]+arr[j]==target
)
return i,j
f=0
END
SOURCE CODE
#include <stdio.h>
int main() {
int n, target;
int i, j;
printf(“Name: R.HARISH\nRoll No: 717823L121\n”);
printf(“Enter the size of array: “);
scanf(“%d”, &n);
printf(“Enter the target”);
scanf(“%d”,&target);
int arr[n];
Printf(“Enter the array elements: “);
for (i= 0; i<n; i++) {
scanf(“%d”, &arr[i]);
}
for (i= 0; i < n – 1; i++) {
for (j = i+ 1; j < n; j++) {
if (arr[i] + arr[j] == target) {
printf(“INDICES OF THE TARGET VALUES:%d %d”, i, j);
return 0;
}
}
}
1 717823L121
printf(“-1”);
return 0;
}
OUTPUT :
RESULT :
2 717823L121
Ex no: 1.2
IMPLEMENTATION OF STACK USING ARRAY
Date:
AIM :
PSEUDOCODE:
BEGIN
DECLARE stack[100] AS INTEGER ARRAY
DECLARE choice, n, top, x, i AS INTEGER
INITIALIZE top TO -1
PRINT "Enter the size of STACK[MAX=100]: "
READ n
PRINT "\n\t STACK OPERATIONS USING ARRAY"
PRINT "\n\t "
PRINT "\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT"
DO
PRINT "Enter the Choice: "
READ choice
CASE choice
WHEN 1:
CALL push
WHEN 2:
CALL pop
WHEN 3:
CALL display
WHEN 4:
PRINT "\n\t EXIT POINT "
ELSE:
PRINT "\n\t Please Enter a Valid Choice(1/2/3/4)"
END CASE
WHILE choice != 4
END PROGRAM
push
IF top >= n-1 THEN
PRINT "\n\tSTACK is over flow"
ELSE
PRINT " Enter a value to be pushed: "
3 717823L121
READ x
top = top + 1
stack[top] = x
END IF
END
pop
IF top <= -1 THEN
PRINT "\n\t Stack is under flow"
ELSE
PRINT "\n\t The popped elements is " stack[top]
top = top - 1
END IF
END
display
IF top >= 0 THEN
PRINT "\n The elements in STACK \n"
FOR i = top DOWN TO 0
PRINT "\n" stack[i]
END FOR
PRINT "\n Press Next Choice"
ELSE
PRINT "\n The STACK is empty"
END IF
END
SOURCE CODE :
#include<stdio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);
void display(void);
int main() {
top = -1;
switch (choice) {
case 1: {
Push();
Break;
}
case 2: {
Pop();
Break;
}
case 3: {
Display();
Break;
}
case 4: {
Printf(“\n\tEXITING…….\n”);
Break;
}
default: {
printf(“You entered an invalid choice.\n”);
printf(“\n\tPlease Enter a Valid Choice (1/2/3/4)\n”);
}
}
} while (choice != 4);
return 0;
}
void push() {
if (top >= n – 1) {
printf(“\n\tSTACK is overflow\n”);
} else {
printf(“Enter a value to be pushed: “);
scanf(“%d”, &x);
top++;
Stack[top] = x;
printf(“The value has been pushed into the stack.\n”);
}
}
5 717823L121
void pop() {
if (top <= -1) {
printf(“\n\tSTACK is underflow\n”);
} else {
printf(“\n\tThe popped element is %d\n”, stack[top]);
top--;
}
}
void display() {
if (top >= 0) {
printf(“\nThe elements in STACK are: \n”);
for (I = top; I >= 0; i--) {
printf(“%d\n”, stack[i]);
}
printf(“CHOOSE NEXT CHOICE\n”);
} else {
print(“\nThe STACK is empty\n”);
}
}
6 717823L121
OUTPUT:
RESULT :
7 717823L121
Ex no: 1.3
IMPLEMENTATION OF QUEUE USING ARRAY
Date:
AIM :
PSEUDOCODE:
BEGIN
DECLARE queue[n] AS INTEGER ARRAY
DECLARE ch, front, rear, i, j, x AS INTEGER
WHILE ch != 0
PRINT "\nEnter the Choice: "
READ ch
CASE ch
WHEN 1:
IF rear == x THEN
PRINT "\n Queue is Full"
ELSE
PRINT "\n Enter no " j ": "
READ queue[rear]
rear = rear + 1
j=j+1
END IF
WHEN 2:
IF front == rear THEN
PRINT "\n Queue is empty"
ELSE
PRINT "\n Deleted Element is " queue[front]
front = front + 1
x=x+1
8 717823L121
END IF
WHEN 3:
PRINT "\nQueue Elements are:\n "
IF front == rear THEN
PRINT "\n Queue is Empty"
ELSE
FOR i = front TO rear - 1
PRINT queue[i]
PRINT "\n"
END FOR
END IF
WHEN 4:
EXIT
ELSE:
PRINT "Wrong Choice: please see the options"
END CASE
END WHILE
END
SOURCE CODE:
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
Printf(" Name : R.HARISH\nRoll number : 717823L121\n") ;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
9 717823L121
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Invalid option….");
}
}
}
return 0;
}
10 717823L121
OUTPUT:
RESULT :
AIM :
PSEUDOCODE:
BEGIN PROGRAM
DECLARE queue[capacity] AS INTEGER ARRAY
DECLARE front, rear AS INTEGER
FUNCTION checkFull
IF (front == rear + 1) OR (front == 0 AND rear == capacity - 1) THEN
RETURN 1
ELSE
RETURN 0
END IF
END FUNCTION
FUNCTION checkEmpty
IF front == -1 THEN
RETURN 1
ELSE
RETURN 0
END IF
END FUNCTION
PROCEDURE enqueue(value)
IF checkFull() THEN
PRINT "Overflow condition"
ELSE
IF front == -1 THEN
front = 0
END IF
12 717823L121
rear = (rear + 1) % capacity
queue[rear] = value
PRINT " " value " was enqueued to circular queue"
END IF
END PROCEDURE
FUNCTION dequeue
DECLARE variable AS INTEGER
IF checkEmpty() THEN
PRINT "Underflow condition"
RETURN -1
ELSE
variable = queue[front]
PRINT " " variable " was dequeued from circular queue"
RETURN 1
END IF
END FUNCTION
PROCEDURE print
DECLARE i AS INTEGER
IF checkEmpty() THEN
PRINT "Nothing to dequeue"
ELSE
PRINT "\nThe queue looks like: \n"
FOR i = front TO rear - 1
PRINT queue[i] " "
END FOR
PRINT queue[rear] " \n\n"
END IF
END PROCEDURE
dequeue()
13 717823L121
enqueue(12)
enqueue(24)
enqueue(36)
enqueue(48)
enqueue(60)
print()
dequeue()
dequeue()
print()
enqueue(10
0)
enqueue(20
0)
enqueue(30
0)
enqueue(50
0) print()
END
SOURCE CODE:
#include <stdio.h>
#define capacity 6
int queue[capacity];
int front = -1, rear = -1;
int checkFull() {
if ((front == rear + 1) || (front == 0 && rear == capacity – 1)) {
return 1;
}
return 0;
}
14 717823L121
Int checkEmpty() {
if (front == -1) {
return 1;
}
return 0;
}
int dequeue() {
int variable;
if (checkEmpty()) {
printf(“Underflow condition\n”);
return -1;
} else {
Variable = queue[front];
if (front == rear) {
// Queue has only one element, so reset it
front = rear = -1;
} else{
front = (front + 1) % capacity;
}
printf(“%d was dequeued from circular queue\n”, variable);
return 1;
}
}
15 717823L121
Void print() {
int I;
if (checkEmpty()) {
printf(“Nothing to dequeue\n”);
} else {
printf(“\nThe queue elements are: \n”);
for (i= front; i!= rear; i= (i⁶ + 1) % capacity) {
prmntf(“%d “, queue[i]);
}
printf(“%d \n\n”, queue[i]);
}
}
int main() {
Printf(“Name: R.HARISH \nRoll number: 717823L121\n”);
dequeue ();
enqueue(12)
enqueue(24)
enqueue(36)
enqueue(48)
enqueue(60)
Print();
dequeue();
dequeue();
Print();
enqueue(100)
enqueue(200)
enqueue(300)
enqueue(500)
Print();
Return 0;
}
16 717823L121
OUTPUT:
RESULT :
17 717823L121
Ex no: 2.1
IMPLEMENTATION OF SINGLY LINKED LIST
Date:
AIM:
To write a C- Program for implementation of Singly Linked List.
PSEUDOCODE:
BEGIN
DECLARE head AS NODE POINTER
FUNCTION deleteStart(headPtr)
DECLARE temp AS NODE POINTER
IF headPtr == NULL THEN
PRINT "Impossible to delete from empty Singly Linked List"
ELSE
temp = headPtr
headPtr = headPtr->next
PRINT "Deleted: " temp->data
FREE temp
END IF
END FUNCTION
FUNCTION insertStart(headPtr, data)
DECLARE newNode AS NODE POINTER
ALLOCATE newNode
newNode->data = data
newNode->next = headPtr
headPtr = newNode
PRINT "Inserted " newNode->data
END FUNCTION
PROCEDURE display(nodePtr)
PRINT "\nLinked List: "
WHILE nodePtr != NULL
PRINT nodePtr->data " "
nodePtr = nodePtr->next
END WHILE
PRINT "\n"
END PROCEDURE
head = NULL
insertStart(head, 46)
insertStart(head, 70)
insertStart(head, 36)
18 717823L121
insertStart(head, 20)
insertStart(head, 18)
display(head)
deleteStart(head)
deleteStart(head)
display(head)
END
SOURCE CODE:
#include<stdlib.h>
#include<stdio.h>
struct Node{
int data;
struct Node *next;
};
void deleteStart(struct Node** head){
struct Node* temp = *head;
if(*head == NULL){
printf("Impossible to delete from empty Singly Linked List");
return;
}
*head = (*head)->next;
printf("Deleted: %d\n", temp->data);
free(temp);
}
int main()
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ;
{
struct Node* head = NULL;
insertStart(&head,46);
insertStart(&head,70);
insertStart(&head,36);
insertStart(&head,20);
insertStart(&head,18);
display(head);
deleteStart(&head);
deleteStart(&head);
display(head);
return 0;
}
OUT PUT:
RESULT:
Thus the program for implementation of singly linked list was Successfully
executed and the output was verified
20 717823L121
Ex no: 2.2
IMPLEMENTATION OF DOUBLY LINKED LIST
Date:
AIM:
To write a C-Program for implementation of Doubly Linked list.
PSEUDOCODE:
Algorithm insert_begin
BEGIN
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL) then
BEGIN
head=newnode;
tail=newnode;
END
else
BEGIN
newnode->next=head;
head->prev=newnode;
head=newnode;
END
count++;
END
Algorithm insert_end
BEGIN
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
BEGIN
head=newnode;
tail=newnode;
END
else
BEGIN
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
END
count++;
21 717823L121
display();
END
Algorithm insert_pos
BEGIN
scanf("%d",&position);
if(position==1)
insert_begin();
else if(position==count+1)
insert_end();
else if(position>count+1)
printf("Not Possible\n");
else
BEGIN
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
BEGIN
head=newnode;
tail=newnode;
count++;
END
else
BEGIN
temp=head;
printf("Enter the position:");
scanf("%d",&position);
for(i=1;i<position-1;i++)
temp=temp->next;
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
newnode->next->prev=newnode;
END
END
END
Algorithm delete_begin
BEGIN
if(head==NULL)
printf("\nThe list is empty");
else if(head==tail)
BEGIN
free(head);
head=tail=NULL;
END
else
BEGIN
struct node*delnode;
22 717823L121
delnode=head;
head=head->next;
head->prev=NULL;
delnode->next=NULL;
free(delnode);
END
count--;
display();
END
Algorithm delete_end
BEGIN
if(head==NULL)
printf("\nThe list is empty");
else if(head==tail)
BEGIN
free(head);
head=tail=NULL;
END
else
BEGIN
struct node*delnode;
delnode=tail;
tail=tail->prev;
tail->next=NULL;
delnode->prev=NULL;
free(delnode); END
END
Algorithm delete_pos
BEGIN
struct node*delnode,*temp;
temp=head;
printf("Enter the position:");
scanf("%d",&position);
for(i=1;i<position-1;i++)
temp=temp->next;
delnode=temp->next;
temp->next=delnode->next;
delnode->next->prev=temp;
delnode->prev=NULL;
delnode->next=NULL;
free(delnode);
END
Algorithm display
BEGIN
struct node *temp;
if(temp==NULL)
printf("There is no element in the list");
else
BEGIN
printf("The elements are:");
23 717823L121
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d->",temp->data);
printf("NULL\n");
END
END
SOURCE CODE:
#include<stdlib.h>
#include<stdio.h>
void insert_begin(int element);
void insert_end(int element);
void insert_pos(int element);
void delete_begin();
void delete_end();
void delete_pos();
void display();
struct node
{
int data;
struct node *next;
struct node *prev;
}*head=NULL,*tail=NULL;
main()
{
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ; int
choice,con,element,pos;
printf("1.Insertion at front\n2.insertion at end\n3.insertion at position\n4.Deletion at
begin\n5.Deletion at
end\n6.Deletion at position\n7.Display");
while(1)
{
printf("Enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element:");
scanf("%d",&element);
insert_begin(element);
break;
case 2:
printf("Enter the element:");
scanf("%d",&element);
insert_end(element);
break;
case 3:
printf("Enter the element:");
scanf("%d",&element);
insert_pos(element);
24 717823L121
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: delete_pos();
break;
case 7: display();
break;
default: printf("EXITING!!!\n");
exit(1);
}
}
}
void insert_begin(int element)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=element;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=tail=newnode;
else
{
newnode->next=head;
head->prev=newnode;
head=newnode;
}
}
void insert_end(int element)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=element;
newnode->next=NULL;
if(head==NULL)
head=tail=newnode;
else
{
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
}
}
void insert_pos(int element)
{
int position,i;
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
25 717823L121
newnode->data=element;
newnode->next=NULL;
if(head==NULL)
head=tail=newnode;
else
{
temp=head;
printf("Enter the position:");
scanf("%d",&position);
for(i=1;i<position-1;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
newnode->next->prev=newnode;
}
}
void delete_begin()
{
if(head==NULL)
printf("The list is empty");
else
{
struct node*delnode;
delnode=head;
head=head->next;
head->prev=NULL;
delnode->next=NULL;
free(delnode);
}
}
void delete_end()
{
if(head==NULL)
printf("The list is empty");
else
{
struct node*delnode;
delnode=tail;
tail=tail->prev;
tail->next=NULL;
delnode->prev=NULL;
free(delnode);
}
}
void delete_pos()
{
if(head==NULL)
26 717823L121
printf("The list is empty");
else
{
int i,position;
struct node*delnode,*temp;
temp=head;
printf("Enter the position:");
scanf("%d",&position);
for(i=1;i<position-1;i++)
{
temp=temp->next;
}
delnode=temp->next;
temp->next=delnode->next;
delnode->next->prev=temp;
delnode->prev=NULL;
delnode->next=NULL;
free(delnode);
}
}
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
printf("There is no element in the list");
else
{
printf("The elements are:");
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
}
27 717823L121
OUTPUT:
RESULT:
Thus the Program for implementation of doubly linked list was successfully
executed and output was verified.
28 717823L121
Ex no: 2.3
Date: IMPLEMENTATION OF CIRCULAR SINGLY LINKED LIST
AIM:
PSEUDOCODE:
BEGIN
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL) then
BEGIN
head=newnode;
tail=newnode;
END
else
BEGIN
newnode->next=head;
head=newnode;
END
tail->next=head;
END
Algorithm insert_end
BEGIN
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
BEGIN
head=newnode;
tail=newnode;
29 717823L121
END
else
BEGIN
tail->next=newnode;
tail=newnode;
END
tail->next=head;
END
Algorithm insert_pos
BEGIN
printf("Enter the position:");
scanf("%d",&position);
if(position==1)
insert_begin();
else if(position==count+1)
insert_end();
else if(position>count+1)
print("Not Possible");
else
BEGIN
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
BEGIN
head=newnode;
tail=newnode;
END
else
BEGIN
temp=head;
for(i=1;i<position-1;i++)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
END
END
END
Algorithm delete_begin
BEGIN
if(head==NULL)
30 717823L121
printf("\nThe list is empty");
else if(head==tail)
BEGIN
free(head);
head=tail=NULL;
END
else
BEGIN
struct node*delnode;
delnode=head;
head=head->next;
delnode->next=NULL;
free(delnode);
END
tail->next=head;
END
Algorithm delete_end
BEGIN
if(head==NULL)
printf("\nThe list is empty");
else if(head==tail)
BEGIN
free(head);
head=tail=NULL;
END
else
BEGIN
struct node *delnode,*temp;
temp=head;
delnode=tail;
while(temp->next!=tail)
temp=temp->next;
temp->next=NULL;
tail=temp;
free(delnode);
END
tail->next=head;
END
Algorithm delete_pos
BEGIN
int position,i;
printf("Enter the position:");
scanf("%d",&position);
31 717823L121
if(head==NULL)
printf("List empty. Cannot Delete\n");
else if(head==tail)
BEGIN
free(head);
head=tail=NULL;
END
else if(position==1)
delete_begin();
else if(position==count)
delete_end();
else if (position<=0||position>count)
printf("Not Possible\n");
else
BEGIN
struct node*delnode,*temp;
temp=head;
for(i=1;i<position-1;i++)
temp=temp->next;
delnode=temp->next;
temp->next=delnode->next;
free(delnode);
END
END
Algorithm display
BEGIN
struct node *temp;
if(temp==NULL)
printf("There is no element in the list");
else
BEGIN
printf("The elements are:");
for(temp=head;temp!=tail;temp=temp->next)
printf("%d->",temp->data);
printf("%d->",temp->data);
printf("NULL\n");
END
END
32 717823L121
SOURCE CODE:
#include<stdlib.h>
#include<stdio.h>
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
void display();
struct node {
int data;
struct node *next;
}*head=NULL,*tail=NULL;
int count=0;
void insert_begin()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL) {
head=newnode;
tail=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
tail->next=head;
count++;
display();
}
void insert_end()
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
33 717823L121
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
tail=newnode;
}
else
{
tail->next=newnode;
tail=newnode;
}
tail->next=head;
count++;
display();
}
void insert_pos()
{
int position,i;
printf("Enter the position:");
scanf("%d",&position);
if(position==1)
insert_begin();
else if(position==count+1)
insert_end();
else if(position>count+1)
printf("Not Possible\n");
else
{
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the element:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
tail=newnode;
count++;
}
else
{
temp=head;
717823L121
34
for(i=1;i<position-1;i++)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
count++;
}
}
display();
}
void delete_begin()
{
if(head==NULL)
printf("\nThe list is empty");
else if(head==tail)
{
free(head);
head=tail=NULL;
}
else
{
struct node*delnode;
delnode=head;
head=head->next;
delnode->next=NULL;
free(delnode);
}
tail->next=head;
count--;
display();
}
void delete_end()
{
if(head==NULL)
printf("\nThe list is empty");
else if(head==tail)
{
free(head);
head=tail=NULL;
}
else
{
struct node *delnode,*temp;
temp=head;
717823L121
35
delnode=tail;
while(temp->next!=tail)
temp=temp->next;
temp->next=NULL;
tail=temp;
free(delnode);
}
tail->next=head;
count--;
display();
}
void delete_pos()
{
int position,i;
printf("Enter the position:");
scanf("%d",&position);
if(head==NULL)
printf("List empty. Cannot Delete\n");
else if(head==tail)
{
free(head);
head=tail=NULL;
count--;
}
else if(position==1)
delete_begin();
else if(position==count)
delete_end();
else if (position<=0||position>count)
printf("Not Possible\n");
else{
struct node*delnode,*temp;
temp=head;
for(i=1;i<position-1;i++)
temp=temp->next;
delnode=temp->next;
temp->next=delnode->next;
free(delnode);
count--;
}
display();
}
void display()
36 717823L121
{
struct node *temp;
if(temp==NULL)
printf("There is no element in the list");
else
{
printf("The elements are:");
for(temp=head;temp!=tail;temp=temp->next)
printf("%d->",temp->data);
printf("%d->",temp->data);
printf("NULL\n");
}
}
int main()
{
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ; int
choice,con,element,pos;
printf("1.Insertion at Begin\n2.Insertion at End\n3.Insertion at
Position\n4.Deletion at Begin\n5.Deletion at End\n6.Deletion at
Position\n7.Display");
while(1)
{
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_begin();
break;
case 02:
insert_end();
break;
case 3:
insert_pos();
break;
case 4:
delete_begin();
break;
case 5:
delete_end();
break;
case 6:
delete_pos(); break;
37 717823L121
case 7:
display();
break;
default:
printf("Exiting Program!!!"); break;
}
}
}
OUTPUT:
RESULT:
Thus the Program for implementation of Doubly Linked list was
successfully executed and the output was verified.
38 717823L121
Ex no: 3.1
IMPLEMENTATION OF STACK USING LINKED LIST
Date:
AIM :
PSUEDOCODE :
BEGIN
DECLARE top AS NODE POINTER
FUNCTION createNode(data)
DECLARE newNode AS NODE POINTER
ALLOCATE newNode
newNode->data = data
newNode->next = NULL
RETURN newNode
END FUNCTION
FUNCTION isEmpty(topPtr)
RETURN topPtr == NULL
END FUNCTION
newNode = createNode(data)
newNode->next = topPtr
topPtr = newNode
PRINT " " data " pushed to the stack."
END PROCEDURE
FUNCTION pop(topPtr)
DECLARE temp AS NODE POINTER
IF isEmpty(topPtr) THEN
PRINT "Stack underflow!"
RETURN INT_MIN
ELSE
temp = topPtr
popped = temp->data
39 717823L121
topPtr = topPtr->next
FREE temp
RETURN popped
END IF
END FUNCTION
PROCEDURE displayStack(topPtr)
IF isEmpty(topPtr) THEN
PRINT "Stack is empty!"
ELSE
DECLARE current AS NODE POINTER
current = topPtr
PRINT "Stack: "
PRINT "\n"
END IF
END PROCEDURE
top = NULL
push(top, 10)
push(top, 20)
push(top, 30)
displayStack(top)
PRINT " " pop(top) " popped from the stack."
displayStack(top)
END
SOURCE CODE:
#include<stdio.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(size of(struct Node));
newNode->data = data;
40 717823L121
newNode->next = NULL;
return newNode;
}
int isEmpty(struct Node* top) {
return top == NULL;
}
void push(struct Node** top, int data) {
struct Node* newNode = createNode(data);
newNode->next = *top;
*top = newNode;
printf("%d pushed to the stack.\n", data);
}
int pop(struct Node** top) {
if (isEmpty(*top)) {
printf("Stack underflow!\n");
return INT_MIN;
}
struct Node* temp = *top;
int popped = temp->data;
*top = (*top)->next;
free(temp);
return popped;
}
void displayStack(struct Node* top) {
if (isEmpty(top)) {
printf("Stack is empty!\n");
return;
}
struct Node* current = top;
printf("Stack: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ; struct
Node* top = NULL;
push(&top, 121);
push(&top, 156);
push(&top, 157);
41 717823L121
displayStack(top);
printf("%d popped from the stack.\n", pop(&top));
displayStack(top);
return 0;
}
OUTPUT:
RESULT:
Thus the Program for implementation of Stack using linked list was
successfully executed and the output was verified.
42 717823L121
Ex no: 3.2
IMPLEMENTATION OF LINKED LIST USING LINEAR QUEUE
Date:
AIM :
PSEUDOCODE:
BEGIN
DECLARE MAXSIZE AS INTEGER (e.g., 10)
DECLARE Q[MAXSIZE] AS INTEGER ARRAY
DECLARE front, rear AS INTEGER
INITIALIZE front TO -1, rear TO -1
FUNCTION qinsert(x)
IF rear == MAXSIZE - 1 THEN
PRINT "\n Queue is Full."
ELSE IF front == -1 THEN
front = 0
rear = 0
Q[front] = x
ELSE
rear = rear + 1
Q[rear] = x
END IF
END FUNCTION
FUNCTION qdelete
IF front == -1 THEN
PRINT "\n Queue is Empty."
ELSE IF front == rear THEN
PRINT "\n " Q[front] " is removed from Queue."
front = -1
rear = -1
ELSE
PRINT "\n " Q[front] " is deleted from Queue."
front = front + 1
END IF
END FUNCTION
PROCEDURE display
43 717823L121
DECLARE i AS INTEGER
PRINT "\n The Queue elements are...\n"
IF front == -1 THEN
PRINT "\n No elements in Queue."
ELSE
FOR i = front TO rear
PRINT " " Q[i]
END FOR
END IF
END PROCEDURE
DECLARE choice, x AS INTEGER
WHILE TRUE
PRINT "\n 1. Data insert\n 2. Data Delete\n 3. Data Display\n 4. Exit"
PRINT "\n Please, Enter your choice: "
READ choice
CASE choice
WHEN 1:
PRINT "\n Please, Enter the element: "
READ x
qinsert(x)
WHEN 2:
qdelete()
WHEN 3:
display()
WHEN 4:
EXIT
ELSE:
PRINT "\n Wrong Choice."
END CASE
END WHILE
END
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
int Q[MAXSIZE],front=-1,rear=-1;
void qinsert(int x)
{
if(rear==MAXSIZE-1)
44 717823L121
printf("\n Queue is Full.");
else if(front==-1)
{
front=0;
rear=0;
Q[front]=x;
}
else
{
rear++;
Q[rear]=x;
}
}
void qdelete()
{
if(front==-1)
printf("\n Queue is Empty.");
else if(front==rear)
{
printf("\n %d is removed from Queue.",Q[front]);
front=-1;
rear=-1;
}
else
{
printf("\n %d is deleted from Queue.",Q[front]);
front++;
}
}
void display()
{
int i;
printf("\n The Queue elements are...\n");
if(front==-1)
printf("\n No elements in Queue.");
else
{
for(i=front;i<=rear;i++)
printf(" %d ",Q[i]);
}
}
int main()
{
45 717823L121
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ; int
choice,x;
while(1)
{
printf("\n 1.Data insert\n 2.Data Delete\n 3.Data Display\n 4.Exit");
printf("\n Please, Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Please, Enter the element : ");
scanf("%d",&x);
qinsert(x);
break;
case 2: qdelete();
break;
case 3: display();
break;
case 4: exit(0);
default : printf("\n Invalid Choice.");
}
}
}
46 717823L121
OUTPUT:
RESULT:
Thus the Program to implementation of linked list using linear queue was
successfully executed and the output was verified.
47 717823L121
[Ex no: 4.1
IMPLEMENTATION OF EVALUTION OF POSTFIX EXPRESSION
Date:
AIM :
PSEUDOCODE:
BEGIN
DECLARE stack[20] AS INTEGER ARRAY
DECLARE top AS INTEGER
INITIALIZE top TO -1
FUNCTION push(x)
top = top + 1
stack[top] = x
END FUNCTION
FUNCTION pop
RETURN stack[top--]
END FUNCTION
DECLARE exp[20] AS CHARACTER ARRAY
DECLARE e AS CHARACTER POINTER
DECLARE n1, n2, n3, num AS INTEGER
48 717823L121
WHEN '/':
n3 = n2 / n1
END CASE
push(n3)
END IF
e=e+1
END WHILE
PRINT "\nThe result of expression " exp " = " pop() "\n\n"
END
SOURCE CODE:
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ; char
exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
49 717823L121
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
50 717823L121
OUTPUT:
RESULT:
51 717823L121
Ex no: 4.2
CONVERSION OF INFIX TO POSTFIX EXPRESSION
Date:
AIM :
PSEUDOCODE:
BEGIN
DECLARE stack[100] AS CHARACTER ARRAY
DECLARE top AS INTEGER
INITIALIZE top TO -1
FUNCTION push(x)
top = top + 1
stack[top] = x
END FUNCTION
FUNCTION pop
IF top == -1 THEN
RETURN -1
ELSE
RETURN stack[top--]
END IF
END FUNCTION
FUNCTION priority(x)
CASE x
WHEN '(':
RETURN 0
WHEN '+' OR '-':
RETURN 1
WHEN '*' OR '/':
RETURN 2
ELSE:
RETURN 0
END CASE
END FUNCTION
DECLARE exp[100] AS CHARACTER ARRAY
DECLARE e AS CHARACTER POINTER
DECLARE x AS CHARACTER
PRINT "Enter the expression: "
READ exp
52 717823L121
PRINT "\n"
e = exp
WHILE *e != '\0'
IF isalnum(*e) THEN
PRINT *e " "
ELSE IF *e == '(' THEN
push(*e)
ELSE IF *e == ')' THEN
WHILE (x = pop()) != '('
PRINT x " "
END WHILE
ELSE
WHILE priority(stack[top]) >= priority(*e)
PRINT pop() " "
END WHILE
push(*e)
END IF
e=e+1
END WHILE
WHILE top != -1
PRINT pop() " "
END WHILE
END
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
53 717823L121
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
printf(" Name : R.HARISH\nRoll number : 717823L121\n") ; char
exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
54 717823L121
return 0;
}
OUTPUT:
RESULT:
55 717823L121