Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

DS Practical Exam Experiment List Programs

Q1. Write a program to implement Binary Search technique.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

void BubbleSort(int a[], int n);


int BinarySearch(int a[], int n, int key);

int main()
{
int a[100], n, i, key, index;
//clrscr();

printf("Enter the number of elements: ");


scanf("%d", &n);

illuminati
printf("Enter the elements: ");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);

BubbleSort(a, n);
printf("Sorted Array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);

printf("\nEnter the key to be searched: ");


scanf("%d", &key);

index = BinarySearch(a, n, key);

if(index == -1)
printf("\nKey not found!");
else
printf("\nKey found at index %d", index);

//getch();
return 0;
}

void BubbleSort(int a[], int n)


{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = 0; j < n - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

int BinarySearch(int a[], int n, int key)


{
int l, h, mid;
l = 0;
h = n - 1;
while(l <= h)
{
mid = (l + h) / 2;

illuminati
if(key == a[mid])
return mid;
else if(key < a[mid])
h = mid - 1;
else
l = mid + 1;
}
return -1;
}

/*output:
Enter the number of elements: 5
Enter the elements: 5 4 3 2 1
Sorted Array: 1 2 3 4 5
Enter the key to be searched: 3
Key found at index 2
*/
Q2. Write a program to implement Stack using Array.

#include<stdio.h>
#include<conio.h>
#define MAX 20

int stack[MAX], top = -1;

int isEmpty();
int isFull();
void push(int);
int pop();
int peek(); // returns the top element
void display();

int main()
{
int choice, item;

illuminati
//clrscr();
do
{
printf("\n1.Push\n2.Pop\n3.peek\n4.Display\n5.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("\nEnter the item to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
if(item == -1)
printf("\nStack is empty");
else
printf("\nThe popped item is %d", item);
break;
case 3:
item = peek();
if(item == -1)
printf("\nStack is empty");
else
printf("\nThe top element is %d", item);
break;
case 4:
display();
break;
case 5:
printf("\nTerminated");
break;
default:
printf("\nInvalid choice");
}
}while(choice != 5);

//getch();
return 0;
}

int isEmpty()
{
if(top == -1)
return 1;
else
return 0;
}

illuminati
int isFull()
{
if(top == MAX - 1)
return 1;
else
return 0;
}

void push(int item)


{
if(isFull())
{
printf("\nStack is full");
return;
}
top++;
stack[top] = item;
}

int pop()
{
int item;
if(isEmpty())
return -1;
item = stack[top];
top--;
return item;
}
int peek()
{
if(isEmpty())
return -1;
return stack[top];
}

void display()
{
int i;
if(isEmpty())
{
printf("\nStack is empty");
return;
}
printf("\nThe stack elements are: ");
for(i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");

illuminati
}

/*Output:

1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1

Enter the item to be pushed: 10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1

Enter the item to be pushed: 20

1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 3

The stack elements are: 20 10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 4
*/

illuminati
Q3. Write a program to convert an Infix expression into Postfix expression.

#include <stdio.h>
#include<conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 100

char stack[MAX];
int top = -1;

void push(char);
char pop();
int priority(char);

int main()
{

illuminati
char exp[MAX];
char *e, x;
//clrscr();
printf("Enter the expression : ");
scanf("%s", exp);
e = exp;

while(*e != '\0')
{
if(isalnum(*e)) //if it is an operand then print it
printf("%c", *e);

else if(*e == '(') //if it is an opening bracket then push it


push(*e);

else if(*e == ')') //pop closing bracket and print till opening bracket
{
while((x = pop()) != '(')
printf("%c", x);
}

else //pop and print till operator with higher priority is found
{
while(priority(stack[top]) >= priority(*e))
printf("%c", pop());
push(*e);
}
e++;
}

while(top != -1)
printf("%c", pop());

//getch();
return 0;

void push(char x)
{
if(top == MAX - 1)
{
printf("Stack Overflow\n");
return;
}
else
{
top++;
stack[top] = x;
}

illuminati
}

char pop()
{
if(top == -1)
{
printf("Stack Underflow\n");
return -1;
}
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

/*Output:
Enter the expression : (a+b)*c
ab+c*
*/
Q4. Write a program to evaluate Postfix Expression.

//Write a program to evaluate Postfix Expression.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>

#define MAX 100

int stack[MAX];
int top = -1;

void push(char x)
{
stack[++top] = x;

illuminati
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x) //function to return the priority of the operator


{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
if(x == '^')
return 3;
}

int main()
{
char exp[MAX];
char *e, x;
int n1, n2, n3, num;
//clrscr();
printf("Enter the expression: ");
scanf("%s", exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else if(*e == '+' || *e == '-' || *e == '*' || *e == '/' || *e == '^')
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+': n3 = n1 + n2; break;
case '-': n3 = n1 - n2; break;
case '*': n3 = n1 * n2; break;
case '/': n3 = n1 / n2; break;
case '^': n3 = pow(n1, n2); break;
}

illuminati
push(n3);
}
e++;
}
printf("Result of expression %s = %d", exp, pop());
//getch();
return 0;
}

/*Output:
Enter the expression: 231*+9-
Result of expression 231*+9- = -4
*/
Q5. Write a program to implement Stack using Linked List.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*top=NULL;

void push(int item);


int pop();
void Display();

int main()
{

illuminati
int choice, item;
//clrscr();
do
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the item to be pushed: ");
scanf("%d",&item);
push(item);
break;
case 2:
item = pop();
if(item == -1)
printf("\nStack is empty");
else
printf("\nThe popped item is %d",item);
break;
case 3:
Display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("\nInvalid choice\n");
}
} while (choice != 4);

//getch();
return 0;
}

