Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
#include<conio.h>
int a[100],n,i,r,l;.
void sort(int b[],int left,int right)
{
int i,j,p,temp,finished,k,n;
if(right>left)
{
i=left;
j=right;
p=b[left];
finished=0;
while(!finished)
{
do
{
++i;
while((b[i]<=p&&(i<=right));
while((b[j]>=p)&&(j>left))
{
--j;
}
if(j<i)
finished=1;
else
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
temp=b[left];
b[left]=b[j];
b[j]=temp;
sort(b,left,j-1);
sort(b,i,right);
}
}
void int()
{
i=0;
r=n-1;
}
void main()
{
clrscr();
printf(“quick sort output\n”);
printf(“enter the number of elements to be sort:\n”);
scanf(“%d”,&n);
printf(“\n”);
printf(“enter the element to be sort:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
init()
sort(a,l,r);
printf(“the sort elements are \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
OUTPUT FOR QUICK SORT
10
60
50
40
30
10 30 40 50 60
/* HEAP SORT */
#include<stdio.h>
#include<conio.h>
#define size 10
int a[size];
void main()
{
int i,n,val;
int get_data();
void heap(int,int);
void sort(int);
clrscr();
printf(“\n\n output”);
printf(“\n\t\t program for heap sort”);
n=get_data();
for(p=1;i<n;i++)
{
val=a[i];
heap(i,val);
}
printf(“\n heap is created as:”);
for(i=0;i<n;i++)
printf(“\n%d”,a[i]);
sort[n];
printf(“\n after sorting by heap sort”);
for(i=0;i<n;i++)
printf(“\n%d”,a[i]);
getch();
}
int get_data()
{
int i,n;
printf(“\n How many elements you want to sort?”);
scanf(“%d”,&n);
if(n>size)
{
printf(“\n more elements then array size:”);
getch();
return;
}
printf(“\n enter the elements”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
return n;
}
void heap(int i,int val)
{
int s,parent;
s=I;
parent=(s-1)/2;
while(s>0&&a[parent]<val)
{
a[s]>a[parent];
s=parent;
parent=(parent-1)/2;
}
a[s]=val;
}
void sort(int n)
{
int i,node,parent,pos;
for(i=n-1;i>0;i--)
{
pos=a[i];
a[i]=a[0];
parent=0;
if(i==1)
node=-1;
else
node=1;
if(i>2&&a[2]>a[1])
node=2;
while(node>0&&pos<a[node])
{
a[parent]=a[node];
parent=node;
node=2*parent+1;
if(node+1<=i-1&&a[node]<a[node]+0)
node++;
if(node>i-1)
node=-1;
}
a[parent]=pos;
}
}
OUT PUT TO HEAP SORT
10
30
43
20
56
23
56
43
30
10
20
23
10
20
23
30
43
56
//QUEUE USING ARRAYS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert();
void deletion();
void create();
void display();
int a[100],n,front,rear,c,x;
void main()
{
clrscr();
do
{
printf(“\n------------------------“);
printf(“\n main menu”);
printf(“\n 1.creation”);
printf(“\n 2.insertion”);
printf(“\n 3.deletion”);
printf(“\n 4.display”);
printf(“\ 5.exit”);
printf(“\--------------------------“);
printf(“\enter your choice(1/2/3/4/5)\n”);
scanf(“%d”,&c);
switch(c)
{
case 1:
printf (“\queue initial creation”);
printf(“\enter the number of values”);
scanf(“%d”,&n);
create();
break;
case 2:
printf(“\n queue insertion \n\n”);
printf(“\n enter an integer to be inserted:”);
scanf(“%d”,&x);
insert();
display();
break;
case 3:
printf(“\n queue deletion \n\n”);
delete();
display();
break;
case 4:
printf(“\n--------------------“);
printf(“\n current queue \n\n”);
display();
break;
}
}
while(c!=5);
}
void create()
{
int=1;
printf(“\n enter numbers now \n”);
for(i=1;i<=n;++i)
scanf(“%d”,&a[i]);
rear=i-1;
if(front==0)
front=1;
}
void insert()
{
if(rear>50)
printf(“\n queue is full!\n”);
else
{
rear =rear+1;
a[rear]=x;
if(front==0)
front=1;
}
}
void delete()
{
int p;
if(front!=0)
{
p=a[front];
if(front==rear)
{
printf(“\n the last item %d is deleted \n”,p);
front=rear=0;
}
}
else
{
front++;
printf(“\n element deleted is %d \n”,p);
}
printf(“\n queue is already empty!\n”);
}
OUTPUT FOR QUEUE USING ARRAYS
***************************************missing****************
//ARRAY IMPLEMENTATION OF LIST ADT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
struct array
{
int value;
int chain;
} a[size];
void main()
{
int n,I,position,choice,start;
char ans;
int create(struct array a[ ],int);
void display(struct array a[ ],int);
int search (struct array a[ ],int,int);
clrscr();
printf(“\n program for implementation of array using list ADT”);
do
{
printf(“\n \n\t\t MAIN MENU”);
printf(“ \n 1.create \n2.display\n3.search”);
printf(“\n enter the choice(1/2/3):);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“\n how many elements do you want in a list?:”);
scanf(“%d”,&n);
for(I=0;I<n;I++)
{
a[I].value=-1;
a[I].chain=-1;
}
start=create(a,n);
break;
case 2:
display(a,start);
break;
case 3:
printf(“\n enter the position at for searching:”);
scanf(“%d”,&position):
I=search(a, start,position);
If(I!=-1)
Printf(“The %d position indicating value as
%d”,position.a[I].value);
break;
default: printf(“\n Quit”);
break;
}
printf(“\n Do you want to continue?”);
ans=getch();
}while(ans==’Y’);
getch();
}
int create (struct array a[size],int n)
{
int I,flag=0,index,start_index,prev_index;
if(n>size)
{
printf(“\n Elements exceed the list size”);
getch();
exit(0);
}
I=0;
printf(“\n Enter the location and value at that location”);
while(I<n)
{
if(flag==0)
{
scanf(“%d”,&index);
scanf(“%d”,&a[index].value);
start_index=index;
prev_index=start_index;
flag=I;
}
else
{
scanf(“%d”,&index);
scanf(“%d”,&a[index].value);
a[prev_index].chain=index;
prev_index=index;
}
i++;
}
a[index].chain=-99;
return start_index;
}
void display(struct array a[size],int start_index)
{
int I;
printf(“\n you have entered the list as \n”);
for(I=start_index;a[I].chain!=-99;I=a[I].chain)
printf(“%d->”,a[I],value);
printf(“%d->NULL”,a[I].value);
}
int search(struct array a[size],int start_index,int position)
{
int i,count=0;
for(I=start_index;a[i].chain!=-99;I=a[I],chain)
{
count ++;
if(count==position)
return i;
}
printf(“\n The element is not present”);
return -1;
SINGLY LINKED LIST
#include<stdio.h>
#include<alloc.h>
#include<string.h>
#include<conio.h>
#define TRUE 1
#dedfine FALSE 0
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#define MAX 20
int list[MAX];
void main()
{
int choice();
int create();
char ans;
void display (int);
int search(int);
void delet(int);
do
{
clrscr();
printf(“\n PROGRAM TO IMPLEMENT CUSOR USING
LIST”);
printf(“\n 1.creation \n 2.display \n 3.search \n 4.delete \n 5.quit”);
printf(“\n ENTER THE YOUR CHOICE(1-5)”);
scanf(“%d,&choice);
switch(choice)
{
case 1: len=create( );
break;
case 2: display(len);
break;
case 3: position=search(len);
break;
case 4: delet(len);
break;
case 5: printf(“\n DO YOU WANT TO EXIT (y/n)?”);
ans=getch();
if (ans==’y’)
exit(0);
else
break;
default:
clrscr();
printf(“\n invalid choice, try again”);
getch();
}
}while(choice!=5);
}
int create()
{
int n,i;
clrscr()
printf(“\n how many elements you want in the list:”);
scanf(“%d”,&n);
if(n>MAX)
printf(“\n ERROR: number of elements exceed the limit”);
for(i=0;i<n;i++)
{
printf(“\n enter the element number %d:”i+1);
scanf(“%d”,&list[i]);
}
printf(“\n the list is successfully created \n”);
getch();
return(n);
}
void display(int n)
{
int i;
clrscr();
printf(“\n the list is……\n”);
for(i=0;i<n;i++)
{
if (list[i]!=-1)
printf(“\n%d”,list[i]);
}
printf(“\n press any key to continue…\n”);
getch();
}
int search(int n)
{
int i,key;
clrscr();
printf(“\n enter the number you want to search ?”);
scanf(“%d”,key);
for(i=0;i<n;i++)
{
if(list[i]==key)
{
printf(“\n the given number is at poition %d\n”,I+1);
getch();
return i;
}
}
printf(“\n the given number is not in the list\n”);
getch();
}
void delet(int n)
{
int i;
i=search(n);
list[i]=-1;
printf(“\n the element is now deleted!”);
getch( );
}
OUTPUT
1.creation
2.display
3.search
4.delete
5.quit
enter your choice(1-5):
1.create
2.display
3.search
4.delete
5.quit
enter your choice (1-5): 2
main menu
1.push
2.pop
3.display
4.exit
enter the choice:4
//IMPLEMENTATION OF STACK USING ARRY
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int s[size];
int top;
}st;
int stfull()
{
if(st.top>=size-1
return 1;
}
void push(int item)
{
st.top++;
st.s[st.top==-1]=item;
}
int sempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
int pop()
{
int item;
item=st.s[st.top];
st.top--;
return(item);
}
void display()
{
int i;
if (sempty())
printf(“\n stack is empty”);
else
{
for(i=st.top;i>=0;i--)
printf(“\n\t\t implementation of stack “);
do
{
printf(“\n main menu”);
printf(“\n 1.push \n 2.pop \n 3.display \n 4.exit”);
printf(“\n enter the choice”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:printf(“\n enter the item to be pushed”);
scanf(“%d”,&item);
if(stfull())
printf(“\n stack is full!”);
else
push(item);
break;
case 2:if (sempty())
printf(“\n empty stack ! underflow!!”);
else
{
item=pop();
printf(“\n the popped element is %d”,item);
}
break;
case 3:display();
break;
case 4:exit(0);
}
printf(“\n DO YOU WANT TO CONTINUE?”);
ans=getch();
}while(ans==’y’||’Y’);
getch();
}
OUTPUT
PROGRAM FOR IMPLEMENTATION OF STACK
Main menu
1.push
2.pop
3.display
4.exit
enter the choice: 1
#include<stdio.h>
#include<conio.h>]
#include<stdlib.h>
#include<alloc.h>
#include<process.h>
typedef struct stack
{
int data;
struct stack *next
}node;
void main()
{
node *top;
int data, item, choice;
char ans, ch;
void push(node **);
void display(node **);
int pop(node **);
int sempty(node *);
clrscr();
top=NULL;
printf(:\n\t\t stack using the linked list”);
do
{
printf(“\n\n the main menu”);
printf(“\n 1.push\n 2.pop\n3.display \n 4.exit”);
printf(“\n enter the choice”);
switch(“%d”,&choice);
{
case 1: printf(“\n enter the data”);
scanf(“%d”,&data);
break;
case 2: if (sempty(top))
printf(“\n stack underflow”);
else
{
item=pop(&top)
printf(“\n the popped node is %d”,item);
}
break;
case 4: printf(“\n do you want to quit?(y/n)”);
ch=getch();
if(ch==’y’);
exit(0);
break;
}
printf(“\n do you want to continue”);
ans=getch();
getch()
clrscr();
}while(ans==’y’||ans==’Y’);
getch();
}
void push(int item,node **top)
{
node *new;
node *get_node(int);
new=get_node(item);
new->next=*top;
*top=new;
}
node *temp;
temp=(node *)malloc(sizeof(node));
if(itemp==NULL)
printf(“\n memory can not be allocated\n”);
temp->data=item;
temp->next=NULL;
return(temp);
}
int sempty(node *temp)
{
if(temp==NULL)
return 1;
else
return 0;
}
int pop(node **top)
{
int item;
node *itemp;
item=(*top)->data;
temp=*top;
top=(top)->next;
free(temp);
return(item);
}
void display(node **head)
{
node *temp;
temp=*head;
if(sempty(temp))
printf(“\n the stack is empty !”);
else
{
while(temp!=NULL)
{
printf(“%d\n”,temp->data);
temp=temp->next;
}
}
getch();
}
OUTPUT
main menu
1.push
2.pop
3.display
4.exit
enter the choice:1
main menu
1.push
2.pop
3.display
4.exit
enter the choice:1
main menu
1.push
2.pop
3.display
4.exit
enter the choice:3
6
5
main menu
1.push
2.pop
3.display
4.exit
enter the choice:3
5
/*if it is operator */
{
op2 = pop ( ) ;
op1 = pop ( ) ;
switch (ch)
{
case ' + ' : result = op1 + op2 ;
break ;
case ' - ' : result = op1 - op2 ;
break ;
case ' * ' : result = op1 * op2 ;
break ;
case ' / ' : result = op1 / op2 ;
break ;
case ' ^ ' : result = pow ( op1, op2 ) ;
break ;
}
push( result ) ;
}
i++;
ch = exp [ i ] ;
}
result = pop ( );
return (result ) ;
}
/*******************************************************************************
array_st.h file
********************************************************************************/
#define MAX 10
struct stack
{
double s [ MAX ] ;
int top ;
} st;
void push ( double val )
{
if ( st. top + 1 >= MAX )
printf( " \n stack is full \n ") ;
st. top + + ;
st . s[ st . top ] = val ;
}
double pop ( )
{
double val ;
if ( st. top == -1 )
printf (* " \n stack is empty \n " );
val = st . s[ st. top ] ;
st. top - - ;
return ( val ) ;
}