Assignment 2
Assignment 2
Assignment-2:
1)Code to insert a node at the beginning of list using C language:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
int main()
{
int n,num;
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
2)Code to delete occurrences in a singly linked list using C language:
#include <stdio.h>
#include <stdlib.h>
int main()
{
push(&head, 1);
push(&head, 5);
push(&head, 3);
push(&head, 5);
push(&head, 8);
push(&head, 1);
push(&head, 5);
push(&head, 5);
int key = 5;
// Function call
head = deleteKey(head, key);
if (!head)
printf("\nNo element present in the Linked list\n");
else {
printf("\nLinked List after Deletion is:\n");
printList(head);
}
return 0;
}
3)Code to delete a value from a doubly linked list in C language:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
if (*head_ref == del)
*head_ref = del->next;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free(del);
return;
}
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int main()
{
push(&head, 2);
push(&head, 4);
push(&head, 8);
push(&head, 10);
printf(
"Original Linked list\n ");
printList(head);
deleteNode(&head, head);
deleteNode(&head, head->next);
deleteNode(&head, head->next);
struct node {
int data;
struct node * next;
}*head;
int main()
{
int n, data, choice=1;
head = NULL;
while(choice != 0)
{
printf("1. Create List\n");
printf("2. Display list\n");
printf("3. Insert at beginning\n");
printf("4. Insert at any position\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
displayList();
break;
case 3:
printf("Enter data to be inserted at beginning: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 4:
printf("Enter node position: ");
scanf("%d", &n);
printf("Enter data you want to insert at %d position: ", n);
scanf("%d", &data);
insertAtN(data, n);
break;
case 0:
break;
default:
printf("Error! Invalid choice. Please choose between 0-4");
}
printf("\n\n\n\n\n");
}
return 0;
}
void createList(int n)
{
int i, data;
struct node *prevNode, *newNode;
if(n >= 1)
{
prevNode = head;
newNode->data = data;
newNode->next = NULL;
prevNode->next = newNode;
prevNode = newNode;
}
prevNode->next = head;
void displayList()
{
struct node *current;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
current = head;
printf("DATA IN THE LIST:\n");
do {
printf("Data %d = %d\n", n, current->data);
current = current->next;
n++;
}while(current != head);
}
}
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
current = head;
while(current->next != head)
{
current = current->next;
}
current->next = newNode;
head = newNode;
if(head == NULL)
{
printf("List is empty.\n");
}
else if(position == 1)
{
insertAtBeginning(data);
}
else
{
current = head;
for(i=2; i<=position-1; i++)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
if(*head == NULL){
printf("Linked List Empty, nothing to delete");
return;
}
if(tempNode->next == *head){
*head = NULL;
return;
}
while(curr->next != *head)
curr = curr->next;
curr->next = (*head)->next;
*head = (*head)->next;
free(tempNode);
}
if(*head == NULL){
printf("Linked List Empty, nothing to delete");
return;
}
if(tempNode->next == *head){
*head = NULL;
return;
}
else if(n == 1)
deleteFront(head);
else {
struct Node* tempNode = *head;
struct Node* previous;
while (--n) {
previous = tempNode;
tempNode = tempNode->next;
}
previous->next = tempNode->next;
free(tempNode);
}
}
if(*head == NULL){
*head = newNode;
(*head)->next = *head;
return;
}
while(curr->next != *head){
curr = curr->next;
}
curr->next = newNode;
newNode->next = *head;
*head = newNode;
}
if(temp == NULL)
return size;
do{
size++;
temp = temp->next;
}while(temp!=head);
return size;
}
do{
printf("%d ", temp->data);
temp = temp->next;
}while(temp!=head);
printf("\n");
}
int main(){
insert(&head,10);
insert(&head,11);
insert(&head,12);
insert(&head,13);
insert(&head,14);
insert(&head,15);
insert(&head,16);
display(head);
deleteFront(&head);
display(head);
deleteEnd(&head);
display(head);
deletePos(&head, 3);
display(head);
return 0;
}
6) Code to reverse a linked list using C language:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 26);
push(&head, 27);
push(&head, 28);
push(&head, 29);
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK 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");
}
}
8) Code to implement stacks in linked list in C language:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
void destroy()
{
top1 = top;
if(checkInfix(a)):
print("The given expression is valid infix expression")
else:
print("The given expression is not a valid infix expression")
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
rear = temp;
}
count++;
}
void display()
{
front1 = front;
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
int stack_pop(queue_t * q) {
int i, n = queue_count(q);
int removed_element;
}
removed_element = queue_remove(q);
return removed_element;
}
int stack_is_empty(queue_t * q) {
return queue_is_empty(q);
}
int stack_count(queue_t * q) {
return queue_count(q);
}
int queue_count(queue_t * q) {
return q->count;
}
queue_t *
queue_allocate(int n) {
queue_t *queue;
queue = malloc(sizeof(queue_t));
if (queue == NULL)
return NULL;
queue->max = n;
return queue;
}
int queue_remove(queue_t * q) {
int retval;
if (q->count == 0)
return QUEUE_EMPTY_MAGIC;
return retval;
}
int queue_is_empty(queue_t * q) {
return (q->count == 0);
}
void queue_display(queue_t * q) {
int i = (q->front + 1) % q->max, elements = queue_count(q);
while (elements--) {
printf("[%d], ", q->arr[i]);
i = (i >= q->max) ? 0 : (i + 1);
}
}
do {
printf("\n[1] Push\n[2] Pop\n[0] Exit");
printf("\nChoice: ");
scanf(" %d", &select);
switch (select) {
case 1:
printf("\nEnter value to Push:");
scanf(" %d", &x);
/* Pushing */
stack_push(q, x);
printf("\n\n__________________________\nCurrent Queue:\n");
queue_display(q);
printf("\n\nPushed Value: %d", x);
printf("\n__________________________\n");
break;
case 2:
/* Popping */
x = stack_pop(q);
printf("\n\n\n\n__________________________\nCurrent Queue:\n");
queue_display(q);
if (x == QUEUE_EMPTY_MAGIC)
printf("\n\nNo values removed");
else
printf("\n\nPopped Value: %d", x);
printf("\n__________________________\n");
break;
case 0:
printf("\nQutting.\n");
return 0;
default:
printf("\nQutting.\n");
return 0;
}
} while (1);
return 0;
}
14) Code to implement a queue using two stacks in C language:
#include <stdio.h>
#include<stdlib.h>
#define N 100
int count = 0;
else{
top_stack1++;
stack1[top_stack1] = data;
}
return;
}
else
{
top_stack2++;
stack2[top_stack2] = data;
}
return;
int pop_stack1 ()
{
if (top_stack1 == -1){
printf ("Stack1 is underflow\n");
return -1;
}
return stack1[top_stack1--];
}
int pop_stack2 ()
{
if (top_stack2 == -1)
{
printf ("Stack2 is underflow\n");
return -1;
}
return stack2[top_stack2--];
void dequeue ()
{
if (top_stack1 == -1 && top_stack2 == -1)
printf ("Queue is empty\n");
else {
for (int i = 0; i < count; i++){
}
}
}
void display ()
{
if (top_stack1 == -1)
{
printf ("Queue is empty \n");
return;
}
printf ("\n");
void top ()
{
printf ("Top element of queue is %d ", stack1[0]);
}
int main ()
{
enqueue (3);
enqueue (45);
enqueue (-1);
display();
dequeue ();
enqueue (-1);
display ();
return 0;