void push(int item)


{
struct Node *t;
t = (struct Node*)malloc(sizeof(struct Node));
if(t == NULL)
printf("stack is full\n");
else
{
t->data = item;
t->next = top;
top = t;
}
}

illuminati
int pop()
{
struct Node *t;
int item = -1;
if(top == NULL)
printf("stack is empty\n");
else
{
t = top;
top = top->next;
item = t->data;
free(t);
}
return item;
}

void Display()
{
struct Node *p;
p = top;
while(p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}

/*output:
1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 1

Enter the item to be pushed: 10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice: 4

Terminated
*/

illuminati
Q6. Write a program to implement Linear Queue using an array.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10

int queue[MAX], front = -1, rear = -1;

void enqueue();
int dequeue();
void display();

int main()
{
int choice, item;
//clrscr();
do

illuminati
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
enqueue();
break;
case 2:
item = dequeue();
if(item == -1)
printf("\nQueue is empty");
else
printf("\nDeleted item is %d", item);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("\nInvalid choice");
}
} while (choice != 4);

// getch();
return 0;
}
void enqueue()
{
int item;
if(rear == MAX - 1)
printf("\nQueue is full");
else
{
if(front == -1)
front = 0;
printf("\nEnter the item: ");
scanf("%d", &item);
rear++;
queue[rear] = item;
}
}

int dequeue()
{

illuminati
int item;
if(front == -1 || front > rear)
return -1;
else
{
item = queue[front];
front++;
return item;
}
}

void display()
{
int i;
if(front == -1 || front > rear)
printf("\nQueue is empty");
else
{
printf("\nQueue is: ");
for(i = front; i <= rear; i++)
printf("%d ", queue[i]);
}
printf("\n");
}

/*Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1

Enter the item: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice: 4

Terminated
*/

illuminati
Q7. Write a program to implement Linear Queue using Linked List.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
}*front=NULL,*rear=NULL;

void enqueue(int item);


int dequeue();
void display();

int main()

illuminati
{
int choice, item;
//clrscr();
do
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
enqueue(item);
break;
case 2:
item = dequeue();
if(item == -1)
printf("\nQueue is empty");
else
printf("\nDeleted element is %d",item);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("\nInvalid choice");
}
} while (choice != 4);

// getch();
return 0;
}

void enqueue(int item)


{
struct Node *t;
t = (struct Node*)malloc(sizeof(struct Node));
if(t == NULL)
printf("Queue is Full\n");
else
{
t->data = item;
t->next = NULL;
if(front == NULL)
front = rear = t;

illuminati
else
{
rear->next = t;
rear = t;
}
}
}

int dequeue()
{
int item;
struct Node *t;
if(front == NULL)
return -1;
else
{
item = front->data;
t = front;
front = front->next;
free(t);
return item;
}
}

void display()
{
struct Node *p;
p = front;
if(front == NULL)
printf("\nQueue is empty");
else
{
printf("\nQueue is: ");
while(p)
{
printf("%d ",p->data);
p = p->next;
}
}
}

