Professional Documents
Culture Documents
int main(){
scanf("%d", &a[i]);
scanf("%d", &item);
if (item == a[i])
{ flag =1;
break;
if (flag == 0)
else
return 0;
int main()
int flag=0;
scanf("%d", &n);
scanf("%d", &arr[i]);
first=0;
last=n;
scanf("%d", &key);
while(first<=last)
mid=(first + last) / 2;
if(arr[mid] == key)
{ flag=1;
break;
}
else if(key>arr[mid])
first=mid+1;
else
last=mid-1;
if(flag==0)
break;
default:
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\t STACK is over flow"); }
else
{
printf(" Enter a value to be pushed:");
scanf("%d", &x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d", stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
printf("\n The STACK is empty");
}
Exp 10 : Queue Operations
#include<stdio.h>
#define n 5
int main()
{
int queue[n], ch=1, front=0, rear=0,i, j=1, x=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++]);
}
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: printf("\n\t EXIT POINT ");
break;
default: printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
Experiment -11
Creation of a Linked List:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node* head = NULL,*ptr=NULL;
void addNode(int data)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->info = data;
newNode->next = NULL;
if(head==NULL)
{
head = newNode;
ptr = newNode;
}
else
{
ptr->next= newNode;
ptr = newNode;
}
}
void display()
{
struct node* current=head;
if (head==NULL)
{
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list:\n");
printf("\n[head] =>");
while (current!= NULL)
{
printf("%d ->", current->info );
current = current->next;
}
printf(" [null]\n");
}
int main()
{
addNode(11);
addNode(27);
addNode(36);
addNode(48);
display();
return 0;
}
Experiment -12
Traversing a Linked List:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node* head = NULL,*ptr=NULL;
void addNode(int data)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->info = data;
newNode->next = NULL;
if(head==NULL)
{
head = newNode;
ptr = newNode;
}
else
{
ptr->next= newNode;
ptr = newNode;
}
}
void display()
{
struct node* current=head;
if (head==NULL)
{
printf("List is empty\n");
return;
}
printf("\n Nodes of singly linked list:\n");
printf("\n Traversal in linked list.");
printf("\n[head] =>");
while (current!= NULL)
{
printf("%d ->", current->info );
current = current->next;
}
printf(" [null]\n");
}
int main()
{
addNode(11);
addNode(27);
addNode(36);
addNode(48);
display();
return 0;
}
Experiment -13
Searching an element in linked list:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
} * head;
int createList(int n){
struct node *newNode, *temp;
int data, i;
head = (struct node* )malloc(sizeof(struct node));
if (head == NULL){
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}
printf("Enter data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for (i = 2; i <= n; i++)
{
newNode = (struct node*)malloc(sizeof(struct node *));
if (newNode == NULL)
{
printf("Unable to allocate memory. Exiting from app.");
exit(0);
}
printf("Enter data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
return 0;
}
void displayList()
{
struct node *temp;
if (head == NULL)
{
printf("List is empty.\n");
return;
}
temp = head;
while (temp != NULL)
{
printf("%d, ", temp->data);
temp = temp->next;
}
printf("\n");
}
int search(int key)
{
int index;
struct node *curNode;
index = 0;
curNode = head;
while (curNode != NULL && curNode->data != key)
{
index++;
curNode = curNode->next;
}
return (curNode != NULL) ? index : -1;
}
int main()
{
int n, keyToSearch, index;
printf("Enter number of node to create: ");
scanf("%d", &n);
createList(n);
printf("\nData in list: \n");
displayList();
printf("\nEnter element to search: ");
scanf("%d", &keyToSearch);
index = search(keyToSearch);
if (index >= 0)
printf("%d found in the list at position %d\n", keyToSearch, index + 1);
else
printf("%d not found in the list.\n", keyToSearch);
return 0;
}
Experiment -14
Add a node at beginning of linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtBeginning(int data)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = head;
head = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
int main()
{
int n, data;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter data to insert at beginning of the list: ");
scanf("%d", &data);
insertNodeAtBeginning(data);
printf("\nData in the list \n");
displayList();
return 0;
}
Experiment -15
Add a node after a given node of linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head;
void initialize()
{
head = NULL;
}
void insert(int num)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = head;
head = newNode;
printf("Inserted Element : %d\n", num);
}
void insertAfter(struct node* prevNode, int num)
{
if (prevNode == NULL) {
printf("Error : Invalid node pointer !!!\n");
return;
}
struct node* newNode =(struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
void printLinkedList(struct node *nodePtr)
{
printf("\nLinked List\n");
while (nodePtr != NULL) {
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
initialize();
insert(2);
insert(4);
insert(5);
insert(9);
printLinkedList(head);
insertAfter(head->next->next, 8);
printf("\n\nAfter Insertion\n");
printLinkedList(head);
return 0;
}
Experiment -16
Add a node at end of linked list:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void addLast(struct node **head, int val)
{
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
if(*head == NULL)
*head = newNode;
else
{
struct node *lastNode = *head;
while(lastNode->next != NULL)
{
lastNode = lastNode->next;
}
lastNode->next = newNode;
}
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
struct node *head = NULL;
addLast(&head,10);
addLast(&head,20);
addLast(&head,30);
printList(head);
addLast(&head,456);
printf("\nAfter adding 456 in last node\n");
printList(head);
return 0;
}
Experiment -17
Delete a node from beginning of linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Experiment -18
Delete a node after a given node of Linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head;
void createList(int n);
void deleteMiddleNode(int position);
void displayList();
int main()
{
int n, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter the node position you want to delete: ");
scanf("%d", &position);
deleteMiddleNode(position);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED
SUCCESSFULLY\n");
}
}
void deleteMiddleNode(int position)
{
int i;
struct node *toDelete, *prevNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
prevNode = head;
for(i=2; i<=position; i++)
{
prevNode = toDelete;
toDelete = toDelete->next;
if(toDelete == NULL)
break;
}
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
free(toDelete);
printf("SUCCESSFULLY DELETED NODE FROM MIDDLE OF LIST\n");
}
else
{
printf("Invalid position unable to delete.");
}
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Experiment -19
Delete a node from end of Linked list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void createList(int n);
void deleteLastNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete last node: ");
scanf("%d", &choice);
if(choice == 1)
deleteLastNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED
SUCCESSFULLY\n");
}
}
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
if(toDelete == head)
{
head = NULL;
}
else
{
secondLastNode->next = NULL;
}
free(toDelete);
printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
Experiment -20
Linked stack:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
int main ()
{
int choice=0;
while(choice != 4)
{
printf("\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct
node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Experiment -21
Linked Queue:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
int main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}