You are on page 1of 16

Linked list

1. Write a C program for implementation of singly linked list


Ans:
#include <stdio.h>
#include <stdlib.h>
struct node{
int INFO;
struct node *NEXT;
};
struct node *FIRST = NULL;
struct node *LAST = NULL;
void insert(int);
int delete(int);
void print(void);
struct node *search(int);
int main()
{
int num1, num2, choice;
struct node *location;
while (1)
{
printf("Select an option\n");
printf("1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Search\n");
printf("4 - Print\n");
printf("5 - Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted into the linked list: ");
scanf("%d", &num1);
insert(num1);
printf("\n%d successfully inserted into the linked list\n", num1);
break;

case 2:
printf("\nEnter the element to be deleted from the linked list: ");
scanf("%d", &num1);
num2 = delete(num1);
if (num2 == -9999)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d successfully deleted from the linked list\n", num2);
break;

case 3:
printf("\nEnter the element to be searched: ");
scanf("%d", &num1);
location = search(num1);
if (location == NULL)
printf("\n%d is not present in the linked list\n", num1);
else
{
if (location == LAST)
printf("\nElement %d is the last element in the list", num1);
else
printf("\nElement %d is present before element %d in the linked list\n", num1,
(location->NEXT)->INFO);
}
break;

case 4:
print();
break;

case 5:
exit(0);
break;

default:
printf("\nIncorrect Choice, please try again.\n");
break;
}
}
return 0;
}
void insert(int value)
{
struct node *PTR = (struct node *)malloc(sizeof(struct node));
if (PTR == NULL){
printf("\nMemory allocation failed. Exiting...\n");
exit(1);
}
PTR->INFO = value;
if (FIRST == NULL){
FIRST = LAST = PTR;
PTR->NEXT = NULL;
}
else{
LAST->NEXT = PTR;
PTR->NEXT = NULL;
LAST = PTR;
}
}
int delete(int value)
{
struct node *LOC, *TEMP;
int i;
i = value;
LOC = search(i);
if (LOC == NULL)
return -9999;
if (LOC == FIRST)
{
if (FIRST == LAST)
FIRST = LAST = NULL;
else
FIRST = FIRST->NEXT;
return value;
}
for (TEMP = FIRST; TEMP->NEXT != LOC; TEMP = TEMP->NEXT);
TEMP->NEXT = LOC->NEXT;
if (LOC == LAST)
LAST = TEMP;
return LOC->INFO;
}
struct node *search(int value){
struct node *PTR;
if (FIRST == NULL)
return NULL;
for (PTR = FIRST; PTR != NULL && PTR->INFO != value; PTR = PTR->NEXT);
return PTR;
}
void print(){
struct node *PTR;
if (FIRST == NULL){
printf("\nEmpty list!!\n");
return;
}
printf("\nLinked list elements:\n");
for (PTR = FIRST; PTR != NULL; PTR = PTR->NEXT)
printf("\t%d", PTR->INFO);
printf("\n");
}
2. Write a C program for implementation of doubly linked list
Ans:
#include <stdio.h>
#include <stdlib.h>
struct dl_node{
int INFO;
struct dl_node *NEXT;
struct dl_node *PREVIOUS;
};
struct dl_node *FIRST = NULL;
struct dl_node *LAST = NULL;
void insert(int);
int delete(int);
void print(void);
struct dl_node *search(int);
int main(){
int num1, num2, choice;
struct dl_node *location;
while (1)
{
printf("\n\nSelect an option\n");
printf("1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Search\n");
printf("4 - Print\n");
printf("5 - Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted into the linked list: ");
scanf("%d", &num1);
insert(num1);
printf("%d successfully inserted into the linked list!\n", num1);
break;
case 2:
printf("\nEnter the element to be deleted from the linked list: ");
scanf("%d", &num1);
num2 = delete(num1);
if (num2 == -9999)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d successfully deleted from the linked list\n", num2);
break;
case 3:
printf("\nEnter the element to be searched: ");
scanf("%d", &num1);
location = search(num1);
if (location == NULL)
printf("\n%d is not present in the linked list\n", num1);
else
{
if (location == LAST)
printf("\nElement %d is the last element in the list\n", num1);
else
printf("\nElement %d is present before element %d in the linked list\n", num1,
(location->NEXT)->INFO);
}
break;
case 4:
print();
break;
case 5:
exit(0);
default:
printf("\nIncorrect choice. Please try again.\n");
break;
}
}
return 0;
}
void insert(int value){
struct dl_node *PTR = (struct dl_node *)malloc(sizeof(struct dl_node));
PTR->INFO = value;
if (FIRST == NULL){
FIRST = LAST = PTR;
PTR->NEXT = NULL;
PTR->PREVIOUS = NULL;
}
else{
LAST->NEXT = PTR;
PTR->NEXT = NULL;
PTR->PREVIOUS = LAST;
LAST = PTR;
}
}
int delete(int value){
struct dl_node *LOC, *TEMP;
int i;
i = value;
LOC = search(i);
if (LOC == NULL)
return -9999;
if (LOC == FIRST){
if (FIRST == LAST)
FIRST = LAST = NULL;
else{
FIRST->NEXT->PREVIOUS = NULL;
FIRST = FIRST->NEXT;
}
return value;
}
for (TEMP = FIRST; TEMP->NEXT != LOC; TEMP = TEMP->NEXT);
if (LOC == LAST){
LAST = TEMP;
TEMP->NEXT = NULL;
}
else{
TEMP->NEXT = LOC->NEXT;
LOC->NEXT->PREVIOUS = TEMP;
}
return LOC->INFO;
}
struct dl_node *search(int value){
struct dl_node *PTR;
if (FIRST == NULL)
return NULL;
if (FIRST == LAST && FIRST->INFO == value)
return FIRST;
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
if (PTR->INFO == value)
return PTR;
if (LAST->INFO == value)
return LAST;
else
return NULL;
}
void print(){
struct dl_node *PTR;
if (FIRST == NULL){
printf("\nEmpty List!!\n");
return;
}
printf("\nDoubly linked list elements:\n");
if (FIRST == LAST){
printf("%d\n", FIRST->INFO);
return;
}
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
printf("%d\t", PTR->INFO);
printf("%d\n", LAST->INFO);
}
3. Write a C program for implementation of circular linked list
Ans:
#include <stdio.h>
#include <stdlib.h>
struct cl_node{
int INFO;
struct cl_node *NEXT;
};
struct cl_node *FIRST = NULL;
struct cl_node *LAST = NULL;
void insert(int);
int delete(int);
void print(void);
struct cl_node *search(int);
int main(){
int num1, num2, choice;
struct cl_node *location;
while (1){
printf("\n\nSelect an option\n");
printf("1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Search\n");
printf("4 - Print\n");
printf("5 - Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted into the linked list: ");
scanf("%d", &num1);
insert(num1);
printf("\n%d successfully inserted into the linked list!\n", num1);
break;
case 2:
printf("\nEnter the element to be deleted from the linked list: ");
scanf("%d", &num1);
num2 = delete(num1);
if (num2 == -9999)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d successfully deleted from the linked list\n", num2);
break;
case 3:
printf("\nEnter the element to be searched: ");
scanf("%d", &num1);
location = search(num1);
if (location == NULL)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d is present before element %d in the circular linked list\n",
num1, (location->NEXT)->INFO);
break;
case 4:
print();
printf("Press Enter to continue...\n");
getchar();
break;
case 5:
exit(0);
default:
printf("\nIncorrect choice. Please try again.\n");
break;
}
}

