Documente Academic
Documente Profesional
Documente Cultură
#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;
}
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+1])
node++;
if(node>i-1)
node=-1;
}
a[parent]=pos;
}
}
OUT PUT TO HEAP SORT
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 i=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
Main Menu
1.insert
2.delete
3.display
enter your choice:1
1.insert
2.delete
3.display
enter your choice:1
Main Menu
1.insert
2.delete
3.display
enter your choice:1
Main Menu
1.insert
2.delete
3.display
enter your choice:3
23
33
45
do you want to continue?y
Main Menu
1.insert
2.delete
3.display
enter your choice:2
Main Menu
1.insert
2.delete
3.display
enter your choice:3
33
45
Main Menu
1.insert
2.delete
3.display
enter your choice:
//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=1;
}
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;
}
OUTPUT:
Main menu
1.create
2.display
3.search
enter your choice(1/2/3):1
Main menu
1.create
2.display
3.search
enter your choice(1/2/3):2
Main menu
1.create
2.display
3.search
enter your choice(1/2/3):3
Main menu
1.create
2.display
3.search
enter your choice(1/2/3):
SINGLY LINKED LIST
#include<stdio.h>
#include<alloc.h>
#include<string.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
}
void delete()
{
printf(“\n Enter the name to be deleted:”);
scanf(“%s”,&nam);
temp=list;
while(((strcmp(temp->name,nam))!=0)&&(temp->next!=NULL))
{
prev=temp;
temp=temp->next;
if(strcmp(temp->name,nam)==0)
{
prev->next=temp->next;
free(temp);
}
else
{
printf(“\n The given name is not found in the list”);
getch();
}
display();
}
}
void search()
{
int found;
printf(“\n Enter the name you want to search”);
scanf(“%s”,&nam);
while(((strcmp(temp->name,nam))!=0)&&(temp->next!=NULL))
{
prev=temp;
temp=temp->next;
if(strcmp(temp->name,nam)==0)
{
found=TRUE;
else
found=FALSE;
}
if(found)
{
printf(“\n The given name is found”);
getch();
}
else
{
printf(“\nThe given value is not found”);
getch();
}
}
}
display()
{
temp=list;
printf(“\n\n”);
while(temp!=NULL)
{
printf(“%s \n”,temp->name);
temp=temp->next;
}
getch();
return 0;
}
count nodes()
{
int count;
temp=list;
printf(“\n\n”);
while(temp!=NULL)
{
temp=temp->next;
count=count+1;
}
printf(“\n The total number of nodes in the list is
=%d”,count);
getch();
return 0;
}
void menu()
{
clrscr();
printf(“\n\n--------------\n”);
printf(“singly linked list\n”);
printf(“\n\n-------------\n”);
printf(“\n 1.create”);
printf(“\n 2.delete”);
printf(“\n 3.add”);
printf(“\n 4.display”);
printf(“\n 5.countnodes”);
printf(“\n 6.insert”);
printf(“\n 7.search”);
printf(“\n 8.exit”);
printf(“\n---------------\n”);
printf(“\n Enter your choice (1-8):”);
scanf(“%d”,choice);
}
void main()
{
list=NULL;
temp=NULL;
do
{
menu();
switch(choice)
{
case 1:
create();
break;
case 2:
add();
break;
case 3:
delete();
break;
case 4:
display();
break;
case 5:
countnodes();
break;
case 6:
insert();
break;
case 7:
search();
break;
}
}
while(choice!=0);
}
void insert()
{
char nam name[50];
temp=malloc (sizeof (struct listtype));
if(temp->next==NULL)
{
printf(“\n Insertion is not possible \n”);
getch();
}
clrscr();
printf(“\n Enter the element after which you want to insert”);
scanf(“%s”,nam);
while(((strcmp(temp->name,nam))!=0)&&(temp->next!=NULL))
}
OUTPUT:
Main Menu
1.create
2.display
3.search
4.quit
enter your choice(1-4):1
1.create
2.display
3.search
4.quit
enter your choice(1-4):2
1->2->3->4->NULL
1.create
2.display
3.search
4.quit
enter your choice(1-4):3
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#define MAX 20
int list[MAX];
void main()
{
int choice,len,position;
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;
1.create
2.display
3.search
4.delete
5.quit
enter your choice (1-5): 2
#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%d”,st.s[i]);
}
}
void main()
{
int item,choice;
char ans;
st.top=-1;
clrscr();
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
#include <stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
#include"stack7.h"
void main(void)
{
char ans,bracket[10];
char data,item;
node *top;
int choice;
int i;
void push(char,node**);
void display(node **) ;
char pop ( node **) ;
int Sempty ( node *);
top = NULL;
clrscr ( ) ;
printf ( " \n\n\t\t enter the expression and put $ at the end
:\t " );
scanf ( " %s " , bracket ) ;
i = 0;
if ( bracket [ i ] == ' ) ' )
printf ( " \n the expression is invalid " ) ;
else
{
do
{
if ( bracket [ i ] == ' ( ' )
{
push ( bracket [ i ] , &top);
}
else if ( bracket [ i ] == ' ) ' )
{
if ( Sempty ( top ) )
{
printf ( " \nThe expression is invalid " ) ;
getch ( ) ;
exit ( 0 ) ;
}
item = pop ( & top ) ;
}
i + +;
} while ( bracket [ i ] != ' $' ) ;
if ( !Sempty ( top ) )
printf ( " \n The expression is invalid " ) ;
else
printf ( " \n tThe expression has well formed parenthensis "
);
}
getch ( ) ;
}
void display ( node ** head )
{
node * temp ;
temp = * head ;
if ( Sempty (temp) )
printf ( " \n the stack is empty! " ) ;
else
{
while ( temp != NULL )
{
printf ( " %c \n " , temp - > data) ;
temp = temp - > next ;
}
}
getch ( ) ;
}
// stack7.h file
*********************************************************************************/
/* Data structure for the linked stack */
typedef struct stack
{
char data ;
struct stack *next ;
}node ;
/* Functionality performed on linked stack */
void push ( char item , node **top )
{
node *New;
node * get_node ( char);
/*if it is operator */
{
op2 = pop (&top ) ;
op1 = pop ( &top ) ;
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 ) ;
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include”stack1.h>
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post();
clrscr();
printf(“enter the postfix expression :/n”);
scanf(“%s,exp);
len=strlen(exp);
exp[len]=’$’;
result=post(exp);
printf(“the value of the expression is %f\n”,result);
getch()
exit(0);
}
double post(char exp[])
{
char ch,*type;
double result,val,op1,op2;
int I;
node *top;
void push(char,node**);
char pop(node**);
top=NULL;
i=0;
Ch=exp[I];
While(ch!=’$’)
{
if{ch>=’0’ && ch<=’9’)
type=”operand”;
else if(ch==’+’ || ch==’-‘ || ch==’*’ || ch==’/’ || ch==’^’)
type=’operator”;
if(strcmp(type,”operand”)==0)
{
val=ch-48;
push(val,&top);]
}
else
if(strcmp(type,”operator”)==0)
{
op2=pop(&top);
op2=pop(&top);
switch(ch)
{
case’+’:result=op1 + op2;
break;
case’-‘:result=op1 – op2;
break;
case’*’;result=op1 * op2;
break;
case’/’;result=op1 / op2;
result;
case’^’:result=pow(op1,op2);
break;
}
push(result,&top);
}
ch=exp[I];
}
result=pop(&top);
return(result);
}
PROGRAM FOR STACK1.H