Sakshi DS LAB File
Sakshi DS LAB File
ASET 2023-26
1
7. Write a program to store the value at the variable itself.
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.
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=α
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:
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:
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:
31
32