0% found this document useful (0 votes)
11 views46 pages

Ds File

ds file

Uploaded by

Pallavi Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views46 pages

Ds File

ds file

Uploaded by

Pallavi Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

PRACTICAL-1

Singly Linked List:


#include <stdio.h>

#include <stdlib.h>

struct node {

int info;

struct node *link;

};

struct node *start=NULL;

void add( int data){

struct node *temp,*ptr;

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

temp ->info=data;

if(start == NULL){

temp -> link = NULL;

start = temp;

else{

ptr = start;

while(ptr ->link!=NULL){

ptr =ptr ->link;

ptr ->link = temp;

temp -> link = NULL;

};

void insert_pos() {

int ele,pos,i;

printf("Enter element: ");

scanf("%d",&ele);

printf("Enter position: ");


scanf("%d",&pos);

struct node* pip,*p;

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

pip->info=ele;

p=start;

for(i=1;i<pos-1;i++){

p=p->link;

pip->link=p->link;

p->link=pip;

void insert_end() {

int data;

scanf(" %d",&data);

struct node* temp, *p;

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

temp->info=data;

p=start;

while(p->link!=NULL){

p=p->link;

p->link=temp;

temp->link=NULL;

void insert_beg() {

int data;

scanf("%d",&data);

struct node* temp;

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

temp->info=data;

temp->link=start;

start=temp;
}

void del_beg(){

start= start->link;

void del_end(){

struct node *pde;

pde = start;

while(pde->link->link!=NULL){

pde=pde->link;

pde->link=NULL;

void del_pos(){

int pos,i;

struct node *pdp;

printf("Enter positon: ");

scanf("%d",&pos);

pdp=start;

for(i=1;i<pos-1;i++){

pdp=pdp->link;

pdp->link = pdp->link->link;

void length(){

int count=0;

struct node* pl;

pl= start;

while(pl!=NULL){

count++;

pl=pl->link;

printf("Length = %d\n",count);
}

void search(){

int ele;

int count=1;

printf("Enter element: ");

scanf("%d",&ele);

struct node* ps;

ps= start;

while(ps!=NULL){

if(ps->info==ele){

printf("Found at : %d" ,count);

break;

ps=ps->link;

count++;

if(ps==NULL){

printf("Not found");

void reverse(){

struct node* prev=NULL;

struct node* current=start;

struct node* next;

while(current!=NULL){

next =current->link;

current->link=prev;

prev=current;

current=next;

start =prev;

}
void traverse(){

struct node * p;

p=start;

while(p != NULL ){

printf("%d\t",p->info);

p=p->link;

int main(){

int n,i,data;

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

scanf("%d",&n);

printf("Enter values: \n");

for( i=0;i<n;i++){

scanf("%d",&data);

add(data);

int x;

printf("\nEnter an operation:\n1.Insert at end\n2.Insert at beginning\n3.Insert at


position\n4.Delete at beginning\n5.Delete at end\n6.Delete at position\n7.Length\
n8.Search\n9.Reverse\n");

scanf("%d",&x);

switch(x){

case 1: insert_end();

break;

case 2: insert_beg();

break;

case 3: insert_pos();

break;

case 4: del_beg();

break;

case 5: del_end();

break;
case 6: del_pos();

break;

case 7: length();

break;

case 8: search();

break;

case 9: reverse();

break;

traverse();

Output:

PRACTICAL-2
Doubly Linked List:
#include <stdio.h>

#include <stdlib.h>

struct node{

int info;
struct node *next;

struct node *prev;

};

struct node *start = NULL;

struct node *end = NULL;

void add( int data){

struct node *temp,*ptr;

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

temp ->info=data;

if(start == NULL){

temp -> next = NULL;

temp -> prev = NULL;

start = temp;

end = temp;

else{

ptr = start;

while(ptr -> next !=NULL){

ptr =ptr -> next;

temp -> next = NULL;

temp -> prev = ptr;

ptr -> next = temp;

end = temp;

void insert_beg() {

int data;

scanf("%d",&data);

struct node* temp;

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

temp->info=data;
temp->next=start;

temp->prev=NULL;

start->prev=temp;

start=temp;

void insert_end(){

int data;

scanf("%d",&data);

struct node *pie,*p;

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

pie->info=data;

p=start;

while(p->next!=NULL){

p=p->next;

p->next=pie;

pie->next=NULL;

pie->prev=p;

end=pie;

void insert_end1(){

int data;

scanf("%d",&data);

struct node *pie;

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

pie->prev=end->next;

pie=end;

void insert_pos(){

struct node *pip,*p;

int eip,i,pos;

printf("Enter element: ");


scanf("%d",&eip);

printf("Enter position: ");

scanf("%d",&pos);

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

pip->info=eip;

p=start;

for(i=1;i<pos-1;i++){

p=p->next;

pip->next=p->next;

pip->prev=p->next->prev;

p->next->prev=pip;

p->next=pip;

void del_beg(){

start->next->prev=NULL;

start=start->next;

void del_end(){

struct node *pde;

pde=start;

while(pde->next->next!=NULL){

pde=pde->next;

pde->next=NULL;

end=pde;

void del_end1(){

struct node *pde;

// pde=end;

// pde->link=NULL;

end->prev->next=NULL;
end=end->prev;

void del_pos(){

int pos,i;

struct node *pdp;

printf("Enter position: ");

scanf("%d",&pos);

pdp=start;

for(i=1;i<pos-1;i++){

pdp=pdp->next;

pdp->next=pdp->next->next;

pdp->next->prev=pdp;

void length(){

int count=0;

struct node *pl;

pl=start;

while(pl!=NULL){

count++;

pl=pl->next;

printf("Length = %d\n",count);

void search(){

int element,p=1;

printf("Enter element:");

scanf("%d",&element);

struct node*ps;

ps=start;

while(ps!=NULL){

if(ps->info==element){
printf("Found at %d\n",p);

break;

ps=ps->next;

p++;

if(ps==NULL){

printf("Not Found");

void reverse(){

struct node*prev=NULL;

struct node*curr=start;

struct node*next;

printf("Reversed list:\n");

while(curr!=NULL){

next=curr->next;

curr->next=prev;

prev=curr;

curr=next;

start=prev;

void trav_start(){

struct node *p;

p=start;

while(p!=NULL){

printf("%d\t",p->info);

p=p->next;

void trav_end(){
struct node *e;

e=end;

while(e!=NULL){

printf("%d\t",e->info);

e=e->prev;

void main(){

int n,i,data,a;

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

scanf("%d",&n);

for( i=0;i<n;i++){

scanf("%d",&data);

add(data);

// struct node *p;

// p = start;

// while(p != NULL){

// printf("\t\n%d\n",p->info);

// p=p->next;

// }

int x;

printf("\nEnter an operation:\n1.Insert at end\n2.Insert at beginning\n3.Insert at


position\n4.Delete at beginning\n5.Delete at end\n6.Delete at position\n7.Length\
n8.Search\n9.Reverse\n10.trav_end\n");

scanf("%d",&x);

switch(x){

case 1: insert_end();

break;

case 2: insert_beg();

break;

case 3: insert_pos();

break;
case 4: del_beg();

break;

case 5: del_end();

break;

case 6: del_pos();

break;

case 7: del_end1();

break;

case 8: search();

break;

case 9: reverse();

break;

case 10: length();

break;

trav_start();

// trav_end(); }

Output:
PRACTICAL-3
Circular Linked List:
#include <stdio.h>

#include <stdlib.h>

struct node {

int info;

struct node *link;

};

struct node *last = NULL;

void add(int data) {

struct node *temp;

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

temp->info = data;

if (last == NULL) {

temp->link = temp;
last = temp;

} else {

temp->link = last->link;

last->link = temp;

last = temp;

void insert_beg() {

int eib;

struct node *pib;

printf("Enter the element : ");

scanf("%d", &eib);

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

pib->info = eib;

pib->link = last->link;

last->link = pib;

void insert_end() {

int eib;

struct node *pib;

printf("Enter the element : ");

scanf("%d", &eib);

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

pib->info = eib;

pib->link = last->link;

last->link = pib;

last = pib;

void insert_pos() {

int eib, pos, i;

struct node *pib, *p;

printf("Enter the element : ");


scanf("%d", &eib);

printf("Enter position: ");

scanf("%d", &pos);

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

pib->info = eib;

p = last->link;

for (i = 1; i < pos - 1; i++) {

p = p->link;

pib->link = p->link;

p->link = pib;

void del_beg() {

last->link = last->link->link;

void del_end() {

struct node *p;

p = last->link;

while (p->link->link != last->link) {

p = p->link;

p->link = last->link;

last = p;

void del_pos() {

struct node *p;

int pos, i;

printf("Enter position : ");

scanf("%d", &pos);

p = last->link;

for (i = 1; i < pos - 1; i++) {

p = p->link;
}

p->link = p->link->link;

int length() {

if (last == NULL) {

return 0;

int len = 0;

struct node *temp = last->link;

do {

len++;

temp = temp->link;

} while (temp != last->link);

return len;

void search() {

if (last == NULL) {

printf("List is empty\n");

return;

int data, pos = 1, found = 0;

struct node *temp = last->link;

printf("Enter the element to search: ");

scanf("%d", &data);

do {

if (temp->info == data) {

printf("Element %d found at position %d\n", data, pos);

found = 1;

break;

temp = temp->link;

pos++;
} while (temp != last->link);

if (!found) {

printf("Element not found");

void trav_start(){

struct node *p;

p=last->link;

while(1){

printf("%d\t",p->info);

p=p->link;

if(p==last->link)

break;

int main() {

int choice, n, data;

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

scanf("%d", &n);

printf("Enter values: \n");

for (int i = 0; i < n; i++) {

scanf("%d", &data);

add(data);

printf("\nEnter an operation:\n");

printf("1. Insert at end\n");

printf("2. Insert at beginning\n");

printf("3. Insert at position\n");

printf("4. Delete at beginning\n");

printf("5. Delete at end\n");

printf("6. Delete at position\n");

printf("7. Length\n");
printf("8. Search\n");

scanf("%d", &choice);

switch (choice) {

case 1:

insert_end();

break;

case 2:

insert_beg();

break;

case 3:

insert_pos();

break;

case 4:

del_beg();

break;

case 5:

del_end();

break;

case 6:

del_pos();

break;

case 7:

printf("Length = %d\n", length());

break;

case 8:

search();

break;

trav_start();

return 0;

Output:
PRACTICAL-4
Stack using Array:
#include <stdio.h>

#include <stdlib.h>

int top = -1;

#define maxsize 10

int a[maxsize];

void push(){

int n;

if(top == maxsize){

printf("Stack is full");

else{

printf("Enter element to push: ");

scanf("%d",&n);

top = top+1;

a[top] = n;

void pop(){

if(top == -1){

printf("Stack is empty");
}

else{

printf("%d",a[top]);

top = top-1;

void peek(){

if(top == -1){

printf("Stack is empty");

else{

printf("%d",a[top]);

void traverse(){

int i;

if (top==-1){

printf ("Stact is empty");

else{

printf("Traversed elements are: ");

for(i=top;i>=0;i--){

printf("%d",a[i]);

int main() {

int i,choice;

while(1){

printf("\nEnter an operation\n1.push\n2.pop\n3.peek\n4.traverse\n");

printf("Enter your choice: ");

scanf("%d",&choice);
switch(choice){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

traverse();

break;

return 0;

Output:
Push- Peek-

Pop-

Traverse-
PRACTICAL-5
Stack using Linked List:
#include <stdio.h>

#include <stdlib.h>

struct node{

int info;

struct node *link;

};

struct node* top=NULL;

void push(){

struct node*temp;

int data;

scanf("%d",&data);

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

temp->info=data;

if(top==NULL){

temp->link=NULL;

top=temp;

else{

temp->link=top;

top=temp;

void pop(){
if(top == NULL){

printf("Stack is empty");

else{

printf("%d",top->info);

top = top->link;;

void peek(){

if(top==NULL){

printf("stack is empty");

else{

printf("%d",top->info);

void traverse(){

struct node*p;

if(top==NULL){

printf("stack is empty");

else{

p=top;

while(p!=NULL){

printf("%d\t",p->info);

p=p->link;

int main() {

int i,choice;

while(1){
printf("\n");

printf("stack operation \n1.push\n2.pop\n3.peek\n4.traverse\n");

scanf("%d",&choice);

switch(choice){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

traverse();

break;

default:

exit(0);

return 0;}

Output:
Push- Peek-

Pop- Traverse-
PRACTICAL-6
Queue using Array:
#include <stdio.h>

#include <stdlib.h>

#define maxsize 10

int front = -1;

int rear = -1;

int a[maxsize];

void peek(){

if(front==-1 || front>rear){

printf("queue is empty ");

else{

printf("%d",a[front]);

void insert(){

int n;

if(rear==maxsize -1){

printf("Queue is full");

else if (rear== -1 && front ==-1){

scanf("%d",&n);

rear = rear +1;

a[rear]=n;

front = front+1;

else{
scanf("%d",&n);

rear= rear+1;

a[rear]=n;

void delete(){

if (front ==-1 || front>rear){

printf("Queue is empty ");

else if( front == 0 && rear ==0){

printf("%d",a[front]);

front = -1;

rear = -1;

else{

printf("%d",a[front]);

front = front +1;

void traverse(){

if (front==-1 || front>rear){

printf("Queue is empty ");

else{

for (int i = front; i<=rear;i++){

printf("%d",a[i]);

int main() {

int i,choice;

while(1){
printf("\nEnter an operation\n1.peek\n2.insert\n3.delete\n4.traverse\n");

scanf("%d",&choice);

switch(choice){

case 1:

peek();

break;

case 2:

insert();

break;

case 3:

delete();

break;

case 4:

traverse();

break;

return 0;

Output:
Peek- Delete-

Insert- Traverse-
PRACTICAL-7
Queue using Linked List:
#include <stdio.h>

#include <stdlib.h>

struct node{

int info;

struct node* link;

};

struct node* front = NULL, *rear= NULL;

void insert () {

struct node * temp;

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

int data;

scanf("%d",&data);

temp->info = data;

if(front==NULL){

temp->link=NULL;

front= temp;

rear = temp;

else{

temp->link= NULL;

rear->link= temp;

rear = temp;

void delete(){

if (front == NULL){

printf("Queue is empty");

else {
printf("%d",front->info);

front=front->link;

void peek(){

if(front ==NULL){

printf("Queue is empty ");

else {

printf("%d",front->info);

void traverse(){

struct node * ptr;

if(front==NULL){

printf("Queue is empty ");

else {

ptr= front;

while(ptr!=NULL){

printf("%d",ptr->info);

ptr=ptr->link;

int main() {

// Write C code here

int i,choice;

while(1){

printf("\nEnter an operation\n1.peek\n2.insert\n3.delete\n4.traverse\n");

scanf("%d",&choice);

switch(choice){
case 1:

peek();

break;

case 2:

insert();

break;

case 3:

delete();

break;

case 4:

traverse();

break;

return 0;}

Output:
Peek- Delete-

Insert- Traverse-

PRACTICAL-8
Circular Queue:
#include <stdio.h>

#include <conio.h>

#define maxsize 10

int a[maxsize];

int front = -1;

int rear = -1;

void insert(){

int n;

if((rear + 1 ) % maxsize == front ) {

printf(“Queue is full”);

} else if(rear == -1 && front == -1) {

scanf(“%d”, &n);

front = 0;

rear = 0;

A[front] = n;

} else {

scanf(“%d”, &n);

rear = (rear + 1) % maxsize;

A[rear] = n;

void del(){

if(front == -1) {

printf(“Queue is Empty”);

} else if(front == rear) {

printf(“%d”, a[front]);

front = -1;

rear = -1;

} else {

printf(“%d”, a[front]);

front = (front + 1) % maxsize;

}
}

void peek(){

if(front == -1) {

printf(“Queue is Empty”);

} else {

printf(“%d”, a[front]);

void trav(){

if(front == -1)printf(“Queue is Empty”);

else {

int i= front;

while(i!= rear) {

printf(“%d -> “, a[i]);

i = (i + 1) % maxsize;

printf(“%d”, a[i]);

int main(){

int i, choice;

while(1) {

printf(“\nCircular Queue Operations:\n 1. PUSH\n 2. POP\n 3. TRAVERSE\n 4. PEEK\n 5.

EXIT\nEnter your choice: “);

scanf(“%d”, &choice);

switch(choice){

case 1: insert();

break;

case 2: del();

break;

case 3: trav();

break;
case 4: peek();

break;

case 5: exit(0);

break;

default: printf(“Invalid Operation.”);

return 0;

Output:

PRACTICAL-9
Binary Search:
#include <stdio.h>

#include <conio.h>

int main(){
int arr[10], i, low, high, mid, key, found = 0;

printf(“Enter 10 elements of the array: “);

for(i= 0; i< 10; i++){

scanf(“%d”, &arr[i]);

low = 0;

high = 9;

printf(“Enter the element to search: “);

scanf(“%d”, &key);

while(low <= high) {

mid = (high + low)/2;

if(arr[mid] == key) {

found = 1;

printf(“Element has been found at %d index.”, mid);

break;

} else if(arr[mid] > key) {

high = mid – 1;

} else {

low = mid + 1;

if(found == 0) printf(“Element is not found.”);

getch();

return 0;

Output:
PRACTICAL-10
Heap Sort:
#include <stdio.h>

#include <conio.h>

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

void heapify(int a[], int n, int i);

int main(){
int arr[10], i;

printf(“Enter 10 elements of the array: “);

for(I = 0; I < 10; i++){

scanf(“%d”, &arr[i]);

heapsort(arr, 10);

for(i=0; I < 10; i++) {

printf(“%d “, arr[i]);

getch();

return 0;

void heapsort(int a[], int n){

for(int I = n/2 – 1; I >= 0; i--) heapify(a, n, i);

for(int I = n – 1; I > 0; i--){

int temp = a[0];

a[0] = a[i];

a[i] = temp;

heapify(a, I, 0);

void heapify(int a[], int n, int i){

int largest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

if(l < n && a[l] > a[largest]) largest = l;

if(r < n && a[r] > a[largest]) largest = r;

if(largest != i){

int temp = a[i];

a[i] = a[largest];

a[largest] = temp;

heapify(a, n, largest);
}

Output:

PRACTICAL-11
Binary Search Tree:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<malloc.h>

struct node {

int info;

struct node *left;


struct node *right;

};

struct node *root = NULL;

struct node *create(int data){

struct node *temp;

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

temp->left = NULL;

temp->right = NULL;

temp->info = data;

return temp;

struct node *insert(struct node *root, int data){

if(root == NULL) {

return create(data);

if(data < root->info){

root->left = insert(root->left, data);

} else {

root->right = insert(root->right, data);

void inorder(struct node *root){

if(root == NULL) return;

else {

inorder(root->left);

printf(“%d, “, root->info);

inorder(root->right);

void preorder(struct node *root){

if(root == NULL) return;

else {
printf(“%d, “, root->info);

preorder(root->left);

preorder(root->right);

void postorder(struct node *root){

if(root == NULL) return;

else {

preorder(root->left);

preorder(root->right);

printf(“%d, “, root->info);

struct node* search(struct node* root,int key)

if(root==NULL || key==root->info)

return root;

if (key<root->info)

return search(root->left,key);

else

return search(root->right,key);

};

int findmin(struct node *root)

struct node *current = root;

while(current && current->left != NULL)


current = current->left;

return current->info;

struct node *delete(struct node *root, int value)

if(root == NULL)

return root;

if(value < root->info)

root->left = delete(root->left, value);

else if(value > root->info)

root->right = delete(root->right, value);

else

if(root->left == NULL)

struct node *temp = root->right;

free(root);

return temp;

else if(root->right == NULL)

struct node *temp = root->left;

free(root);

return temp;

int minValue = findmin(root->right);

root->info = minValue;

root->right = delete(root->right, minValue);

return root;

int main (){


int I, choice;

while(1) {

printf(“\nBST Operations:\n 1. Insertion\n 2. InOrder Traversing\n 3. PreOrder


Traversing\n 4.

PostOrder Traversing\n 5. Search\n 6. Delete\n 7. EXIT\n Enter your choice: “);

scanf(“%d”, &choice);

switch(choice){

case 1: scanf(“%d”, &i);

root = insert(root, i);

break;

case 2: if(root == NULL) printf(“Tree does not Exist”);

else {

inorder(root); }

break;

case 3: if(root == NULL) printf(“Tree does not Exist”);

else {

preorder(root); }

break;

case 4: if(root == NULL) printf(“Tree does not Exist”);

else {

postorder(root); }

break;

case 5: printf(“Enter the element to search: “);

scanf(“%d”,&key);

if (search(root,key)!=NULL)

printf(“Element %d is Found \n”,key); }

else {

printf(“Element %d is Not Found \n”,key);}

break;

case 6: if (root == NULL) {

printf(“No Tree Found\n”);


} else {

printf(“Enter the element to delete: “);

scanf(“%d”, &del);

if (search(root, del) != NULL) {

Root = delete(root, del);

Printf(“Element %d deleted\n”, del);

} else {

Printf(“Element %d is Not Found\n”, del);

Case 7: Exit(0);

break;

default: printf(“Invalid Operation.”);

return 0;

Output:
PRACTICAL-12
Graph:
#include <stdio.h>

Int V, E;

Void create(int adj[V][V], int e[E][2]) {

For (int I = 0; I < V; i++) {

For (int j = 0; j < V; j++) {

Adj[i][j] = 0;

For (int I = 0; I < E; i++) {

Int x = e[i][0];

Int y = e[i][1];

Adj[x – 1][y – 1] = 1;

Adj[y – 1][x – 1] = 1;

Void print(int adj[V][V]) {

Printf(“\nAdjacency Matrix:\n”);

For (int I = 0; I < V; i++) {

For (int j = 0; j < V; j++) {

Printf(“%d “, adj[i][j]);

Printf(“\n”);

Int main() {

Int I, j;

Printf(“Enter number of vertices: “);

Scanf(“%d”, &V);

Printf(“Enter number of edges: “);

Scanf(“%d”, &E);
Int adj[V][V];

Int e[E][2];

Printf(“Enter the edges (as pairs of vertices, 1-indexed):\n”);

For (I = 0; I < E; i++) {

Printf(“Edge %d: “, I + 1);

Scanf(“%d %d”, &e[i][0], &e[i][1]);

Create(adj, e);

Print(adj);

Return 0;

Output:

You might also like