0% found this document useful (0 votes)
30 views32 pages

Sakshi DS LAB File

Uploaded by

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

Sakshi DS LAB File

Uploaded by

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

Amity University Haryana

ASET 2023-26

Practical File: Data Structure [DS]


Branch Year: 2023-26
Session: 2024-25

Submitted by: Submitted to:


Sakshi Dr. Shivani Sharma
Enrollment no. A50504823068
BCA – 1st Year (A)

S. NO TITLE DATE SIGN

1. Write a program for traversing elements into Linear Array.

2. Write a program for inserting elements into Linear Array.

3. Write a program to delete an element from Linear Array.


4. Write a program to implement selection sort.

5. Write a program to implement bubble sort.

6. Write a program to print the location address of a variable


using pointer.

1
7. Write a program to store the value at the variable itself.

8. Write a program to declare a pointer data type.

9. Write a program for pointer increments and to measure the


scaling factor.
10. Write a program to declare pointers using Array.

11. Write a program to implement Binary Search in Array.

12. Write a program that follows the following functions:


1. Create stack.
2. PUSH operation on stack.
3. POP operation on stack.
13. Write a program that follows the following functions:
1. Create queue.
2. Insert operation on queue.
3. Delete operation on queue.
14. Write a program that follows the following functions:
1. Create dequeue.
2. Insert operation on dequeue.
3. Delete operation on dequeue.
15. Write a program using switch case that follows the
following functions:
1. Create dequeue.
2. Insert operation on dequeue.
3. Delete operation on dequeue.
16. Write a program that follows the following functions:
1. Create Circular queue.
2. Insert operation on Circular queue.
3. Delete operation on Circular queue.

2
17. Write a program that follows the following functions:
1. Create a Singly Linked List.
2. Delete an element from the above Linked List.
3. Display the contents f the above Linked List.

PROGRAM 1 Implementation of traversing.