/*output:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice: 1

Enter the element to be inserted: 10

illuminati
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice: 4

Terminated
*/
Q8. Write a program to implement Circular Queue using an array.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 10

int queue[MAX], front = -1, rear = -1;

void enqueue(int);
int dequeue();
void display();

int main()
{
int choice, item;
//clrscr();

illuminati
do
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("\nEnter the item to be inserted: ");
scanf("%d", &item);
enqueue(item);
break;
case 2:
item = dequeue();
if(item == -1)
printf("\nQueue is empty");
else
printf("\nThe deleted item is %d", item);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("\nInvalid choice");
}
} while (choice != 4);
// getch();
return 0;
}

void enqueue(int item)


{
if((rear + 1) % MAX == front)
{
printf("Queue is Full");
return;
}
else
{
rear = (rear + 1) % MAX;
queue[rear] = item;
}
}

illuminati
int dequeue()
{
int item = -1;
if(front == rear)
printf("Queue is Empty\n");
else
{
front=(front + 1) % MAX;
item = queue[front];
}
return item;
}

void display()
{
int i = front + 1;
if(front == rear)
printf("Queue is Empty\n");
else
{
do
{
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}while(i!=(rear + 1) % MAX);
}
}

/*Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1

Enter the item to be inserted: 10

1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4

Terminated
*/

illuminati
Q9. Write a program to perform following operations on Binary Search Tree
1. Insert a node
2. Search for a given node
3. Display (In Order, Preorder, Postorder)

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct node
{
int data;
struct node *lchild;
struct node *rchild;
}*root=NULL;

void insert(int);
int search(int);

illuminati
void inorder(struct node *);
void preorder(struct node *);
void postorder(struct node *);

int main()
{
int choice, item, flag;
//clrscr();

do
{

printf("\n1.Insert\n2.Search\n3.Inorder\n4.Preorder\n5.Postorder\n6.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the item to be inserted: ");
scanf("%d",&item);
insert(item);
break;
case 2:
printf("\nEnter the item to be searched: ");
scanf("%d",&item);
flag=search(item);
if(flag==1)
printf("\nItem found");
else
printf("\nItem not found");
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
printf("\nTerminated");
break;
default:
printf("\nInvalid choice");
}
} while (choice != 6);

illuminati
// getch();
return 0;
}

void insert(int item)


{
struct node *ptr, *parent, *temp;
ptr=(struct node *)malloc(sizeof(struct node));
ptr->data=item;
ptr->lchild=NULL;
ptr->rchild=NULL;
if(root==NULL)
root=ptr;
else
{
temp=root;
while(temp!=NULL)
{
parent=temp;
if(item<temp->data)
temp=temp->lchild;
else
temp=temp->rchild;
}
if(item<parent->data)
parent->lchild=ptr;
else
parent->rchild=ptr;
}
}
int search(int item)
{
struct node *temp;
temp=root;
while(temp!=NULL)
{
if(item==temp->data)
return 1;
else if(item<temp->data)
temp=temp->lchild;
else
temp=temp->rchild;
}
return 0;
}

void inorder(struct node *temp)


{

illuminati
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%d\t",temp->data);
inorder(temp->rchild);
}
}

void preorder(struct node *temp)