return 0;
}
void insert(int value)
{
struct cl_node *PTR = (struct cl_node *)malloc(sizeof(struct cl_node));
PTR->INFO = value;

if (FIRST == NULL)
{
FIRST = LAST = PTR;
PTR->NEXT = FIRST;
}
else{
LAST->NEXT = PTR;
PTR->NEXT = FIRST;
LAST = PTR;
}
}
int delete(int value)
{
struct cl_node *LOC, *TEMP;
int i;
i = value;
LOC = search(i);
if (LOC == NULL)
return -9999;
if (LOC == FIRST){
if (FIRST == LAST){
FIRST = LAST = NULL;
}
else{
FIRST = FIRST->NEXT;
LAST->NEXT = FIRST;
}
return value;
}
for (TEMP = FIRST; TEMP->NEXT != LOC; TEMP = TEMP->NEXT);
if (LOC == LAST){
LAST = TEMP;
TEMP->NEXT = FIRST;
}
else{
TEMP->NEXT = LOC->NEXT;
}
return LOC->INFO;
}
struct cl_node *search(int value)
{
struct cl_node *PTR;
if (FIRST == NULL)
return NULL;
if (FIRST == LAST && FIRST->INFO == value)
return FIRST;
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
if (PTR->INFO == value)
return PTR;
if (LAST->INFO == value)
return LAST;
else
return NULL;
}
void print()
{
struct cl_node *PTR;
if (FIRST == NULL){
printf("\nEmpty List!!\n");
return;
}
printf("\nCircular linked list elements:\n");
if (FIRST == LAST){
printf("\t%d\n", FIRST->INFO);
return;
}
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
printf("\t%d", PTR->INFO);
printf("\t%d\n", LAST->INFO);
}