#include <stdio.h>
int main() {
int a[20],n,i;
printf("Enter the value:\n");
scanf("%d",&n);
printf("Enter %d Elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("traversed array:\n");
for(i=0;i<n;i++)
printf("Element no. is %d and Value is %d\n",i+1,a[i]);
return 0;
}
OUTPUT:

3
PROGRAM 2 Implementation of inserting.
#include <stdio.h>
int main() {
int a[20],m,i,j,k,n;
printf("Enter the number of elements: \n");
scanf("%d",&m);
printf("Enter %d elements\n",m);
for(k=1;k<=m;k++)
scanf("%d",&a[k]);
printf("Enter the new element to be inserted: \n");
scanf("%d",&n);
printf("Where %d is to be inserted: \n",n);
scanf("%d",&j);
i=m;
while(i>=j){
a[i+1]=a[i];
i--;
}
a[j]=n;
4
m = m+1;
printf("Array after insertion is: \n");
for(k=1;k<=m;k++)
printf("%d\n",a[k]);
return 0;
}

OUTPUT:

5
PROGRAM 3 Implementation of deleting.

#include <stdio.h>
int main(){
int a[20],m,i,j,k,n;
printf("Enter the number of elements \n");
scanf("%d",&m);
printf("Enter %d elements \n",m);
for(k=1;k<=m;k++)
scanf("%d",&a[k]);
printf("Enter the position of element to be deleted \n");
scanf("%d",&j);
i = j;
while(i<=m-1){
a[i]=a[i+1];
i++;
}
m=m-1;
printf("Array after deletion is \n");
for(k=1;k<=m;k++)
printf("%d\n",a[k]);
return(0);
}

OUTPUT:

6
PROGRAM 4 Implementation of Selection Sort.
#include<stdio.h>
int main(){
int a[50],n,i,k,t,m,p;
printf("Enter the number of elements \n");
7
scanf("%d", &n);
printf("Enter the array elements \n");
for(i=1;i<=n;++i)
scanf("%d",&a[i]);
for(k=1;k<=n-1; k++){
m=a[k];
p=k;
for(i=k+1;i<=n;++i){
if(m>a[i]){
m=a[i];
p= i;
}
}
if(p!=k){
t=a[k];
a[k]=a[p];
a[p]=t;
}
}
printf("The sorted array is \n");
for(k=1;k<=n;k++){
printf("%d\n",a[k]);
}
return 0;
}

OUTPUT:

8
PROGRAM 5 Implementation of Bubble Sort.
#include<stdio.h>
int main(){
int a[50],n,i,j,t;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the array elements \n");
for(i=1;i<=n;++i)
scanf("%d", &a[i]);

9
for(i=1;i<=n-1;i++){
for(j=1;j<=n-i;++j){
if(a[j]>a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("The sorted array is \n");
for(i=1;i<=n;i++)
printf("%d\n", a[i]);
return 0;
}

OUTPUT:

10
PROGRAM 6 Implementation of Pointers.
a) #include <stdio.h>
int main(){
int alpha=1;
printf ("The value %d is stored at address %d\n", alpha, &alpha);
}
OUTPUT:

11
PROGRAM 7
b) #include <stdio.h>
int main(){
int alpha=1;
printf ("The value %d is stored at address %d\n", alpha, &alpha);
printf ("The value %d is stored at address %d\n",* (&alpha), &alpha);
}
OUTPUT:

12
PROGRAM 8
c) #include <stdio.h>
int main(){
int alpha;
int* beta;
alpha=1;
beta=&alpha;
printf("The value %d is stored at address %d\n", alpha, &alpha);
printf("The value %d is stored at address %d\n", beta, &beta);
printf("The value %d is stored at address %d\n", *beta, beta);
}
OUTPUT:

13
PROGRAM 9
d) #include <stdio.h>
int main(){
printf("Size of int is %d\n",sizeof(int));
printf("Size of float is %d\n",sizeof(float));
printf("Size of char is %d\n",sizeof(char));
printf("Size of long is %d\n",sizeof(long));
printf("Size of double is %d\n",sizeof(double));
}
OUTPUT:

PROGRAM 10
14
e) #include <stdio.h>
int main(){
int a[]={1,2,3,4,5};
int i;
for(i=0;i<5;++i)
printf(" no. is %d and address is %u\n",i,&a[i]);
}

OUTPUT:

PROGRAM 11 Implementation of Binary Search.


#include <stdio.h>
int main(){
int c,f,l,m,n,s,a[100];
printf("Enter number of elements\n");
scanf("%d",&n);

15
printf("Enter %d integers\n", n);
for (c=0;c<n;c++)
scanf("%d",&a[c]);
printf("Enter value to find\n");
scanf("%d",&s);
f=0;
l=n-1;
m=(f+l)/2;
while(f<=l){
if (a[m]<s)
f=m+1;
else if (a[m]==s){
printf("%d found at location %d.\n",s,m+1);
break;
}
else
l=m-1;
m=(f+l)/2;
}
if(f>l)
printf("Not found! %d isn't present in the list.\n", s);
return 0;
}

OUTPUT:

16
PROGRAM 12 Implementation of Stack.
#include<stdio.h>
#define MAX_SIZE 20
int stack[MAX_SIZE],top=NULL;
int main(){
int ch, x;
printf("\nSTACK OPERATIONS");
printf("\n1. push operation");
printf("\n2. pop operation");
printf("\n3. exit");
while(1){

17
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("enter the number to be pushed: ");
scanf("%d", &x);
push(x);
break;
case 2:
x = pop();
if(x != NULL)
printf("\nThe number poped is: %d", x);
else
printf("\n the stack is empty");
break;
case 3:
exit(0);
default:
printf("\ninvalid input");
break;
}
}
}
push(int x){
if(top == MAX_SIZE){
printf("\n overflow");
return;
}
top= top+1;
stack[top]=x;

18
return;
}
int pop(){
int x;
if(top==NULL)
return(NULL);
else{
x=stack[top];
top=top-1;
return(x);
}
}

