//WAP to find and print the smallest element in an array
#include<stdio.h>
int main()
int n,i,small,pos;
printf("Enter the size of array:");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of array:\n");
for(i=0;i<n;i++)
printf("arr[%d]:",i);
scanf("%d",&arr[i]);
small=arr[0];
pos=0;
for(i=1;i<n;i++)
if(arr[i]<small)
small=arr[i];
pos=i;
printf("The smallest element is %d and the position is %d",small,pos);
return 0;
}
//WAP to implement a singly linked list using all operation.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node*next;
};
struct node *head;
void beg_insert();
void last_insert();
void random_insert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
int main()
int choice;
while(1)
printf("\n----------------Main Menu----------------");
printf("\n1. Insert in beginning");
printf("\n2. Insert at last");
printf("\n3. Insert at any random location");
printf("\n4. Delete from beginning");
printf("\n5. Delete from last");
printf("\n6. Delete node from after specified location");
printf("\n7. Search for an element.\n8. Display.\n9. Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
case 1:
beg_insert();
break;
case 2:
last_insert();
break;
case 3:
random_insert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("\nInvalid choice, please try again");
return 0;
void beg_insert()
struct node*ptr;
int item;
ptr=(struct node*)malloc(sizeof(struct node*));
if(ptr==NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value:");
scanf("%d",&item);
ptr->data=item;
ptr->next=head;
head=ptr;
printf("\nNode Inserted");
void last_insert()
struct node*ptr,*temp;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
printf("\nOVERFLOW");
else
{
printf("\nEnter value:");
scanf("%d",&item);
ptr->data=item;
if(head==NULL)
ptr->next=NULL;
head=ptr;
printf("\nNode Inserted");
else
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=ptr;
ptr->next=NULL;
printf("\nNode Inserted");
void random_insert()
int i,loc,item;
struct node*ptr,*temp;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value:");
scanf("%d",&item);
ptr->data=item;
printf("\nEnter the location you want to insert:");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
temp=temp->next;
if(temp==NULL)
printf("\nCan't insert");
return;
ptr->next=temp->next;
temp->next=ptr;
printf("\nNode Inserted");
void begin_delete()
struct node*ptr;
if(head==NULL)
printf("\nList is empty");
else
ptr=head;
head=ptr->next;
free(ptr);
printf("\nNode deleted from the beginning");
}
void last_delete()
struct node*ptr,*ptr1;
if(head==NULL)
printf("\nList is empty");
else if(head->next==NULL)
head=NULL;
free(head);
printf("\nOnly node of the list deleted");
else
ptr=head;
while(ptr->next!=NULL)
ptr1=ptr;
ptr=ptr->next;
ptr1->next=NULL;
free(ptr);
printf("\nDeleted Node from the last");
void random_delete()
struct node*ptr,*ptr1;
int loc,i;
printf("\nEnter the location you want to deleted:");
scanf("%d",&loc);
ptr=head;
for(i=1;i<loc;i++)
ptr1=ptr;
ptr=ptr->next;
if(ptr==NULL)
printf("\nCan't delete");
return;
ptr1->next=ptr->next;
free(ptr);
printf("\nDeleted node %d",loc);
void search()
struct node*ptr;
int item,i=0,flag;
ptr=head;
if(ptr==NULL)
printf("\nEmpty list");
else
printf("\nEnter the item you want to search:");
scanf("%d",&item);
while(ptr!=NULL)
flag=0;
if(ptr->data==item)
printf("\nItem found at location %d",i+1);
flag=0;
else
flag=1;
i++;
ptr=ptr->next;
if(flag==1)
printf("\nItem not found");
void display()
struct node*ptr;
ptr=head;
if(ptr==NULL)
printf("\nNothing to print");
else
while(ptr!=NULL)
printf("\n%d",ptr->data);
ptr=ptr->next;
}
//WAP to insert an element in the stack and perform all operation.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node* next;
};
struct node* newnode(int data)
struct node* node=(struct node*)malloc(sizeof(struct node));
node->data=data;
node->next=NULL;
return node;
void push(struct node** head_ref, int new_data)
struct node* new_node=newnode(new_data);
new_node->next=(*head_ref);
(*head_ref)=new_node;
printf("%d pushed to stack\n",new_data);
int pop(struct node** head_ref)
if(*head_ref==NULL)
printf("Stack is empty\n");
return -1;
struct node* temp=*head_ref;
int popped=temp->data;
(*head_ref)=temp->next;
free(temp);
return popped;
int peek(struct node* head)
if(head==NULL)
printf("\nThe stack is empty");
return-1;
return head->data;
int isEmpty(struct node* head)
return head==NULL;
void printList(struct node* node)
if(node==NULL)
printf("Stack is empty\n");
return;
printf("Stack:");
while(node!=NULL)
printf("%d",node->data);
node=node->next;
printf("\n");
}
int main()
struct node* head=NULL;
int choice,data;
while(1)
printf("\n----------------Stack Operations----------");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Displayed Stack");
printf("\n4. Peek data");
printf("\n5. Check if stack is empty");
printf("\n6. Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
case 1:
printf("Enter data to push onto stack:");
scanf("%d",&data);
push(&head,data);
break;
case 2:
data=pop(&head);
if(data!=-1)
printf("Popped element:%d\n",data);
break;
case 3:
printList(head);
break;
case 4:
data=peek(head);
if(data!=-1)
printf("\nTop element is:%d",data);
break;
case 5:
if(isEmpty(head))
printf("\nThe stack is empty");
else
printf("\nThe stack is not empty");
break;
case 6:
printf("Exiting program");
exit(0);
default:
printf("Invalid choice, please try again");
return 0;
}
/*In a small company there are 5 salesman. Each salesman is suppose to sell 3 product.
WAP using 2D array to print
1. The total sales by each salesman.
2. Total sales of each item.
1 2 3 item
salesman
1 261
2 131
3 623
4 312
5 3 3 6*/
#include<stdio.h>
int main()
int arr[5][3],i,j,sales=0,ttlsales=0;
printf("Enter the element:\n");
for(i=0;i<5;i++)
for(j=0;j<3;j++)
scanf("%d",&arr[i][j]);
for(i=0;i<5;i++)
for(j=0;j<3;j++)
sales=sales+arr[i][j];
}
printf("Total values of salesman %d=%d\n",i+1,sales);
sales=0;
for(i=0;i<3;i++)
for(j=0;j<5;j++)
ttlsales=ttlsales+arr[j][i];
printf("Total salesman of item %d=%d\n",i+1,ttlsales);
ttlsales=0;