Stack:

1. Write a C program for array implementation of stack


Ans:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack_array[MAX];
int top = -1;
void push(int data) {
if (isFull()) {
printf("Stack overflow!\n");
return;
}
top = top + 1;
stack_array[top] = data;
}
int pop() {
int value;
if (isEmpty()) {
printf("Stack Underflow!\n");
exit(1);
}
value = stack_array[top];
top = top - 1;
return value;
}
int peek() {
if (isEmpty()) {
printf("Stack Underflow!\n");
exit(1);
}
return stack_array[top];
}
void print() {
if (isEmpty()) {
printf("Stack Underflow!\n");
return;
}
for (int i = top; i >= 0; i--) {
printf("%d\n", stack_array[i]);
}
}
int isFull() {
return top == MAX - 1;
}
int isEmpty() {
return top == -1;
}
int main() {
int choice, data;
while (1) {
printf("\n1. Push\n");
printf("2. Pop\n");
printf("3. Print the top element\n");
printf("4. Print all the elements of the stack\n");
printf("5. Quit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to be pushed: ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
printf("Deleted element is %d\n", data);
break;
case 3:
printf("The topmost element of the stack is %d\n", peek());
break;
case 4:
print();
break;
case 5:
exit(0);
}
}
return 0;
}

2. Write a C program for linked implementation of stack


Ans:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int element;
struct stack *next;
}*top;
void push(int);
int pop();
void display();
int main() {
int num1, num2, choice;
while (1) {
printf("Select a choice from the following:\n");
printf("[1] Push an element into the stack\n");
printf("[2] Pop out an element from the stack\n");
printf("[3] Display the stack elements\n");
printf("[4] Exit\n");
printf("Your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to be pushed into the stack: ");
scanf("%d", &num1);
push(num1);
break;

case 2:
num2 = pop();
printf("%d element popped out of the stack\n", num2);
break;

case 3:
display();
break;

case 4:
exit(1);

default:
printf("Invalid choice!\n");
}
}
return 0;
}
void push(int value) {
struct stack *ptr = (struct stack*)malloc(sizeof(struct stack));
ptr->element = value;
ptr->next = top;
top = ptr;
}
int pop() {
if (top == NULL) {
printf("Stack is Empty.\n");
exit(1);
} else {
int temp = top->element;
top = top->next;
return temp;
}
}
void display() {
struct stack *ptr1 = top;
printf("The various stack elements are:\n");
while (ptr1 != NULL) {
printf("%d\t", ptr1->element);
ptr1 = ptr1->next;
}
}

3. Write a C program to for infix to postfix using stack


