krishG.DS
krishG.DS
krishG.DS
#include<stdio.h>
#include<stdlib.h>
#define size 10
int top=-1,array[size];
void push();
void pop();
void peek();
int main(){
int choice;
while(1){
printf("\n\n perfom operation on the stack: ");
printf("\n1.push the element\n2.pop the element\
n3.show(peek operation)\n4.End");
printf("\n\n enter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek;
break;
case 4:
exit(0);
defualt:
printf("\n invalid choice||");
}
}
}
void push(){
int x;
if(top==size-1){
printf("\n overflow||");
}else{
printf("\n enter the element to be added onto the stack: ");
scanf("%d",&x);
top=top+1;
array[top]=x;
}
}
void pop(){
if(top==-1){
printf("\nUnderflow||");
}else{
printf("\n enter element to be remove onto the stack:
%d",array[top]);
top=top-1;
}
}
void peek(){
if(top==-1){
printf("\n underflow");
}else{
printf("\n element present in the stack: \n");
for(int i=0;i<=top;i++)
printf("%d\n",array[i]);
}
}
Practical-13
Quick sort:
#include<stdio.h>
void swap(int*a,int*b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int arr[],int low,int high){
int pivot=arr[low];
int start=low;
int end=high;
while(start<end){
while(arr[start]<=pivot){
start ++;
}
while(arr[end]>=pivot){
end --;
}
if(start<end){
swap(&arr[start],&arr[end]);
}
}
swap(&arr[low],&arr[end]);
return end;
}
void quicksort(int arr[],int low,int high){
if(low<high){
int pi=partition(arr,low,high);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
}
}
int main(){
int arr[10],n,i;
printf("enter the array size: ");
scanf("%d",&n);
printf("\n enter array elements>>\n");
for(i=0;i<n;i++){
printf("array[%d]:>>",i);
scanf("%d",&arr[i]);
}
quicksort(arr,0,n-1);
printf("\n sorted array: \n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
struct node* head=NULL;
struct node* tail=NULL;
int size=0;
void create(){
int element;
struct node* newnode=(struct node*)malloc(sizeof(struct
node));
printf("enter element: ");
scanf("%d",&element);
newnode->data=element;
newnode->next=NULL;
if(head==NULL){
head=tail=newnode;
size++;
}else{
tail->next=newnode;
tail=newnode;
size++;
}
}
void insertatpos(){
int pos,data,i=1;
printf("enter position: ");
scanf("%d",&pos);
struct node* newnode=(struct node*)malloc(sizeof(struct
node));
printf("enter the element: ");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
if(pos<1 || pos>size+1)
printf("invalid\n");
else if(pos==1){
newnode->next=head;
head=newnode;
size++;
}else{
struct node* temp=head;
while(i<pos+1){
temp=temp->next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
size++;
}
}
void display(){
struct node* temp;
temp=head;
if(temp==NULL){
printf("list is empty\n");
}else{
while(temp!=NULL){
printf("%d-->",temp->data);
temp=temp->next;
}
printf("\n");
}
}
void deletefrompos(){
struct node* temp;
int pos;
printf("enter position to delete: ");
scanf("%d",&pos);
if(pos>size){
printf("invalid position\n");
}else if(pos==1){
temp=head;
head=temp->next;
printf("deleted node is:%d\n",temp->data);
free(temp);
size--;
}else{
struct node* p=head,*temp;
int i=1;
while(i<pos-1){
p=p->next;
i++;
}
temp=p->next;
p->next=temp->next;
printf("deleted node is: %d\n",temp->data);
free(temp);
size--;
}
}
int main(){
int ch;
while(1){
printf("single linked list operation: \n");
printf("1.create a list\n");
printf("2.insert at specific posititon\n");
printf("3.display\n");
printf("4.delete from specific position\n");
printf("5.exit\n");
printf("enter your choice: ");
printf("%d",&ch);
switch(ch){
case 1: create();
break;
case 2: insertatpos();
break;
case 3: display();
break;
case 4: deletefrompos();
break;
case 5: exit(0);
default: printf("invalid choice");
}
}
return 0;
}