You are on page 1of 10

Q 5 (a) insert at front

(b) insert at end

(c) insert at any position


(d)delete 1st node
(e)delete at end

(e) delete at position

(f) display all elements of LL


void display() {

//Node current will point to head

struct node *current = head;

if(head == NULL) {

printf("List is empty\n");

return;

printf("Nodes of singly linked list: \n");

while(current != NULL) {

//Prints each node by incrementing pointer

printf("%d ", current->data);

current = current->next;

printf("\n");

}
Q6

(A) Push Oeration

void push(int element)


{
if (size - top > 1)
{
top++;
arr[top] = element;
}
else
{
cout << "Stack overflow" << endl;
}
}

(B)pop operation

void pop()
{
if (top >= 0)
{
top--;
}
else
{
cout << "Stack Underflow " << endl;
}
}

(C)peep operation

int peak()
{
if (top >= 0)
{
return arr[top];
}
else
{
cout << "Stack is empty" << endl;
return -1;
}
}

(d) display operation

void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
Q2(a) insertion
void insert(struct node * ptr, int item) {

ptr = (struct node * ) malloc(sizeof(struct node));


if (ptr == NULL) {
printf("\nOVERFLOW\n");
return;
} else {
ptr - > data = item;
if (front == NULL) {
front = ptr;
rear = ptr;
front - > next = NULL;
rear - > next = NULL;
} else {
rear - > next = ptr;
rear = ptr;
rear - > next = NULL;
}
}
}

(b)deletion
void deleteNode(struct node * ptr) {
if (front == NULL) {
printf("Underflow");
return;
} else {
ptr = front;
front = front - > next;
free(ptr);
}
}

(c) display

void display()

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;

Q1

(a)circular queue insertion

bool enqueue(int value)


{

if ((front == 0 && rear == size - 1) || rear == (front - 1) % (size -


1))
{
cout << "Queue is full " << endl;
return false;
}

else if (front == -1)


{
front = rear = 0;
}
else if (rear == size - 1)
{
rear = 0;
}
else
{
rear++;
}

arr[rear] = value;
return true;
}

(b) deletion

int dequeue()
{
if (front == -1)
{
cout << "Queue is empty " << endl;
return -1;
}

int ans = arr[front];


arr[front] = -1;
if (front == rear)
{
front = rear = -1;
}
else if (front == size - 1)
{
front = 0;
}

else
{
front++;
}

return ans;
}

(C) display

void display()

int i=front;

if(front==-1 && rear==-1)

printf("\n Queue is empty..");

else

printf("\nElements in a Queue are :");

while(i<=rear)

printf("%d,", queue[i]);

i=(i+1)%max;

You might also like