Ans:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void initialize(Stack *stack) {
stack->top = -1;
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
void push(Stack *stack, char item) {
if (stack->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
stack->data[++stack->top] = item;
}
char pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
return stack->data[stack->top--];
}
int getPrecedence(char operator) {
if (operator == '+' || operator == '-')
return 1;
else if (operator == '*' || operator == '/')
return 2;
else
return 0; }
void infixToPostfix(char *infix, char *postfix) {
Stack stack;
initialize(&stack);
push(&stack, '(');
strcat(infix, ")");
int i = 0, j = 0;
while (!isEmpty(&stack)) {
char current = infix[i];
if (isalnum(current)) {
postfix[j++] = current;
} else if (current == '(') {
push(&stack, '(');
} else if (current == ')') {
while (stack.data[stack.top] != '(') {
postfix[j++] = pop(&stack);
}
pop(&stack);
} else {
while (getPrecedence(current) <= getPrecedence(stack.data[stack.top])) {
postfix[j++] = pop(&stack);
}
push(&stack, current);
}
i++;
}
postfix[j] = '\0';
int main() {
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
printf("Enter the infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}

4. Write a C program for evaluation of postfix ( postfix to infix )


Ans:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Stack {
int *data;
int top;
int capacity;
} Stack;
Stack *createStack(int capacity) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->data = (int *)malloc(capacity * sizeof(int));
stack->top = -1;
stack->capacity = capacity;
return stack;
}
void push(Stack *stack, int item) {
if (stack->top == stack->capacity - 1) {
printf("Stack overflow\n");
exit(1);
}
stack->top++;
stack->data[stack->top] = item;
}
int pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack underflow\n");
exit(1);
}
int item = stack->data[stack->top];
stack->top--;
return item;
}
int evaluatePostfix(char *expression) {
Stack *operandStack = createStack(100);
for (char *symbol = expression; *symbol != '\0'; symbol++) {
if (isdigit(*symbol)) {
push(operandStack, *symbol - '0');
} else if (*symbol == '+' || *symbol == '-' || *symbol == '*' || *symbol == '/') {
int A = pop(operandStack);
int B = pop(operandStack);
int result;
switch (*symbol) {
case '+':
result = B + A;
break;
case '-':
result = B - A;
break;
case '*':
result = B * A;
break;
case '/':
result = B / A;
break;
}
push(operandStack, result);
}
}
int value = pop(operandStack);
free(operandStack->data);
free(operandStack);
return value;
}
int main() {
char postfixExpression[100];
printf("Enter the postfix expression: ");
scanf("%s", postfixExpression);
int result = evaluatePostfix(postfixExpression);
printf("The value of the postfix expression %s is: %d\n", postfixExpression, result);
return 0;
}
5. Write a C program for Tower of Hanoi ( recursive )
Ans:
#include <stdio.h>
void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from %c to %c\n", n, source, destination);
towerOfHanoi(n - 1, auxiliary, destination, source);
}
int main() {
int numDisks;
printf("Enter the number of disks: ");
scanf("%d", &numDisks);
towerOfHanoi(numDisks, 'A', 'C', 'B');
return 0;
}

Tree:

1. Write a C program for array implementation of tree operation- traverse preorder, in-order,
post-order)
Ans:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct bin_tree {
int INFO;
}node;
node tree[MAX_SIZE];
int count = 0;
int insert(int n) {
if (count < MAX_SIZE) {
tree[count].INFO = n;
count = count + 1;
return 1;
}
else {
printf("Tree is full. Cannot insert node.\n");
return 0;
}
}
void preorder(int index) {
if (index < count) {
printf("%d\n", tree[index].INFO);
preorder(2 * index + 1);
preorder(2 * index + 2);
}
}
void inorder(int index) {
if (index < count) {
inorder(2 * index + 1);
printf("%d\n", tree[index].INFO);
inorder(2 * index + 2);
}
}
void postorder(int index) {
if (index < count) {
postorder(2 * index + 1);
postorder(2 * index + 2);
printf("%d\n", tree[index].INFO);
}
}
int main() {
int element, choice;
while (1) {
printf("Select an option\n");
printf("\n1 - Insert");
printf("\n2 - Preorder");
printf("\n3 - Inorder");
printf("\n4 - Postorder");
printf("\n5 - Exit");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:

printf("\n\nEnter the node value: ");


scanf("%d", &element);
if (insert(element))
printf("Node inserted successfully.\n");
break;

case 2:
printf("Preorder Traversal:\n");
preorder(0);
break;

case 3:
printf("Inorder Traversal:\n");
inorder(0);
break;

case 4:
printf("Postorder Traversal:\n");
postorder(0);
break;

case 5:
exit(0);
break;

default:
printf("\nIncorrect choice. Please try again.");
break;
}
}
return 0;
}

You might also like