Você está na página 1de 31

Q1.

Implement singly and doubly linked lists


Answer:
//For singly link list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;

void append(int num)


{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}

void add( int num )


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));

temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;

void insert(int num)


{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}

int delete(int num)


{
struct node *temp, *prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}

}
else
{

prev=temp;
temp= temp->next;

}
}
return 0;
}

void display(struct node *r)


{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf("\n");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int main()
{
int i,num;
struct node *n;
head=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0){

printf("Enter only an Integer\n");


exit(0);
} else {
switch(i)
{
case 1:
printf("Enter the number to insert : ");
scanf("%d",&num);
insert(num);
break;
case 2:
if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3:
printf("Size of the list is %d\n",count());
break;
case 4:
if(head==NULL)
printf("List is Empty\n");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
printf("%d deleted successfully\n",num);
else
printf("%d not found in the list\n",num);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}

*
* C Program to Implement a Doubly Linked List & provide Insertion, Deletion & Display
Operations
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void
void
void
void
void
void
void
void
void

insert1();
insert2();
insert3();
traversebeg();
traverseend(int);
sort();
search();
update();
delete();

int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
printf("\n
printf("\n
printf("\n
printf("\n
printf("\n
printf("\n
printf("\n
printf("\n
printf("\n

1
2
3
4
5
6
7
8
9

Insert at beginning");
Insert at end");
Insert at position i");
Delete at i");
Display from beginning");
Display from end");
Search for element");
Sort the list");
Update an element");

printf("\n 10 - Exit");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);

default:
printf("\n Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter value to node : ");
scanf("%d", &data);
temp->n = data;
count++;
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();

h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To insert at any position */
void insert3()
{
int pos, i = 2;
printf("\n Enter position to be inserted : ");
scanf("%d", &pos);
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf("\n Position out of range to insert");
return;
}
if ((h == NULL) && (pos != 1))
{
printf("\n Empty list cannot insert other than 1st position");
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;

}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
/* To delete an element */
void delete()
{
int i = 1, pos;
printf("\n Enter position to be deleted : ");
scanf("%d", &pos);
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
printf("\n Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf("\n Error : Empty list no elements to delete");
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}

}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next;
== 1 check */
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}
/* Traverse from beginning */
void traversebeg()
{
temp2 = h;
if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
while (temp2->next != NULL)
{
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);
}
/* To traverse from end recursively */
void traverseend(int i)
{
if (temp2 != NULL)

/* Might not need this statement if i

{
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
printf(" %d ", i);
}
}
/* To search for an element in the list */
void search()
{
int data, count = 0;
temp2 = h;
if (temp2 == NULL)
{
printf("\n Error : List empty to search for data");
return;
}
printf("\n Enter value to search : ");
scanf("%d", &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf("\n Data found in %d position",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
}
/* To update a node value in the list */
void update()
{
int data, data1;
printf("\n Enter node data to be updated : ");
scanf("%d", &data);
printf("\n Enter new data : ");
scanf("%d", &data1);

temp2 = h;
if (temp2 == NULL)
{
printf("\n Error : List empty no node to update");
return;
}
while (temp2 != NULL)
{
if (temp2->n == data)
{
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
printf("\n Error : %d not found in list to update", data);
}
/* To sort the linked list */
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
if (temp2 == NULL)
{
printf("\n List empty to sort");
return;
}
for (temp2 = h; temp2 != NULL; temp2 = temp2->next)
{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (temp2->n > temp4->n)
{
x = temp2->n;
temp2->n = temp4->n;

temp4->n = x;
}
}
}
traversebeg();
}

$ cc pgm1.c
$ a.out
1 - Insert at beginning
2 - Insert at end
3 - Insert at position i
4 - Delete at i
5 - Display from beginning
6 - Display from end
7 - Search for element
8 - Sort the list
9 - Update an element
10 - Exit
Enter choice : 1
Enter value to node : 10
Enter choice : 2
Enter value to node : 50
Enter choice : 4
Enter position to be deleted : 1
Node deleted
Enter choice : 1
Enter value to node : 34
Enter choice : 3
Enter position to be inserted : 2

Enter value to node : 13


Enter choice : 4
Enter position to be deleted : 4
Error : Position out of range to delete
Enter choice : 1
Enter value to node : 15
Enter choice : 1
Enter value to node : 67
Enter choice : 3
Enter position to be inserted : 2
Enter value to node : 34
Enter choice : 4
Enter position to be deleted : 3
Node deleted
Enter choice : 7
Enter value to search : 15
Error : 15 not found in list
Enter choice : 8
Linked list elements from begining :
Enter choice : 9
Enter node data to be updated : 45
Enter new data : 89
Error : 45 not found in list to update
Enter choice : 9
Enter node data to be updated : 50

13

34

34

50

67

Q3.Implement a double-ended queue (dequeue) where insertion and deletion


operations are possible at both the ends
Ans:

#define MAX 100


#include<stdio.h>
#include<conio.h>
void insert(int);
int delStart();
int delEnd();
int queue[MAX];
int rear =0, front =0;
void display();
void insertEnd(int);
void insertStart(int);
int main()
{
char ch , a='y';
int choice, c, token;
printf("Enter your choice for which deque operation you want to
perform operation \n");
do
{
printf("\n1.Input-restricted deque \n");
printf("2.output-restricted deque \n");
printf("\nEnter your choice for the operation : ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\nDo operation in Input-Restricted c
deque\n");
printf("1.Insert");

printf("\n2.Delete from end");


printf("\n3.Delete from begning");
printf("\n4.show or display");
do
{
printf("\nEnter your choice for the operation in
c deque: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertEnd(token);
display();
break;
case 2: token=delEnd();
printf("\nThe token deleted is %d",token);
display();
break;
case 3: token=delStart();
printf("\nThe token deleted is %d",token);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n) to do
operation in input-restricted c deque: ");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
break;
case 2 :

printf("\nDo operation in Output-Restricted c


deque\n");
printf("1.Insert at the End");
printf("\n2.Insert at the begning");
printf("\n3.Delete the element");
printf("\n4.show or display");
do
{
printf("\nEnter your choice for the operation:
");
scanf("%d",&choice);
switch(choice)
{
case 1: insertEnd(token);
display();
break;
case 2: insertStart(token);
display();
break;
case 3: token=delStart();
printf("\nThe token deleted is %d",token);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
break ;
}

printf("\nDo you want to continue(y/n):");


ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
}
void display()
{
int i;
printf("\nThe queue elements are:");
for(i=rear;i<front;i++)
{
printf("%d

",queue[i]);

}
}
void insertEnd(int token)
{
char a;
if(front==MAX/2)
{
printf("\nQueue full\nyou cant enter more elements at
the end of c queue");
return;
}
do
{
printf("\nEnter the token to be inserted:");
scanf("%d",&token);
queue[front]=token;
front=front+1;
printf("do you want to continue insertion Y/N");
a=getch();
}
while(a=='y');

}
void insertStart(int token)
{
char a;
if(front==MAX/2)
{
printf("\nQueue full\nyou cant enter more elements at
the start of queue");
return;
}
do
{
printf("\nEnter the token to be inserted:");
scanf("%d",&token);
rear=rear-1;
queue[rear]=token;
printf("do you want to continue insertion Y/N");
a=getch();
}
while(a=='y');
}
int delEnd()
{
int t;
if(front==rear)
{
printf("\nQueue empty");
return 0;
}
front=front-1;
t=queue[front+1];
return t;
}

int delStart()
{
int t;
if(front==rear)
{
printf("\nQueue empty");
return 0;
}
rear=rear+1;
t=queue[rear-1];
return t;
}

Q5.Implement binary search tree


Ans:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
void main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
clrscr();
printf("\nProgram For Binary Search Tree ");
do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");

scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf("\nWant To enter More Elements?(y/n)");
ans = getch();
} while (ans == 'y');
break;
case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
printf("\nParent of node %d is %d", tmp->data, parent->data);
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;

return temp;
}
/*
This function is for creating a binary search tree
*/
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf("\nThe %d Element is Present", temp->data);
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion

*/
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);

Q2.Implement stack and use it to convert infix to postfix expression

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 100
typedef struct stack
{
int data[MAX];
int top;
}stack;
int priority(char);
void init(stack *);
int empty(stack *);
int full(stack *);
char pop(stack *);
void push(stack *,char);
char top(stack *);
void main()
{
stack s;
char x;
int token;
init(&s);
clrscr();
printf("nEnter infix expression:");
while((token=getchar())!='n')
{
if(isalnum(token))
printf("%c",token);
else
if(token == '(')
push(&s,'(');
else
{
if(token == ')')
while((x=pop(&s))!='(')
printf("%c",x);
else
{
while(priority(token)< =priority(top(&s)) && !empty(&s))
{

x=pop(&s);
printf("%c",x);
}
push(&s,token);
}
}
}
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
getch();
}
//--------------------------------------------int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
//--------------------------------------------void init(stack *s)
{
s->top=-1;
}
//--------------------------------------------int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
//--------------------------------------------int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
//---------------------------------------------

void push(stack *s,char x)


{
s->top=s->top+1;
s->data[s->top]=x;
}
//--------------------------------------------char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
//--------------------------------------------char top(stack * s)
{
return(s->data[s->top]);
}
//---------------------------------------------

Q6.Implement insertion in AVL trees


Ans:
#include<stdio.h>
#include<stdlib.h>
// An AVL tree node
struct node
{
int key;
struct node *left;
struct node *right;
int height;
};
// A utility function to get maximum of two integers
int max(int a, int b);
// A utility function to get height of the tree
int height(struct node *N)
{
if (N == NULL)
return 0;
return N->height;
}
// A utility function to get maximum of two integers
int max(int a, int b)
{
return (a > b)? a : b;
}
/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct node* newNode(int key)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->key
= key;
node->left
= NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}
// A utility function to right rotate subtree rooted with y
// See the diagram given above.
struct node *rightRotate(struct node *y)
{
struct node *x = y->left;
struct node *T2 = x->right;

// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
// Return new root
return x;
}
// A utility function to left rotate subtree rooted with x
// See the diagram given above.
struct node *leftRotate(struct node *x)
{
struct node *y = x->right;
struct node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;

// Return new root


return y;

// Get Balance factor of node N


int getBalance(struct node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
struct node* insert(struct node* node, int key)
{
/* 1. Perform the normal BST rotation */
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
/* 2. Update height of this ancestor node */
node->height = max(height(node->left), height(node->right)) + 1;
/* 3. Get the balance factor of this ancestor node to check

whether

this node became unbalanced */


int balance = getBalance(node);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
/* return the (unchanged) node pointer */
return node;

}
// A utility function to print preorder traversal of the tree.
// The function also prints height of every node
void preOrder(struct node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
/* Drier program to test above function*/
int main()
{
struct node *root = NULL;
/* Constructing tree given in the above figure */
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);

root = insert(root, 25);

/* The constructed AVL Tree would be


30
/ \
20
40
/ \
\
10 25
50
*/
printf("Pre order traversal of the constructed AVL tree is \n");
preOrder(root);

return 0;

Output:

Pre order traversal of the constructed AVL tree is


30 20 10 25 40 50
Time Complexity: The rotation operations (left and righ

Você também pode gostar