Report
Report
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:
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;
}
}
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:
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;
}