Professional Documents
Culture Documents
Name-Pandey Kartikey Kaushal ROLL NO-1904093 SEC - E.T.C - 2 Dsa Lab Assigment
Name-Pandey Kartikey Kaushal ROLL NO-1904093 SEC - E.T.C - 2 Dsa Lab Assigment
Name-Pandey Kartikey Kaushal ROLL NO-1904093 SEC - E.T.C - 2 Dsa Lab Assigment
ROLL NO-1904093
SEC- E.T.C- 2
Contents
SERIAL NO PAGE NUMBER
1. SECTION 1 2-12
a. Arrays
b. Dynamic array/ insert delete
2. Section 2 12-26
A. Singly Linked list
B. Polynomial add & Multipy
C. Doubly Linked List
3. Section 3
A. Stack And Ques 26- 32
SECTION 1
Output:
Q2. WAP to find out the second smallest and second
largest element stored in a dynamic array.
ANS -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr,large=0,small=0,smax=0,smin=0,i,n;
printf("Enter the size of the dynamic array");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
large=0;
small=*(arr+0);
smax=0;
smin=*(arr+0);
for(i=0;i<n;i++)
{
if(*(arr+i)>large)
large=*(arr+i);
if(*(arr+i)<small)
small=*(arr+i);
}
for(i=0;i<n;i++)
{
if((*(arr+i)>smax)&&(*(arr+i)!=large))
smax=*(arr+i);
if((*(arr+i)<smin)&&(*(arr+i)!=small))
smin=*(arr+i);
}
printf(" The Second largest element in the array is:- %d \n The Second minimal element array is :- %d "
,smax,smin);
return 0;
}
Output:
ANS -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr,i,n,count=0;
printf("Enter the size of the dynamic array");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
int left = 0, right = n-1,temp=0;
while (left < right)
{
while (arr[left]%2 == 0 && left < right)
left++;
while (arr[right]%2 == 1 && left < right)
right--;
if (left < right)
{
temp=arr[left];
arr[left]=arr[right];
arr[right]=temp;
left++;
right--;
} for(i=0;i<n;i++)
{
printf("%d ",*(arr+i));
}
return 0;
}
OUTPUT:
ANS -
#include <stdio.h>
#include<stdlib.h>
int main()
{
int *arr,n,x,i,count=0,a,b,next,j;
printf("enter the number of elements:");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
printf("enter elements");
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
for(i=0;i<n;i++)
{
next=-1;
for(j=i+1;j<n;j++)
{
if(*(arr+i)<*(arr+j))
{
next=*(arr+j);
break;
}
}
printf("%d %d \n",*(arr+i),next);
}
return 0;
}
Output:
ANS -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr,*brr,i,n,k;
printf("Enter the size of the dynamic array");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
brr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
for(i=0;i<n;i++)
{
if(i==0)
*(brr+i)=*(arr+(n-1)) * *(arr+(i+1));
else if(i==(n-1))
*(brr+i)=*(arr+(i-1)) * *(arr+0);
else
*(brr+i)=*(arr+(i-1)) * *(arr+(i+1));
}
for(i=0;i<n;i++)
{
printf("%d",*(brr+i));
}
return 0;
}
OUTPUT:
Q6. WAP to find the largest number and counts the
occurrence of the largest number in a dynamic array
of n integers using a single loop.
ANS-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr,i,n,max,rep,occ=0;
printf("Enter the size of the dynamic array");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
max=arr[0];
for(i=0;i<n;i++)
{
if(max<*(arr+i))
{
occ=0;
max=*(arr+i);
rep=max;
}
if(rep==*(arr+i))
occ++;
}
printf(" The largest number in the array is %d and it occured %d times in the array ",max,occ);
return 0;
}
Output:
Q7.You are given an array of 0s and 1s in random
order. Segregate 0s on left side and 1s on right side
of the array. Traverse array only once.
ANS-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *arr,i,n,count=0;
printf("Enter the size of the dynamic array");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",arr+i);
}
for(i=0;i<n;i++)
{
if(*(arr+i)==0)
count++;
}
for(i=0;i<count;i++)
*(arr+i)=0;
for(i=count;i<n;i++)
*(arr+i)=1;
for(i=0;i<n;i++)
printf("%d ",*(arr+i));
return 0;}
Output:
Q8. WAP to swap all the elements in the 1st column
with all the corresponding elements in the last
column, and 2nd column with the second last
column and 3rd with 3rd last etc. of a 2-D dynamic
array. Display the matrix.
ANS-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r ,c;
printf(" enter the value of rows");
scanf("%d",&r);
printf(" enter the value of columns");
scanf("%d",&c);
int left=0,right=(r-1);
int arr[r][c];
int i, j, count = 0,temp;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("The Matrix is :-");
printf("\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++) {
printf(" %d\t",arr[i][j]);
}
printf("\n");
}
//Algortithm
for(j=0;j<c;j++)
{
if(left<right)
for(i=0;i<r;i++)
{
int t=arr[i][j];
arr[i][j]=arr[i][right];
arr[i][right]=t;
}
left++;
right--;
}
printf("The Swaped matrix is :- ");
printf("\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf(" %d\t",arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:-
Output:
Section 2
Output:-
// Node of the doubly linked list
typedef struct node {
int data;
struct node *prev, *next;
} Node;
// function to insert a node at the beginning
// of the Doubly Linked List
void push(Node** head_ref, int new_data)
{
// allocate node
Node* new_node = (Node*)malloc(sizeof(Node));
// put in the data
new_node->data = new_data;
// since we are adding at the beginning,
// prev is always NULL
new_node->prev = NULL;
// link the old list off the new node
new_node->next = (*head_ref);
// change prev of head node to new node
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to delete a node in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del --> pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
// base case
if (*head_ref == NULL || del == NULL)
return;
// If node to be deleted is head node
if (*head_ref == del)
*head_ref = del->next;
// Change next only if node to be
// deleted is NOT the last node
if (del->next != NULL)
del->next->prev = del->prev;
// Change prev only if node to be
// deleted is NOT the first node
if (del->prev != NULL)
del->prev->next = del->next;
// Finally, free the memory occupied by del
free(del);
return;
}
// function to delete all the even nodes
// from the doubly linked list
void deleteEvenNodes(Node** head_ref)
{
Node* ptr = *head_ref;
Node* next;
while (ptr != NULL) {
next = ptr->next;
// if true, delete node 'ptr'
if (ptr->data % 2 == 0)
deleteNode(head_ref, ptr);
ptr = next;
}
}
// function to print nodes in a
// given doubly linked list
void printList(Node* head)
{
while (head != NULL) {
printf("%d-->",head->data);
head = head->next;
}
}
// Driver program
int main()
{
// start with the empty list
Node* head = NULL;
// create the doubly linked list
// 15 <-> 16 <-> 7 <-> 6 <-> 17
push(&head, 17);
push(&head, 6);
push(&head, 7);
push(&head, 16);
push(&head, 15);
printf("Original List: ");
printList(head);
deleteEvenNodes(&head);
printf("\nModified List: ");
printList(head);
return 0;
}
Output:-
Q3 Wap for Linked List :-
1) To insert an element at beg/end/anyposition
2) To delete an element at beg/end/particular element
3) To count the number of element
4) To search a particular element
ANS-
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
void create(){
struct node *ptr,*temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("no space");
exit(0);
}
printf("nEnter the data value for the node:t");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void insert_beg(){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("nOut of Memory Space:n");
return;
}
printf("nEnter the data value for the node:t" );
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=temp;
}
else{
temp->next=head;
head=temp;
}
}
void insert_end(){
struct node *new_node;
int num;
new_node=(struct node *)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("nOut of Memory Space:n");
return;
}
printf("Enter the data:-");
scanf("%d",&num);
new_node->data=num;
struct node *ptr=head;
if(head==NULL){
head=new_node;
}
else{
while(ptr!=NULL){
ptr=ptr->next;
}
new_node->next=ptr->next;
free(ptr);
}
}
void insert_pos(){
struct node *new_node;
int pos,i,num;
if(head==NULL)
{
printf("List is empty!!");
return;
}
new_node=(struct node*)malloc(sizeof(struct node));
printf("Enter the positon you want to enter the node");
scanf("%d",&pos);
printf("Enter the value in node:");
scanf("%d",&num);
new_node->data=num;
struct node *ptr=head;
if(pos==1)
{
new_node->next=head;
head=new_node;
return;
}
for( i=1; i<(pos-1);i++)
{
if(ptr->next==NULL)
{
printf("There are less elements!!");
return;
}
ptr=ptr->next;
}
new_node->next=ptr->next;
ptr->next=new_node;
}
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("nList is Empty:n");
return;
}
else
{
ptr=head;
head=head->next ;
printf("nThe deleted element is :%dt",ptr->data);
free(ptr);
}
}
void delete_end()
{
struct node *temp,*ptr;
if(head==NULL)
{
printf("nList is Empty:");
exit(0);
}
else if(head->next ==NULL)
{
ptr=head;
head=NULL;
printf("nThe deleted element is:%dt",ptr->data);
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("nThe deleted element is:%dt",ptr->data);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(head==NULL)
{
printf("nThe List is Empty:n");
exit(0);
}
else
{
printf("nEnter the position of the node to be deleted:t");
scanf("%d",&pos);
if(pos==0)
{
ptr=head;
head=head->next ;
printf("nThe deleted element is:%dt",ptr->data );
free(ptr);
}
else
{
ptr=head;
for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
if(ptr==NULL)
{
printf("nPosition not Found:n");
return;
}
}
temp->next =ptr->next ;
printf("nThe deleted element is:%dt",ptr->data );
free(ptr);
}
}
}
void del_key(){
int k;
printf("the number to be deleted");
scanf("%d",&k);
struct node *temp,*prev;
prev=NULL;
if(head==NULL){
printf("List is Empty");
return;
}
temp=head;
if(temp!=NULL && temp->data==k){
temp=temp->next;
}
while(temp !=NULL && temp->data !=k){
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
}
void total_nod(){
struct node *temp;
temp=head;
int ctr=0;
while(temp!=NULL){
temp=temp->next;
ctr++;
}
printf("The number of nodes in the list is %d ",ctr);
}
void display()
{
struct node *ptr;
if(head==NULL)
{
printf("nList is empty:n");
return;
}
else
{
ptr=head;
printf("nThe List elements are:n");
while(ptr!=NULL)
{
printf("%d----->",ptr->data );
ptr=ptr->next ;
}
}
}
void search(){
int n,k=1;
printf("Enter the element to be searched :-");
scanf("%d",&n);
struct node *temp;
temp=head;
while(temp!=NULL){
temp=temp->next;
if(temp->data==n){
printf("element is found");
k=0;
break;
}
}
if(k==1){
printf("Element not found");
}
}
int main()
{
int choice;
while(1){
printf("n MENU \n");
printf("n 1.Create \n");
printf("n 2.Display \n");
printf("n 3.Insert at the beginning \n");
printf("n 4.Insert at the end \n");
printf("n 5.Insert at specified position \n");
printf("n 6.Delete from beginning \n");
printf("n 7.Delete from the end \n");
printf("n 8.Delete from specified position \n");
printf("n 9.delete particular element \n");
printf("n 10.calculate total number of nodes \n");
printf("n 11.search an element in linked list \n");
printf("n 12. exit(0) \n");
printf("n--------------------------------------n");
printf("Enter your choice:- ");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_beg();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
del_key();
break;
case 10: total_nod();
break;
case 11:
search();
break;
case 12:
exit(0);
break;
default:
printf("n Wrong Choice:n");
break;
}
}
return 0;
free(head);
//free(struct node);
}
Output:-
Section 3
Q1. Implement Stack using arrays:-
ANS-
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
Output:-
struct node
{
int data;
struct node *next;
};
typedef struct node node;
node *top;
void initialize()
{
top = NULL;
}
void push(int value)
{
node *tmp;
tmp = malloc(sizeof(node));
tmp -> data = value;
tmp -> next = top;
top = tmp;
}
int pop()
{
node *tmp;
int n;
tmp = top;
n = tmp->data;
top = top->next;
free(tmp);
return n;
}
int Top()
{
return top->data;
}
int isempty()
{
return top==NULL;
}
void display(node *head)
{
if(head == NULL)
{
printf("NULL\n");
}
else
{
printf("%d\n", head -> data);
display(head->next);
}
}
int main()
{
initialize();
push(10);
push(20);
push(30);
printf("The top is %d\n",Top());
pop();
printf("The top after pop is %d\n",Top());
display(top);
return 0;
}
Output:-
Q> Implement Priorty Que using linked List:-
ANS-
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
int prr;
struct Node *next;
}node;
node *new_node (int a ,int b){
node *temp=(node*)malloc(sizeof(node));
temp->data=a;
temp->prr=b;
temp->next=NULL;
return temp;
}
void enque(node** head){
int data,prio;
printf("ENter the data for the node:- ");
scanf("%d",&data);
printf("Enter the priority for the node");
scanf("%d",&prio);
node *temp=new_node(data,prio);
node *start= (*head);
if((*head)->prr>prio){
temp->next=(*head);
(*head)=temp;
}
else{
while(start->next!=NULL && start->next->prr<prio){
start=start->next;
}
temp->next=start->next;
start->next=temp;
}
}
void deque(node** head){
node *temp=(*head);
(*head)=(*head)->next;
}
void display(node** head){
node *temp=(*head);
while(temp!=NULL){
printf("%d---->",temp->data);
temp=temp->next;
}
printf("\n");
}
int main(){
int a,pr,ch;
printf("Enter data and priority:- ");
scanf("%d %d",&a,&pr);
node *new=new_node(a,pr);
while(1){
printf("-------MENU---------\n");
printf("1. ENQUE \n");
printf("2 DEQUE \n");
printf("3 Display \n");
printf("4.exit \n");
printf("Enter your choice :-");
scanf("%d",&ch);
switch (ch)
{
case 1:
enque(&new);
break;
case 2:
deque(&new);
break;
case 3:
display(&new);
break;
case 4:
exit(0);
break;
default:
printf("Wrong choice");
break;
}
}
return 0;
}
Output;-