{
if(temp!=NULL)
{
printf("%d\t",temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}

void postorder(struct node *temp)


{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d\t",temp->data);
}
}

/*Output:
1.Insert
2.Search
3.Inorder
4.Preorder
5.Postorder
6.Exit
Enter your choice: 1

Enter the item to be inserted: 10

1.Insert
2.Search
3.Inorder
4.Preorder
5.Postorder
6.Exit
Enter your choice: 1

Enter the item to be inserted: 5

illuminati
1.Insert
2.Search
3.Inorder
4.Preorder
5.Postorder
6.Exit
Enter your choice: 1

Enter the item to be inserted: 15

1.Insert
2.Search
3.Inorder
4.Preorder
5.Postorder
6.Exit
Enter your choice: 3

5 10 15

1.Insert
2.Search
3.Inorder
4.Preorder
5.Postorder
6.Exit
Enter your choice: 6

Terminated
*/
Q10. Write a program to implement Depth First Search traversal of a graph.

#include<stdio.h>
#include<conio.h>
#define MAX 20

int a[20][20] = {0}; // adjacency matrix


int visited[20] = {0}; // visited array
int n; //no of vertices
void dfs(int v);
void input();

int main()
{
int start;
//clrscr();
input();
printf("Enter the start vertex: ");

illuminati
scanf("%d",&start); dfs(start);
//getch();
return 0;
}

void input()
{
int i,j;
printf("Enter no. of vertices: ");
scanf("%d", &n);
printf("Enter adjacency matrix:\n");
for(i=0; i < n; i++)
{
for(j=0; j < n; j++)
scanf("%d", &a[i][j]);
}
}

void dfs(int v)
{
int i;
visited[v] = 1;
printf("%d\t", v);
for(i = 0; i < n; i++)
{
if(a[v][i] == 1 && visited[i] == 0)
dfs(i);
}
}

/*Output:
Enter no. of vertices: 4
Enter adjacency matrix:
0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0
Enter the start vertex: 0
0 1 3 2
*/

illuminati
Q11. Write a Program to implement Breadth First Search traversal of a graph.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20

int a[MAX][MAX], q[MAX], visited[MAX], n, i, j, f=0, r=-1;


//f=front, r=rear, n=no. of vertices, a=adjacency matrix, q=queue, visited=visited
array

void enqueue(int v); //v: vertex


int dequeue();
void input();
void bfs(int v);

int main()
{

illuminati
int v;
//clrscr();
input();
printf("Enter the starting vertex: ");
scanf("%d", &v);
bfs(v);
//getch();
return 0;
}

void enqueue(int v)
{
if(r == MAX-1) //r: rear of queue
printf("Queue Overflow\n");
else
{
r++;
q[r] = v;
}
}

int dequeue()
{
int v;
if(f > r) //f: front of queue
{
printf("Queue Underflow\n");
return -1;
}
else
{
v = q[f];
f++;
return v;
}
}

void input()
{
int i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for(i=0; i< n; i++)
{
q[i] = 0;
visited[i] = 0;
}
printf("Enter the adjacency matrix:\n");
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)

illuminati
scanf("%d", &a[i][j]);
}

void bfs(int v)
{
int i;
enqueue(v);
visited[v] = 1; //marked the starting vertex as visited
while(f <= r)
{
v = dequeue();
printf("%d ", v);
for(i = 0; i < n; i++)
{
if(a[v][i] && !visited[i]) //if there is an edge between v and i
{
enqueue(i);
visited[i] = 1;
}
}
}
for(i = 0; i < n; i++)
{
if(!visited[i]) //if there is a vertex which is not visited
bfs(i);
}
}

/*Output:
Enter the number of vertices: 4
Enter the adjacency matrix:
0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0
Enter the starting vertex: 0
0 1 2 3
*/

illuminati
Q12. Write a Program to implement a Singly linked list with following choices.
1. Create a list
2. Insert a node at beginning
3. Delete a node from beginning
4. Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*first = NULL;

void create(int x);

illuminati
void display();
void insertBeg(int x);
int deleteBeg();

int main()
{
int choice, x;
//clrscr();
printf("Enter an element to create list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1. Insert at beginning");
printf("\n2. Delete from beginning");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertBeg(x);
break;
case 2:
x = deleteBeg();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("Invalid choice");
}
} while (choice != 4);

// getch();
return 0;
}

illuminati
void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
first = t;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}

void insertBeg(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = first;
first = t;
}

int deleteBeg()
{
int x = -1;
struct Node *p = first;
if(first != NULL)
{
first = first->next;
x = p->data;
free(p);
}
return x;
}

illuminati
Q13. Write a Program to implement a Singly linked list with following choices.
1. Create a list
2. Insert a node at end
3. Delete a node from end
4. Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*first = NULL;

void create(int x);

illuminati
void insertEnd(int x);
int deleteEnd();
void display();

int main()
{
int choice, x;
//clrscr();
printf("Enter an element to create list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1. Insert at end");
printf("\n2. Delete from end");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertEnd(x);
break;
case 2:
x = deleteEnd();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("Invalid choice");

}
}while(choice != 4);
//getch();
return 0;
}

illuminati
void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
first = t;
}

void insertEnd(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *p = first, *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
while (p->next != NULL)
p = p->next;
p->next = t;
}
}

