21BCE3118 Aryan Chugh
21BCE3118 Aryan Chugh
ARYAN CHUGH
CODE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 10
int stack[MAX];
int top = -1;
void push(int stack[], int val);
int peek(int stack[]);
void display(int stack[]);
int main()
{
int num, rem;
printf("Enter a decimal number: ");
scanf("%d", &num);
while (num != 0)
{
rem = num % 2;
push(stack, rem);
num = num / 2;
}
printf("Binary number is: ");
display(stack);
return 0;
}
void push(int stack[], int val)
{
if (top == MAX - 1)
printf("Stack Overflow");
else
{
top++;
stack[top] = val;
}
}
int peek(int stack[])
{
if (top == -1)
{
printf("Stack Underflow");
return -1;
}
else
return stack[top];
}
void display(int stack[])
{
int i;
if (top - 1)
printf("Stack is empty");
else
{
for (i = top; i >= 0; i--)
{
printf("%d", stack[i]);
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
void push(int val);
void pop();
void display();
int main()
{
int num, rem;
printf("Enter a decimal number: ");
scanf("%d", &num);
while (num != 0)
{
rem = num % 2;
push(rem);
num = num / 2;
}
printf("Binary number is: ");
display();
return 0;
}
void push(int val)
{
struct node *newNode;
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = val;
if (top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
}
void peak()
{
if (top == NULL)
printf("Stack is empty");
else
printf("Top element is %d", top->data);
}
void display()
{
struct node *temp;
temp = top;
if (top == NULL)
printf("Stack is empty");
else
{
while (temp != NULL)
{
printf("%d", temp->data);
temp = temp->next;
}
}
}
Q2The above code will reverse a given singly linked list. But the code is incomplete. To complete the
code you need to write a single statement by replacing “/*ADD A STATEMENT HERE*/” (given in
red). Write the required statement to complete the program.
*head_ref = prev;
At the end of while loop, the prev pointer points to the last node of original linked list. We need to
change *head_ref so that the head pointer now starts pointing to the last node
Q3Write a C program to count the total number of elements in a circular linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
void createList(int n);
void countNodes();
void displayList();
// Write a C program to count the total number of elements in a circular
linked list.
void createList(int n)
{
int i, data;
struct node *newNode, *temp;
if (n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));
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));
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
temp->next = head;
}
}
void countNodes()
{
int ctr = 0;
struct node *current;
if (head == NULL)
{
printf("List is empty.");
}
else
{
current = head;
do
{
ctr++;
current = current->next;
} while (current != head);
printf("Total number of nodes = %d", ctr);
}
}
void displayList()
{
struct node *temp;
int n = 1;
if (head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
do
{
printf("Data of %d node = %d \n", n, temp->data);
n++;
temp = temp->next;
} while (temp != head);
}
}
int main()
{
int n, data;
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
printf("Data in the list are \n");
displayList();
countNodes();
return 0;
}
4. Which function grows faster in each of the following cases? (a) nlog n; (log n)n (b) log nk ; (log n)k
(c) nlog log log n; (log n)! (d) nn ; n!.
A (log n)n
B (log n)k
C (log n)!
D = n^n
b) n2 – 20n – 20 is O(n3 )
A .TRUE
B.FALSE
C. FALSE
D. TRUE
E.FALSE