Вы находитесь на странице: 1из 26

AAA Assessment-1

1)
C program for linked list:
#include<stdio.h>
#include<stdlib.h
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;

int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();

while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\n---- Insert Menu ----");

VASANTHAN R
19PB10
AAA Assessment-1

printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit");


printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;

case 2: display();
break;

case 3: printf("\n---- Delete Menu ----");


printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified
position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: delete_beg();
break;
case 2: delete_end();
break;

VASANTHAN R
19PB10
AAA Assessment-1

case 3: delete_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}

void insert_beg()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;

if(start==NULL)
{
t->next=NULL;
start=t;
}
else
{
t->next=start;
start=t;
}
}

VASANTHAN R
19PB10
AAA Assessment-1

void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;

if(start==NULL)
{
start=t;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}

int insert_pos()
{
int pos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}

VASANTHAN R
19PB10
AAA Assessment-1

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


printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}

q=q->next;
}

t->next=q->next;
q->next=t;
return 0;
}

void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{

VASANTHAN R
19PB10
AAA Assessment-1

q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}

void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}

void delete_end()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else

VASANTHAN R
19PB10
AAA Assessment-1

{
q=start;
while(q->next->next!=NULL)
q=q->next;

t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}

int delete_pos()
{
int pos,i;

if(start==NULL)
{
printf("List is empty!!");
return 0;
}

printf("Enter position to delete:");


scanf("%d",&pos);

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;

VASANTHAN R
19PB10
AAA Assessment-1

}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);
return 0;
}

2)
C program for queue:
#include <stdio.h>
#define MAX 50
void enqueue();
void dequeue();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Enqueue element to queue \n");
printf("2.Dequeue element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)

VASANTHAN R
19PB10
AAA Assessment-1

{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}

void enqueue()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}

VASANTHAN R
19PB10
AAA Assessment-1

}
void dequeue()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}

VASANTHAN R
19PB10
AAA Assessment-1

3)
C program for stack:
#include<stdio.h>
#include<stdlib.h>

#define MAX 5

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;

while(1)
{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);

VASANTHAN R
19PB10
AAA Assessment-1

default: printf("\nWrong Choice!!");


}
}
}

void push()
{
int val;

if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);

VASANTHAN R
19PB10
AAA Assessment-1

top=top-1;
}
}

void display()
{
int i;

if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}

4)
C program for BFS:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct queue* createQueue();
void enqueue(struct queue* q, int);

VASANTHAN R
19PB10
AAA Assessment-1

int dequeue(struct queue* q);


void display(struct queue* q);
int isEmpty(struct queue* q);
void printQueue(struct queue* q);
struct node
{
int vertex;
struct node* next;
};
struct node* createNode(int);
struct Graph
{
int numVertices;
struct node** adjLists;
int* visited;
};
struct Graph* createGraph(int vertices);
void addEdge(struct Graph* graph, int src, int dest);
void printGraph(struct Graph* graph);
void bfs(struct Graph* graph, int startVertex);
int main()
{
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;

VASANTHAN R
19PB10
AAA Assessment-1

}
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();

graph->visited[startVertex] = 1;
enqueue(q, startVertex);

while(!isEmpty(q)){
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);

struct node* temp = graph->adjLists[currentVertex];

while(temp) {
int adjVertex = temp->vertex;
if(graph->visited[adjVertex] == 0){
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}

struct node* createNode(int v)


{
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}

VASANTHAN R
19PB10
AAA Assessment-1

struct Graph* createGraph(int vertices)


{
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node*));


graph->visited = malloc(vertices * sizeof(int));

int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
}

void addEdge(struct Graph* graph, int src, int dest)


{
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;

newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;

VASANTHAN R
19PB10
AAA Assessment-1

q->rear = -1;
return q;
}
int isEmpty(struct queue* q) {
if(q->rear == -1)
return 1;
else
return 0;
}
void enqueue(struct queue* q, int value){
if(q->rear == SIZE-1)
printf("\nQueue is Full!!");
else {
if(q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}

int dequeue(struct queue* q){


int item;
if(isEmpty(q)){
printf("Queue is empty");
item = -1;
}
else{
item = q->items[q->front];
q->front++;
if(q->front > q->rear){
printf("Resetting queue");
q->front = q->rear = -1;

VASANTHAN R
19PB10
AAA Assessment-1

}
}
return item;
}
void printQueue(struct queue *q) {
int i = q->front;
if(isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for(i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
5)C program for tree
# include <stdio.h>
# include <malloc.h>

struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
void find(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;

if(root==NULL)
{
*loc=NULL;

VASANTHAN R
19PB10
AAA Assessment-1

*par=NULL;
return;
}
if(item==root->info)
{
*loc=root;
*par=NULL;
return;
}

if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;

while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=NULL;
*par=ptrsave;
}

VASANTHAN R
19PB10
AAA Assessment-1

void insert(int item)


{ struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
printf("Item already present");
return;
}

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


tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;

if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}

void case_a(struct node *par,struct node *loc )


{
if(par==NULL)
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else

VASANTHAN R
19PB10
AAA Assessment-1

par->rchild=NULL;
}

void case_b(struct node *par,struct node *loc)


{
struct node *child;
if(loc->lchild!=NULL)
child=loc->lchild;
else
child=loc->rchild;

if(par==NULL )
root=child;
else
if( loc==par->lchild)
par->lchild=child;
else
par->rchild=child;
}

void case_c(struct node *par,struct node *loc)


{
struct node *ptr,*ptrsave,*suc,*parsuc;

ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}

VASANTHAN R
19PB10
AAA Assessment-1

suc=ptr;
parsuc=ptrsave;

if(suc->lchild==NULL && suc->rchild==NULL)


case_a(parsuc,suc);
else
case_b(parsuc,suc);

if(par==NULL)
root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;

suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}
int del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
printf("Tree empty");
return 0;
}

find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");

VASANTHAN R
19PB10
AAA Assessment-1

return 0;
}

if(location->lchild==NULL && location->rchild==NULL)


case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}

int preorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return 0;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}

void inorder(struct node *ptr)


{
if(root==NULL)

VASANTHAN R
19PB10
AAA Assessment-1

{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}

void postorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}

void display(struct node *ptr,int level)


{
int i;
if ( ptr!=NULL )
{

VASANTHAN R
19PB10
AAA Assessment-1

display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}
}
main()
{
int choice,num;
root=NULL;
while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Postorder Traversal\n");
printf("6.Display\n");
printf("7.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num);
break;

VASANTHAN R
19PB10
AAA Assessment-1

case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
break;
default:
printf("Wrong choice\n");
}
}
}

VASANTHAN R
19PB10

Вам также может понравиться