int deleteEnd()
{
struct Node *p = first, *del;
int x = -1;
if (first == NULL)
return -1;
else if (p->next == NULL)
{
x = p->data;
free(p);
first = NULL;
}
else
{
while (p->next->next != NULL)
p = p->next;
del = p->next;
x = del->data;
p->next = NULL;
free(del);
}
return x;
}

illuminati
void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q14. Write a Program to implement a Singly linked list with following choices.
1. Create a list
2. Insert a node at beginning
3. Delete a node from end
4. Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*first = NULL;

void create(int x);

illuminati
void insertBeg(int x);
int deleteEnd();
void display();

int main()
{
int choice, x;
//clrscr();
printf("Enter an element to create list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1. Insert at Beginning");
printf("\n2. Delete from end");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertBeg(x);
break;
case 2:
x = deleteEnd();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("Invalid choice");

}
}while(choice != 4);
//getch();
return 0;
}

illuminati
void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
first = t;
}

void insertBeg(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = first;
first = t;
}

int deleteEnd()
{
struct Node *p = first, *del;
int x = -1;
if (first == NULL)
return -1;
else if (p->next == NULL)
{
x = p->data;
free(p);
first = NULL;
}
else
{
while (p->next->next != NULL)
p = p->next;
del = p->next;
x = del->data;
p->next = NULL;
free(del);
}
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{

illuminati
struct Node *p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q15. Write a Program to implement a Singly linked list with following choices.
1. Create a list
2. Insert a node at end
3. Delete a node from beginning
4. Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*first = NULL;

void create(int x);

illuminati
void insertEnd(int x);
int deleteEnd();
void display();

int main()
{
int choice, x;
//clrscr();
printf("Enter an element to create list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1. Insert at End");
printf("\n2. Delete from Beginning");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertEnd(x);
break;
case 2:
x = deleteEnd();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("Invalid choice");

}
}while(choice != 4);
//getch();
return 0;
}

illuminati
void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
first = t;
}

void insertEnd(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *p = first, *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
while (p->next != NULL)
p = p->next;
p->next = t;
}
}

int deleteBeg()
{
int x = -1;
struct Node *p = first;
if(first != NULL)
{
first = first->next;
x = p->data;
free(p);
}
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p = first;
while(p != NULL)
{

illuminati
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q16. Write a Program to implement a Singly linked list with following choices.
1. Create a list
2. Insert a node at beginning
3. Delete a node after some specific node
4. Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*first = NULL;

void create(int x);

illuminati
void insertBeg(int x);
int deleteAfter(int pos);
void display();
int length();

int main()
{
int choice, x, pos;
//clrscr();
printf("Enter an element to create list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1. Insert at beginning");
printf("\n2. Delete after specific position");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertBeg(x);
break;
case 2:
printf("Enter the position after which you want to delete: ");
scanf("%d", &pos);
x = deleteAfter(pos);
if(x == -1)
printf("List is empty\n");
else if(x == -2)
printf("Invalid position\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("Invalid choice");

illuminati
}
}while(choice != 4);
//getch();
return 0;
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
first = t;
}

int length()
{
struct Node *p = first;
int len = 0;
while(p)
{
len++;
p = p->next;
}
return len;
}

void insertBeg(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = first;
first = t;
}

int deleteAfter(int pos)


{
Node *p,*q = NULL;
int x = -1;
if(pos < 0 || pos >= length())
return -2;
if(pos == 0)
{
p = first;
first = first->next;
x = p->data;
delete p;
}

illuminati
else
{
p = first;
for(int i = 0; i < pos; i++)
{
q = p;
p = p->next;
}
q->next = p->next;
x = p->data;
delete p;
}
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q17. Write a Program to implement a Singly linked list with following choices.
1. Create a list
2. Insert a node after some specific node
3. Delete a node from beginning
4. Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct Node
{
int data;
struct Node *next;
}*first = NULL;

void create(int x);


void insertAfter(int x, int pos); // insert x after pos

illuminati
int deleteBeg();
void display();
int length();

int main()
{
int choice, x, pos;
//clrscr();
printf("Enter an element to create list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1. Insert after specific position");
printf("\n2. Delete from beginning");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
printf("Enter position to insert: ");
scanf("%d", &pos);
insertAfter(x, pos);
break;
case 2:
x = deleteBeg();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("\nTerminated");
break;
default:
printf("Invalid choice");

}
}while(choice != 4);
//getch();

illuminati
return 0;
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
first = t;
}

