0% found this document useful (0 votes)
28 views8 pages

Dsu SP

The document contains C code for implementing bubble sort, insertion sort and operations on queue and stack using linear data structures. It also includes code for creating, traversing and performing operations on linked lists like insertion, deletion and search.

Uploaded by

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

Dsu SP

The document contains C code for implementing bubble sort, insertion sort and operations on queue and stack using linear data structures. It also includes code for creating, traversing and performing operations on linked lists like insertion, deletion and search.

Uploaded by

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

//# BUBBLE SORT SP

#include <stdio.h>
#define MAX 10
int main() {
// Write C code here
int a[MAX],n,i,pass,temp;
printf("Enter the number of elements <%d",MAX);
scanf("%d",&n);
printf("Enetr %d elements",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(pass=1;pass<n;pass++)
{
for(i=0;i<n-pass;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("Elements sorted list is ");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);

return 0;
}
===================================================================================
=============================

// Online C compiler to run C program online


//# BUBBLE SORT SP
#include <stdio.h>
#define MAX 10
int main() {
// Write C code here
int a[MAX],n,i,j,k,temp;
printf("Enter the number of elements <%d",MAX);
scanf("%d",&n);
printf("Enetr %d elements",n);
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)

{
for(j=0;j<i;j++)
{
if(a[i]<a[j])
{
temp=a[j];
for(k=j-1;k>=i;k--)
a[k+1]=a[k];
a[j]=temp;
break;

}
}
}
printf("Elements sorted list is ");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);

return 0;
}

***********************************************************************************
*****************************************
***operations on queue*************

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

typedef struct
{
int queue[MAX];
int front,rear;
}Queue;
main()
{
int ch;
Queue q;
q.front=-1;
q.rear=-1;

while(1)
{
printf("\n Menu for \n***********Linear Queue*********\n 1.Insert 2.Delete
3.display 4.exit ");
scanf("%d",&ch);
switch(ch)
{
case 1:enqueue(&q);
break;

case 2:dequeue(&q);
break;

case 3:display(&q);
break;

case 4:
exit(0);
}
}
}
enqueue(Queue *q)
{
int value;
if(q->rear==MAX-1)
{
printf("Queue is full");

}
else
{
printf("\n enter value to insert ");
scanf("%d",&value);

q->queue[++q->rear]=value;
printf("%d is inserted successfully",value );
if(q->front==-1)
q->front=0;
}
if(q->front==-1) q->front=0;
}
dequeue(Queue *q)
{
if(q->front==-1)
{
printf("\n queue is empty");

}
else
{
printf("%d is deleted from the queue",q->queue[q->front++]);

}
if(q->front=q->rear+1)
q->front=q->rear=-1;
}
display(Queue *q)
{
int i;
if(q->front==-1)
{
printf("\n queue is empty");
}
else
{

1
printf("\n*******Elements in the linear queue are********* \n");
for(i=q->front;i<=q->rear;i++)
{
printf(" %d",q->queue[i]);
}
}
}
***********************************************************************************
******************************************************

#include<stdio.h>
#define MAX 5
#include<stdlib.h>

typedef struct
{
int stack[MAX];
int top;
}Stk;
main()
{
Stk s;
int ch;
s.top=-1;
while(1)
{
printf("\n ******************OPERATION ON STACK*******************\n");
printf("\n 1.Push 2.Pop 3.Display 4.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1: push(&s);
break;
case 2: pop(&s);
break;
case 3: display(&s);
break;
case 4: printf("\n Exiting from the stack operation \n");
exit(0);
}
}
return 0;
}

push(Stk *s)
{
int item;
if(s->top==MAX-1)
{
printf("Stack is full");
return;

}
else
{
printf("\n Enter item to insert :\n");
scanf("%d",&item);
s->stack[++s->top]=item;
printf("\n %d pushed successfully into the stack",item);

}
}
pop(Stk *s)
{
if(s->top==-1)
{
printf("\n Stack is empty cannot perform pop operation ");
return;
}
else
{
printf("%d is popped from the stack",s->stack[s->top--]);
}
}
display(Stk *s)
{
int i;
if(s->top==-1)
{
printf("\n Stack is empty ");
return;
}
else
{
printf("\n The stack elements are>>>>*******\n");
for(i=0;i<=s->top;i++)
{
printf("%d ",s->stack[i]);

}
}
}

***********************************************************************************
***************************************************

#include<stdio.h>
#include<stdlib.h>

typedef struct node


{
int data;
struct node *next;
}Node;
main()
{
int item,ch;
Node *head,*temp,*add;
head=NULL;
printf("\n Creating initial linked list (-999 to stop)");
printf("\n Enter items :");
while(1)
{

scanf("%d",&item);
if(item==-999) break;

add=(Node*)malloc(sizeof(Node));
add->data=item;
add->next=NULL;
if(head==NULL)
{
head=add;

}
else

temp->next=add;
temp=add;
}

while(1)
{
printf("\n **********Operation on Linear Linked List**************\n");
printf("\n 1.Insert 2.Delete 3.Search 4.Display 5.Exit\n");
scanf("%d",&ch);

switch(ch)
{
case 1: insert(&head);
break;
case 2: del(&head);
break;
case 3: search(&head);
break;
case 4: display(&head);
break;
case 5: printf("\n Exiting from the linked list operations");
exit(0);
}

}
}

insert(Node **head)
{
int loc,i,count,item;
char choice;
Node *temp=*head,*add;
printf("\n Enter item to insert:");
scanf("%d",&item);
add=(Node*)malloc(sizeof(Node));
add->data=item;
add->next=NULL;

printf("\n a.Insert at beginning b.Insert at end c.Insert at specific


Location ");
scanf(" %c",&choice);

switch(choice)
{
case 'a':
add->next=*head;
*head=add;
break;
case 'b': while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=add;
printf("item insertd to the end ");
break;
case 'c': printf("\n enter the loc");
scanf("%d",&loc);
for(i=1;i<loc-1;i++)
{
temp=temp->next;
}
add->next=temp->next;
temp->next=add;
break;

}
del(Node **head)
{
int i,item;
Node *prev,*temp=*head;
if(*head==NULL)
{
printf("List is empty");
return;
}
printf("\n Enter item to delete ");
scanf("%d",&item);
if((*head)->data==item)
{
*head=(*head)->next;

}
else
{
prev=*head;
temp=(*head)->next;
while((temp->data!=item)&&(temp!=NULL))
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
{
printf("item is not in the list");
return;

}
else
{
prev->next=temp->next;
}
}
free(temp);

}
search(Node **head)
{
int item,loc;
Node *temp=*head;
if((*head)->data==NULL)
{
printf("List is empty");
}
else
{
printf("\n Enter item to search :");
scanf("%d",&item);
for(temp=*head,loc=1;temp->next!=NULL;temp=temp->next,loc++)
{
if(temp->data==item)
{
printf("%d found at %d",item,loc);
}
}

display(Node **head)
{
int count;
Node *temp=*head;
if((*head)==NULL)
{
printf("List is empty");
return;
}
else
{
printf("Printing linked list items : \n List>>> ");
for(temp=*head,count=0;temp!=NULL;temp=temp->next,count++)
{
printf(" %d->",temp->data);

}
printf("the count of node is %d ",count);
}

You might also like