OUTPUT:

19
PROGRAM 13 Implementation of Queue.
#include <stdio.h>
# define SIZE 100
void enqueue();
void show();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
main()
{
int ch;
while (1)
{
printf("1.Enqueue Operation\n");
printf("2.Display the Queue\n");
printf("3.Exit\n");

20
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
show();
break;
case 3:
return(0);
default:
printf("Incorrect choice \n");
}
}
}

void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)

Front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &insert_item);

21
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}
void show()
{

if (Front == - 1)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}
OUTPUT:

PROGRAM 14 Implementation of Dequeue.


#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
22
void display();
int items[SIZE],f=-1,r=-1;
int main() {
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
display();
deQueue();
display();
return 0;
}
void enQueue(int value) {
if (r==SIZE - 1)
printf("\nQueue is Full!!");
else {
if (f==-1)
f=0;
r++;
items[r] = value;
printf("\nInserted -> %d", value);
}
}
void deQueue() {
if (f==-1)
printf("\nQueue is Empty!!");
else {

23
printf("\nDeleted : %d", items[f]);
f++;
if (f>r)
f=r=-1;
}
}
void display() {
if (r==-1)
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = f; i <= r; i++)
printf("%d ", items[i]);
}
printf("\n");
}
OUTPUT:

24
PROGRAM 15 Implementation of Dequeue using switch case.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 50
void insert();
void delete();
void display();
int a[SIZE],f=-1,r=-1;
int main(){
int choice;
while(1){
printf("insert\n");
printf("delete\n");
printf("display\n");
printf("quit\n");
printf("enter choice\n");

25
scanf("%d",&choice);
switch(choice){
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("wrong choice");
}
}
}
void insert(){
int item;
if(r==SIZE-1)
printf("\nQueue is full");
else{
if(f==-1)
f=0;
printf("insert elements:\n");
scanf("%d",&item);
r++;
a[r]=item;
}

26
}
void delete(){
if(f==-1||f>r){
printf("\nQueue is empty");
return;
}
else{
printf("\ndeleted %d",a[f]);
f++;
}
}
void display(){
int i;
if(f==-1)
printf("\nQueue is empty");
else{
printf("\nQueue elements are\n");
for(i=f;i<=r;i++)
printf("%d",a[i]);
}
printf("\n");
}
OUTPUT:

27
PROGRAM 16 Implementation of Circular Queue.
#include <stdio.h>
#define max 20
int queue[max],f=-1,r=-1;
int main() {
int ch,x;
printf("operations\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.exit\n");
while(1){
printf("enter choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("enter value to insert\n");
scanf("%d",&x);
insert(x);

28
break;
case 2:
x=delete();
if(x!=-1)
printf("deleted value is %d\n",x);
else
printf("queue is empty\n");
break;
case 3:
exit(0);
default:
printf("invalid\n");
break;
}
}
}
insert(int x){
if((f==0&&r==(max-1))||(f==r+1)){
printf("overflow\n");
return;
}
if(f==-1){
f=r=0;
}
else{
if(r==max-1)
r=0;
else
r=+1;
}

29
queue[r]=x;
return;
}
int delete(){
int x;
if(f==-1){
printf("underflow\n");
return(-1);
}
x=queue[f];
if(f==r)
f=r=-1;
else{
if(f==max-1)
f=0;
else
f=f+1;
}
return(x);
}
OUTPUT:

PROGRAM 17 Implementation of Linked List.


#include <stdio.h>
30
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void printLinkedlist(struct node *p) {
while (p != NULL) {
printf("%d ", p->value);
p = p->next;
}
}
int main() {
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
one->value = 1;
two->value = 2;
three->value = 3;
one->next = two;
two->next = three;
three->next = NULL;
head = one;
printLinkedlist(head);
}
OUTPUT:

31
32

You might also like