int length()
{
struct Node *p = first;
int len = 0;
while(p)
{
len++;
p = p->next;
}
return len;
}

void insertAfter(int x, int pos)


{
struct Node *t, *p;
int i;
if(pos < 0 || pos > length())
printf("Invalid position\n");
else
{
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
p = first;
if(pos == 0)
{
t->next = first;
first = t;
}
else
{
for(i = 0; i < pos; i++)
p = p->next;
t->next = p->next;
p->next = t;
}
}

illuminati
}

int deleteBeg()
{
int x = -1;
struct Node *p = first;
if(first != NULL)
{
first = first->next;
x = p->data;
free(p);
}
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q18. Write a program to implement a Doubly linked list with following choices.
1. Create a list
2. Insert a node at beginning
3. Delete a node from beginning
4. Display

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
}*first = NULL;

illuminati
void create(int x);
void insertBeg(int x);
int deleteBeg();
void display();

int main()
{
int choice, x;
//clrscr();
printf("\nEnter an element to create a list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1.Insert at beginning");
printf("\n2.Delete from beginning");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertBeg(x);
break;
case 2:
x = deleteBeg();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("Terminated");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);

// getch();
return 0;

illuminati
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
t->prev = NULL;
first = t;
}

void insertBeg(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = first;
t->prev = NULL;
first->prev = t;
first = t;
}
}

int deleteBeg()
{
struct Node *p = first;
int x = -1;
if(first == NULL)
return x;
else if (p->next == NULL)
{
x = p->data;
free(p);
first = NULL;
}
else
{
first = first->next;
if (first) first->prev = NULL;
x = p->data;
free(p);
}
return x;
}

illuminati
void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p;
p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q19. Write a program to implement a Doubly linked list with following choices.
1. Create a list
2. Insert a node at end
3. Delete a node from end
4. Display

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
}*first = NULL;

illuminati
void create(int x);
void insertEnd(int x);
int deleteEnd();
void display();

int main()
{
int choice, x;
//clrscr();
printf("\nEnter an element to create a list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1.Insert at End");
printf("\n2.Delete from End");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertEnd(x);
break;
case 2:
x = deleteEnd();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("Terminated");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);

// getch();
return 0;

illuminati
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
t->prev = NULL;
first = t;
}

void insertEnd(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *t, *p;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
p = first;
while(p->next != NULL)
p = p->next;
p->next = t;
t->prev = p;
}
}
int deleteEnd()
{
if(first == NULL)
return -1;
else
{
struct Node *p;
int x;
p = first;
while(p->next != NULL)
p = p->next;
x = p->data;
if(p == first)
first = NULL;
else
p->prev->next = NULL;
free(p);
return x;
}

illuminati
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p;
p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q20. Write a program to implement a Doubly linked list with following choices.
1. Create a list
2. Insert a node at end
3. Delete a node from beginning
4. Display

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
}*first = NULL;

illuminati
void create(int x);
void insertEnd(int x);
int deleteBeg();
void display();

int main()
{
int choice, x;
//clrscr();
printf("\nEnter an element to create a list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1.Insert at End");
printf("\n2.Delete from End");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertEnd(x);
break;
case 2:
x = deleteBeg();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("Terminated");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);

// getch();
return 0;

illuminati
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
t->prev = NULL;
first = t;
}

void insertEnd(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *t, *p;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
p = first;
while(p->next != NULL)
p = p->next;
p->next = t;
t->prev = p;
}
}
int deleteBeg()
{
struct Node *p = first;
int x = -1;
if(first == NULL)
return x;
else if (p->next == NULL)
{
x = p->data;
free(p);
first = NULL;
}
else
{
first = first->next;
if (first) first->prev = NULL;
x = p->data;
free(p);
}

illuminati
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p;
p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q21. Write a program to implement a Doubly linked list with following choices.
1. Create a list
2. Insert a node at beginning
3. Delete a node from end

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
}*first = NULL;

void create(int x);

illuminati
void insertBeg(int x);
int deleteEnd();
void display();

int main()
{
int choice, x;
//clrscr();
printf("\nEnter an element to create a list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1.Insert at Beginning");
printf("\n2.Delete from End");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertBeg(x);
break;
case 2:
x = deleteEnd();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("Terminated");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);

// getch();
return 0;
}

illuminati
void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
t->prev = NULL;
first = t;
}

void insertBeg(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = first;
t->prev = NULL;
first->prev = t;
first = t;
}
}

int deleteEnd()
{
if(first == NULL)
return -1;
else
{
struct Node *p;
int x;
p = first;
while(p->next != NULL)
p = p->next;
x = p->data;
if(p == first)
first = NULL;
else
p->prev->next = NULL;
free(p);
return x;
}
}

void display()

illuminati
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p;
p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
Q22. Write a program to implement a Doubly linked list with following choices.
1. Create a list
2. Insert a node at beginning
3. Delete a node after some specific node
4. Display

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
}*first = NULL;

illuminati
void create(int x);
int length();
void insertBeg(int x);
int deleteAfter(int pos);
void display();

int main()
{
int choice, x, pos;
//clrscr();
printf("\nEnter an element to create a list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1.Insert at Beginning");
printf("\n2.Delete after the given position");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
insertBeg(x);
break;
case 2:
printf("Enter the position after which to delete: ");
scanf("%d", &pos);
x = deleteAfter(pos);
if(x == -1)
printf("List is empty\n");
else if(x == -2)
printf("Invalid position\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("Terminated");
break;
default:
printf("Invalid choice\n");

illuminati
}
} while (choice != 4);

// getch();
return 0;
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
t->prev = NULL;
first = t;
}

int length()
{
struct Node *p = first;
int len = 0;
while(p)
{
len++;
p = p->next;
}
return len;
}

void insertBeg(int x)
{
if(first == NULL)
create(x);
else
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = first;
t->prev = NULL;
first->prev = t;
first = t;
}
}

int deleteAfter(int pos)


{
Node *p,*q = NULL;
int x = -1;

illuminati
if(pos < 0 || pos >= length())
return -2;
else if(pos == 0)
{
p = first;
first = first->next;
x = p->data;
delete p;
}
else
{
p = first;
for(int i = 0; i < pos; i++)
p = p->next;
p->prev->next = p->next;
if(p->next)
p->next->prev = p->prev;
x = p->data;
free(p);
}
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p;
p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}

illuminati
Q23. Write a program to implement a Doubly linked list with following choices.
1. Create a list
2. Insert a node before some specific node
3. Delete a node from beginning
4. Display

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
}*first = NULL;

illuminati
void create(int x);
int length();
void insertAfter(int x, int pos);
int deleteBeg();
void display();

int main()
{
int choice, x, pos;
//clrscr();
printf("\nEnter an element to create a list: ");
scanf("%d", &x);
create(x);

do
{
printf("\n1.Insert After given position");
printf("\n2.Delete from beginning");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter an element to insert: ");
scanf("%d", &x);
printf("Enter the position after which to insert: ");
scanf("%d", &pos);
insertAfter(x, pos);
break;
case 2:
x = deleteBeg();
if(x == -1)
printf("List is empty\n");
else
printf("Deleted element is %d\n", x);
break;
case 3:
display();
break;
case 4:
printf("Terminated");
break;
default:
printf("Invalid choice\n");
}

illuminati
} while (choice != 4);

// getch();
return 0;
}

void create(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
t->prev = NULL;
first = t;
}

int length()
{
struct Node *p = first;
int len = 0;
while(p)
{
len++;
p = p->next;
}
return len;
}

void insertAfter(int x, int pos)


{
struct Node *t, *p;
int i;
if(pos < 0 || pos > length())
printf("Invalid position\n");
else
{
t = (struct Node *)malloc(sizeof(struct Node));
t->data = x;
if(pos == 0)
{
t->next = first;
t->prev = NULL;
first->prev = t;
first = t;
}
else
{
p = first;
for(i = 0; i < pos; i++)

illuminati
p = p->next;
t->next = p->next;
t->prev = p;
if(p->next) // if p is not the last node
p->next->prev = t;
p->next = t;
}
}
}

int deleteBeg()
{
struct Node *p = first;
int x = -1;
if(first == NULL)
return x;
else if (p->next == NULL)
{
x = p->data;
free(p);
first = NULL;
}
else
{
first = first->next;
if (first) first->prev = NULL;
x = p->data;
free(p);
}
return x;
}

void display()
{
if(first == NULL)
printf("List is empty\n");
else
{
struct Node *p;
p = first;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}

illuminati

You might also like