Академический Документы
Профессиональный Документы
Культура Документы
FILES
Function name:
(FILE *) fopen(file_name, mode)
Description
This function opens a file and establishes a current offset
within the file. This must be done before attempts are
made to read from or write to the file.
Argument list
(char *) file_name – The starting address of a character
string describing the name of the file.
This can be either a character string, or
array name, or a character pointer.
(char *) mode-
This describes the actions to be
performed on the file and governs the
initial offset in the file. The modes for
opening a file are:
r - Opened for reading. Positioned at the
beginning of the file.
w - Opened for writing. The assumption is
that the file is to be created. If the file
does not exist, it is created, if it does
exist, it is truncated. Positioned at the
beginning of the file.
a - Opened for writing. Same rules apply
as for mode w except that initial
positioning is at the end of the file and
the file is not truncated.
r+ - Opened for update. File is available for
reading and writing but is not
truncated. The initial positioning is at
the start of the file.
w+ - Opened for update. File is available for
reading & writing and is truncated if
the file does not exist, it is created.
Initial positioning is at the start of the
file.
a+ - Opened for update. File is available for
reading and writing, is not truncated,
and initial positioning is at the end of
the file.
Return values:
FILE * Retained for successful open. Value is a
pointer to a structure that contains file
opening and buffering information. The
return value is to identify the file for
performing input and output.
(FILE *) NULL Returned if the file cannot be opened.
Occurs if the file does not exist
and is being opened other than for writing,
or if the user is opening a new file for
writing, but does not have permission to create a
file with the supplied file name.
Argument list
(FILE *)stream This is the return value from fopen()
and is used to identify which file is to be
closed.
Return values
0 This means a successful file closing
EOF This means an error was encountered when closing
the file.
Function name:
int fputc(c,stream);
Description:
The fputc function writes the single character to the file at
the current position.
Argument list:
int c character to be written
FILE *stream pointer to FILE structure that is the return
value from fopen().
Return values
int The value of character return
EOF indicates an error.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char strptr1[]=“This is the first FILE program !!\n";
char *p;
clrscr();
fptr=fopen("charfile.txt","w");
if(fptr==NULL)
{
printf("\nUnable to open the file");
exit(1);
}
for(p=strptr1;*p!='\0';p++)
fputc(*p,fptr);
getch();
}
Function Name:
int fputs(str,stream);
Description
The fputs function copies string to the output file at the
current position. The terminating character (‘\0’) is not
copied.
Argument list
char * str pointer to the string to be output.
FILE *stream pointer to FILE structure which
specifies the name of the file to which
output has to be written.
Return Values
int (>0) Non negative value if successful
EOF Error
This program receives strings from keyboard and
writes them to file using fputs() function.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char s[80];
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL)
{
puts("Cannot open file ");
exit();
}
printf("\nEnter a few lines of text : \n");
fflush(stdin);
while(strlen(gets(s))>0)
fputs(s,fptr);
fclose(fptr);
getch();
}
Function Name:
int fprintf(stream, format, argument)
Description:
fprintf() printf formatted data to the file. The fprintf()
function formats and prints a series of characters &
values to output file.
Argument list:
FILE *stream Pointer to FILE structure
char *format Format-control string
Arguments Each argument is converted and
output according to corresponding format
specification in format.
Return values
int (>0) The number of characters printed.
(<0) In case of an output error.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ans='Y';
char name[40];
int age;
float bsal;
clrscr();
fp=fopen("EMPLOYEE.txt","w");
if(fp==NULL)
{
printf("\nCannot open file");
exit();
}
while(ans=='Y' || ans =='y')
{
printf("\nEnter name,age and basic salary\n");
scanf("%s %d %f",name,&age,&bsal);
fprintf(fp, "%s %d %f\n",name,age,bsal);
printf("\nAnother employee (Y/N) : ");
fflush(stdin);
scanf("%c",&ans);
}
fclose(fp);
getch();
}
Function Name:
int fgetc(stream);
Description:
The fgetc() function reads a single character from the
current position of the file associated with the stream.
The character is converted and returned as an int. The
function then increments the associated file pointer(if
any) to point to the next character.
Argument List:
(FILE *) stream Pointer to FILE structure that is the
return value from fopen()
Return values:
int The value of the character read.
EOF Indicates an error or end-of-file.
Function Name:
char *fgets(str,n,stream)
Description:
The fgets() function reads a string from the input stream
argument and stores it in str. Characters are read from the
current stream position upto and including the first
newline character (‘\n’), upto the end of the stream, or
until the number of characters read is equal to n-1, which
ever comes first. The result is stored in str, and a null
character (‘\0’) is appended. The new line character if
read is included in the string. If n is equal to 1, the str is
empty(“”). The fgets function is similar to the gets
function; however gets replaces the newline character
with NULL.
Argument List:
char *str storage location for data
int n Number of characters stored
FILE *stream Pointer to FILE structure, which identifies
the file from where the data has to be
read.
Return Values:
char * The starting address of str.
NULL Error or end-of-file condition.
#include<stdio.h>
#include<conio.h>
void main()
{
char inp_line[300],filename[30];
int counter;
FILE *fptr;
clrscr();
printf("\nEnter filename : ");
gets(filename);
fptr=fopen(filename,"r");
if(fptr==NULL)
{
printf("\nCannot open %s",filename);
exit(0);
}
counter=0;
printf("\nThe contents of the file :\n ");
while(fgets(inp_line,300,fptr)!=NULL)
{
printf("%s",inp_line);
counter++;
}
printf("\nTotal NUMBER OF LINES = %d",counter);
fclose(fptr);
getch();
}
Function Name:
int fscanf(stream, format, argument)
Description:
fscanf() function reads formatted data from a stream. It
reads data from the current position of stream into the
location given by the argument. Each argument must be a
pointer to a variable with a type that corresponds to a type
specifier in format.
Argument list:
FILE *stream Pointer to FILE structure
char *format Format-control string
argument Optional arguments
Return values:
int > 0 The number of strings that were
successfully converted and assigned.
The return values does not include fields that were
read but not assigned.
0 No values were assigned.
EOF error or end-of-file.
Function Name:
(int) fseek(stream,offset,where)
Description:
This function changes the current position within a file by
changing the offset associated with the opening of the
file. Positioning can be specified as a number of bytes
from the beginning of the file, from the end of the file, or
from the current position in the file. If a file is positioned
past the actual end of the file and a write is performed,
then the size of the file will be extended and a gap
will be placed in the file. If a read is attempted past the
end of the file, an error will occur.
Argument List:
(FILE *) stream This identifies the file in which the
current offset is to be changed.
Should be the saved return value from the file’s
fopen().
(long) offset The offset, in bytes, either from the start of
the file, the end of the file, or from the current position.
Function Name:
long ftell(stream)
Description:
The ftell function gets the current position of the file
pointer(if any). The position is expressed as an offset
relative to the beginning of the file.
Argument List:
(FILE *) stream Pointer to a FILE structure, the return
value from the fopen of the file whose offset
is needed.
Return values:
long Current file position
-1 On error.
Function Name:
void rewind(stream)
Description:
The rewind function repositions the file pointer to the
beginning of the file. A call to rewind is equivalent to
fseek(stream, 0L, seek_set);
Argument List:
(FILE *) stream Pointer to FILE structure
Return Values:
No return value
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
struct student
{
int usn;
char name[30];
int marks1;
int marks2;
int marks3;
};
typedef struct student STUD;
2. STACKS
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int top=-1;
int stack[SIZE];
void display()
{
int i;
if(top==-1)
{
printf("\nSTACK IS EMPTY....");
getch();
return;
}
for(i=top;i>=0;i--)
printf("\nstack[%d]=%d",i,stack[i]);
getch();
}
void push(int d)
{
if(top==(SIZE-1))
{
printf("\nSTACK IS FULL ..!! STACK
OVERFLOW...");
getch();
}
else
{
stack[++top]=d;
printf("\nThe content of the stack after insertion is :\n");
display();
}
}
void pop(void)
{
int pop_ele;
if(top==-1)
{
printf("\nThe stack is empty..!!STACK
UNDERFLOW...");
getch();
}
else
{
pop_ele=stack[top--];
printf("\nThe popped out element of the stack is :
%d",pop_ele);
printf("\n\nThe content of the stack after deletion is ");
display();
}
}
void main()
{
int data,choice,q=0;
while(q!=1)
{
clrscr();
printf("\n\n\n\t\t\t\t*****MAIN MENU*****");
printf("\n\n\n\n\n\n\t ENTER 1 --> TO PUSH THE
ELEMENT INTO THE STACK");
printf("\n\t ENTER 2 --> TO POP THE ELEMENT
FROM THE STACK");
printf("\n\t ENTER 3 --> TO DISPLAY THE CONTENT
OF THE STACK ");
printf("\n\t ENTER 4 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nENTER THE ELEMENT TO BE
INSERTED : ");
scanf("%d",&data);
push(data);
break;
case 2:
pop();
break;
case 3:
printf("\nTHE CONTENT OF THE STACK IS : ");
display();
break;
case 4:
q=1;
printf("\nTHANK YOU....HAVE A NICE DAY");
break;
}
}
getch();
}
2.3 Expressions
Infix Expression :
Any expression in the standard form like "2*3-4/5" is an
Infix expression. (Operator in between the two operands).
(In-order) expression.
Postfix Expression :
The Postfix expression has the Operator after the two
operands. (Post order) form of the above expression is
"23*45/-".
End result:
Postfix expression: ab+c-
Infix expression: ((a+b)-c)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#define size 20
int top=-1;
int stack[size];
int operate(char symb, int oprd1, int oprd2)
{
switch(symb)
{
case '+': return oprd1+oprd2;
case '-': return oprd1-oprd2;
case '*': return oprd1*oprd2;
case '/': return oprd1/oprd2;
case '$':
case '^': return pow(oprd1,oprd2);
}
}
void push(int s[], int d)
{
s[++top]=d;
}
int pop(int s[])
{
int data;
data=s[top--];
return data;
}
void main()
{
int i,ans,oprd1,oprd2,choice,q=0;
char symb, expr[20];
clrscr();
do
{
printf("\n\n\n\t\t\t\t*******MAIN
MENU**********");
printf("\n\tPRESS 1 --> TO EVALUATE
EXPRESSION");
printf("\n\tPRESS 2 --> TO QUIT");
printf("\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the postfix expression : ");
fflush(stdin);
gets(expr);
for(i=0;i<strlen(expr);i++)
{
symb=expr[i];
if(symb>='0' && symb<='9')
push(stack,symb-'0');
else
{
oprd2=pop(stack);
oprd1=pop(stack);
ans=operate(symb,oprd1,oprd2);
push(stack,ans);
}
}
ans=pop(stack);
printf("\nThe result of the postfix expression is :
%d",ans);
getch();
clrscr();
break;
case 2:
q=1;
}
}while(!q);
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define SIZE 20
#define Operator (-10)
#define Operand (-20)
#define Leftparen (-30)
#define Rightparen (-40)
int top=-1;
char stack[SIZE],infix[SIZE],postfix[SIZE];
void push(char symbol)
{
if(top==(SIZE-1))
printf("\nSTACK OVERFLOW...!!");
else
stack[++top]=symbol;
}
char pop(void)
{
char pop_ele;
if(top==-1)
printf("\nSTACK UNDERFLOW...!!");
else
{
pop_ele=stack[top--];
return pop_ele;
}
}
void infix_to_postfix(void)
{
int i,p,len,type,precedence;
char next;
i=p=0;
len=strlen(infix);
while(i<len)
{
if(!white_space(infix[i]))
{
type=get_type(infix[i]);
switch(type)
{
case Leftparen:
push(infix[i]);
break;
case Rightparen:
while((next=pop())!='(')
postfix[p++]=next;
break;
case Operand:
postfix[p++]=infix[i];
break;
case Operator:
precedence=get_prec(infix[i]);
while(top>-1 &&
precedence<=get_prec(stack[top]))
postfix[p++]=pop();
push(infix[i]);
break;
}
}
i++;
}while(top>-1)
postfix[p++]=pop();
postfix[p]='\0';
}
2.4 RECURSION
#include<stdio.h>
#include<conio.h>
int mul(int a,int b)
{
return(b==1 ? a: mul(a,b-1)+a);
}
void main()
{
int a,b,res;
clrscr();
printf("\nEnter the two numbers : ");
scanf("%d %d",&a,&b);
res=mul(a,b);
printf("\nThe result is = %d",res);
getch();
}
#include<stdio.h>
#include<conio.h>
int count=0;
void twrofhanoi(int m, char srcpeg, char tempeg, char
destpeg)
{
if(m==1)
{
printf("\nMove disk 1 from %c to %c",srcpeg,destpeg);
count++;
return;
}
twrofhanoi(m-1,srcpeg,destpeg,tempeg);
printf("\nMove disk %d from %c to
%c",m,srcpeg,destpeg);
count++;
twrofhanoi(m-1,tempeg,srcpeg,destpeg);
}
void main()
{
int n;
clrscr();
printf("\n\n\n\n\n\t\tPROGRAM TO IMPLEMENT THE
TOWER OF HANOI");
printf("\n\n\n\n\nPLEASE ENTER THE NUMBER OF
DISCS : ");
scanf("%d",&n);
twrofhanoi(n,'X','Y','Z');
printf("\nTotal number of disc moves = %d",count);
getch();
}
3. QUEUES
For inserting elements into the queue are done from the rear
end and deletion is done from the front end, we use external
pointers called as rear and front to keep track of the status
of the queue.
During insertion, Queue Overflow condition has to be
checked. Likewise during deletion, Queue Underflow
condition is checked.
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int front=-1,rear=-1,flag,queue[SIZE];
int qinsert(int data)
{
flag=0;
if(rear<SIZE-1)
{
queue[++rear]=data;
if(front==-1)
front++;
flag=1;
}
else
printf("\nThe queue is full, Can't insert");
return(flag);
}
int qdelete()
{
int data;
flag=0;
if(front<=rear)
{
flag=1;
data=queue[front++];
printf("\nThe Element deleted from the queue is :
%d",data);
if(front>rear)
{
front=-1;
rear=-1;
}
}
else
printf("\nThe queue is empty, No elements to
delete....!!");
return(flag);
}
void qdisplay()
{
int i;
if(front==-1)
printf("\nQueue is empty...!!!");
else
for(i=front;i<=rear;i++)
printf("\nqueue[%d] = %d",i,queue[i]);
}
void main()
{
int choice,data,q=0;
while(q!=1)
{
clrscr();
printf("\n\n\n\t\t\t*****MAIN MENU*****");
printf("\n\n\n\tENTER 1 --> TO ENTER THE
ELEMENTS INTO THE QUEUE");
printf("\n\tENTER 2 --> TO DELETE THE
ELEMENTS FROM THE QUEUE");
printf("\n\tENTER 3 --> TO DISPLAY THE
CONTENTS OF THE QUEUE");
printf("\n\tENTER 4 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
qinsert(data);
if(flag)
{
printf("\nThe content of the queue after
insertion is : ");
qdisplay();
}
break;
case 2:
qdelete();
if(flag)
{
printf("\nThe content of the queue after deletion
is : ");
qdisplay();
}
break;
case 3:
printf("\nThe contents of the queue is : ");
qdisplay();
break;
case 4:
q=1;
printf("\nTHANK YOU..!! HAVE A NICE
DAY");
break;
}
getch();
}
}
3.2Circular Queue
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int cq[SIZE],data,count=0,flag=0;
int rear=-1,front=0;
void Insert_cqueue()
{
flag=0;
if(count<SIZE)
{
printf("\nEnter the element : ");
scanf("%d",&data);
rear=(rear+1)%SIZE;
cq[rear]=data;
count++;
flag=1;
}
else
printf("\nCircular queue is full.!!OVERFLOW
CONDITION..!!\a");
}
void Delete_cqueue()
{
if(count==0)
{
printf("\n\aCIRCULAR QUEUE UNDERFLOW....!!");
flag=0;
return;
}
data=cq[front];
printf("\nElement deleted is : %d",data);
front=(front+1)%SIZE;
count--;
flag=1;
}
void Display_cqueue()
{
int i=front,j;
if(count==0)
{
printf("\nCircular queue is Empty");
return;
}
for(j=0;j<count;j++)
{
printf("\ncq[%d]=%d",i,cq[i]);
i=(i+1)%SIZE;
}
}
void main()
{
int choice,k=0;
do
{
clrscr();
printf("\n\n\n\n\t\t\t*****MAIN MENU*****");
printf("\n\n\n\tENTER 1 --> TO INSERT AN
ELEMENT INTO THE CIRCULAR QUEUE");
printf("\n\tENTER 2 --> TO DELETE AN ELEMENT
FROM THE CIRCULAR QUEUE");
printf("\n\tENTER 3 --> TO DISPLAY THE
CONTENTS OF THE CIRCULAR QUEUE");
printf("\n\tENTER 4 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
printf("\nYour choice is --> %d",choice);
switch(choice)
{
case 1:
Insert_cqueue();
if(flag)
{
printf("\nCircular queue contents after inserting is :
\n");
Display_cqueue();
}
break;
case 2:
Delete_cqueue();
if(flag)
{
printf("\nCircular queue contents after deletion is :
\n");
Display_cqueue();
}
break;
case 3:
Display_cqueue();
break;
case 4:
k=1;
break;
}
getch();
}while(k!=1);
}
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int front=-1,rear=-1,flag,queue[SIZE];
int qinsert_rear(int data)
{
flag=0;
if(rear<SIZE-1)
{
queue[++rear]=data;
if(front==-1)
front++;
flag=1;
}
else
printf("\nCan't insert from rear end\a");
return(flag);
}
int qdelete_front()
{
int data;
flag=0;
if(front>=0&&front<=rear)
{
flag=1;
data=queue[front++];
printf("\nThe Element deleted from the queue is :
%d",data);
if(front>rear)
{
front=-1;
rear=-1;
}
}
else
printf("\nThe queue is empty, No elements to
delete....!!");
return(flag);
}
int qinsert_front(int data)
{
flag=0;
if(front==-1&&rear==-1)
{
queue[++front]=data;
rear++;
flag=1;
return;
}
if(front!=0)
{
queue[--front]=data;
flag=1;
}
else
printf("\nCan't insert from front end\a");
return(flag);
}
int qdelete_rear()
{
int data;
flag=0;
if(rear>=0&&front<=rear)
{
flag=1;
data=queue[rear--];
printf("\nThe Element deleted from the queue is :
%d",data);
if(front>rear)
{
front=-1;
rear=-1;
}
}
else
printf("\nThe queue is empty, No elements to
delete....!!");
return(flag);
}
void qdisplay()
{
int i;
if(front==-1)
printf("\nQueue is empty...!!!");
else
for(i=front;i<=rear;i++)
printf("\nqueue[%d] = %d",i,queue[i]);
}
void main()
{
int choice,data,q=0;
while(q!=1)
{
clrscr();
printf("\n\n\n\t\t\t*****MAIN MENU*****");
printf("\n\n\n\tENTER 1 --> TO ENTER THE
ELEMENTS INTO THE QUEUE FROM THE REAR
END");
printf("\n\tENTER 2 --> TO DELETE THE
ELEMENTS FROM THE QUEUE FROM THE FRONT
END");
printf("\n\tENTER 3 --> TO ENTER THE
ELEMENTS INTO THE QUEUE FROM THE FRONT
END");
printf("\n\tENTER 4 --> TO DELETE THE
ELEMENTS FROM THE QUEUE FROM THE REAR
END");
printf("\n\tENTER 5 --> TO DISPLAY THE
CONTENTS OF THE QUEUE");
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int q1[SIZE],q2[SIZE],q3[SIZE],ch;
int r1=-1,r2=-1,r3=-1;
int f1=0,f2=0,f3=0;
void Insert_queue(int n)
{
if(n==1)
if(r1<SIZE-1) q1[++r1]=ch;
if(n==2)
if(r2<SIZE-1) q2[++r2]=ch;
if(n==3)
if(r3<SIZE-1) q3[++r3]=ch;
else
printf("\nQueue %d is full!!",n);
}
void Delete_queue()
{
if(f1>r1) printf("\nQueue1 is empty!!");
else
{ printf("\nElement deleted is : %d",q1[f1++]);
return;
}
if(f2>r2) printf("\nQueue2 is empty!!");
else
{ printf("\nElement deleted is : %d",q2[f2++]);
return;
}
if(f3>r3) printf("\nQueue3 is empty!!");
else printf("\nElement deleted is : %d",q3[f3++]);
}
void Display_queue()
{
int i;
if(f1>r1)
printf("\n\n\nQueue1 is empty!!");
else
{
printf("\n\n\n");
for(i=f1;i<=r1;i++)
printf("\nq1[%d]=%d",i,q1[i]);
}
if(f2>r2)
printf("\n\n\nQueue2 is empty!!");
else
{
printf("\n\n\n");
for(i=f2;i<=r2;i++)
printf("\nq2[%d]=%d",i,q2[i]);
}
if(f3>r3)
printf("\n\n\nQueue3 is empty!!");
else
{
printf("\n\n\n");
for(i=f3;i<=r3;i++)
printf("\nq3[%d]=%d",i,q3[i]);
}
}
void main()
{
int choice,q=0,n,i;
do
{
clrscr();
printf("\n\n\n\t\t\t*****MAIN MENU******");
printf("\n\tEnter 1 --> TO INSERT");
printf("\n\tEnter 2 --> TO DELETE");
printf("\n\tEnter 3 --> TO DISPLAY");
printf("\n\tEnter 4 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the priority of the queue : ");
scanf("%d",&n);
if(n>=1 &&n<=3)
{
printf("\nEnter the data : ");
scanf("%d",&ch);
}
else
{
printf("\nInvalid...! priority queue number..Cannot
Insert");
getch();
break;
}
Insert_queue(n);
Display_queue();
getch();
break;
case 2:
Delete_queue();
Display_queue();
getch();
break;
case 3:
Display_queue();
getch();
break;
case 4:
q=1;
printf("\nTHANK YOU..... HAVE A NICE DAY");
break;
}
}while(!q);
getch();
}
4. LINKED LISTS
4.1 Overview
Disadvantages of static/sequential allocation technique:
NODE:
4.2 Implementations
A ‘C’ program to construct a SINGLE LINKED LIST
using dynamic variables and pointers.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
void main()
{
NODE first=NULL;
int k=0,choice,item;
while(k!=1)
{
clrscr();
printf("\nPRESS 1 --> TO INSERT FROM FRONT");
printf("\nPRESS 2 --> TO DELETE FROM FRONT");
printf("\nPRESS 3 --> TO DISPLAY THE CONTENT
OF THE LIST");
printf("\nPRESS 4 --> TO QUIT");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the element to be inserted : ");
scanf("%d",&item);
first=insert_front(item,first);
printf("\nThe contents of the list after insertion is :
\n");
display(first);
break;
case 2:
first=delete_front(first);
printf("\nThe contents of the list is : \n");
display(first);
break;
case 3:
printf("\nThe contents of the list is : \n");
display(first);
break;
case 4:
k=1;
printf("\nTHANK YOU...!! HAVE A NICE DAY");
break;
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
NODE insert(int item, NODE first)
{
NODE temp,prev,cur;
temp=getnode();
temp->info=item;
temp->next=NULL;
if(first==NULL)
return temp;
if(item<first->info)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
while(cur!=NULL && item > cur->info)
{
prev=cur;
cur=cur->next;
}
prev->next=temp;
temp->next=cur;
return first;
}
void main()
{
NODE first=NULL;
int k=0,choice,item;
while(k!=1)
{
clrscr();
printf("\nPRESS 1 --> TO INSERT INTO AN
ORDERED LINKED LIST");
printf("\nPRESS 2 --> TO DISPLAY THE CONTENT
OF THE LIST");
printf("\nPRESS 3 --> TO QUIT");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the element to be inserted : ");
scanf("%d",&item);
first=insert(item,first);
printf("\nThe contents of the list after insertion is :
\n");
display(first);
break;
case 2:
display(first);
break;
case 3:
k=1;
printf("\nHAVE A NICE DAY.. THANK YOU");
break;
}
getch();
}
}
A ‘C’ program to CREATE and MERGE two SORTED
LISTS into a single list (Merging two ordered link list)
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
NODE insert(int item, NODE first)
{
NODE temp,prev,cur;
temp=getnode();
temp->info=item;
temp->next=NULL;
if(first==NULL)
return temp;
if(item<first->info)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
while(cur!=NULL && item > cur->info)
{
prev=cur;
cur=cur->next;
}
prev->next=temp;
temp->next=cur;
return first;
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct stud
{
int regno;
char name[20];
int rank;
struct stud *next;
};
typedef struct stud *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct stud));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
NODE insert(NODE first)
{
NODE temp,prev,cur;
temp=getnode();
printf("\nEnter the student's regno : ");
scanf("%d",&temp->regno);
printf("\nEnter the student's name : ");
fflush(stdin);
gets(temp->name);
printf("\nEnter the rank of the student : ");
scanf("%d",&temp->rank);
temp->next=NULL;
if(first==NULL)
return temp;
if(temp->rank<first->rank)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
while(cur!=NULL && temp->rank > cur->rank)
{
prev=cur;
cur=cur->next;
}
prev->next=temp;
temp->next=cur;
return first;
}
void main()
{
NODE first=NULL;
int k=0,choice;
while(k!=1)
{
clrscr();
printf("\n\n\n\t\t\t*****MAIN MENU*****");
printf("\n\n\n\tPRESS 1 --> TO INSERT STUDENT'S
INFORMATION INTO AN ORDERED LINKED LIST");
printf("\n\tPRESS 2 --> TO DISPLAY THE CONTENT
OF THE LIST");
printf("\n\tPRESS 3 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
first=insert(first);
printf("\nThe contents of the list after insertion is :
\n");
display(first);
break;
case 2:
printf("\nTHE CONTENT OF THE STUDENT'S
ORDERED LIST BASED ON THE RANK IS AS
FOLLOWS");
display(first);
break;
case 3:
k=1;
printf("\nHAVE A NICE DAY.. THANK YOU");
break;
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
NODE insert(int item, NODE first)
{
NODE temp,prev,cur;
temp=getnode();
temp->info=item;
temp->next=NULL;
if(first==NULL)
return temp;
if(item<first->info)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
while(cur!=NULL && item > cur->info)
{
prev=cur;
cur=cur->next;
}
prev->next=temp;
temp->next=cur;
return first;
}
void main()
{
NODE first=NULL;
int k=0,choice,item;
while(k!=1)
{
clrscr();
printf("\nPRESS 1 --> TO INSERT INTO AN
ORDERED LINKED LIST");
printf("\nPRESS 2 --> TO SEARCH FOR AN ITEM");
printf("\nPRESS 3 --> TO DISPLAY THE CONTENT
OF THE LIST");
printf("\nPRESS 4 --> TO QUIT");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the element to be inserted : ");
scanf("%d",&item);
first=insert(item,first);
printf("\nThe contents of the list after insertion is :
\n");
display(first);
break;
case 2:
printf("\nEnter the element to be searched : ");
scanf("%d",&item);
first=search(item,first);
break;
case 3:
display(first);
break;
case 4:
k=1;
printf("\nHAVE A NICE DAY.. THANK YOU");
break;
}
getch();
}
}
Function to reverse the linked list without using extra
nodes.
NODE reverse(NODE first)
{
NODE cur,prev;
prev=NULL;
while(first!=NULL)
{
cur=first;
first=first->next;
cur->next=prev;
prev=cur;
}
return cur;
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct stud
{
int id;
char st_name[25];
int sem;
struct stud *next;
};
typedef struct stud *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE) malloc(sizeof(struct stud));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return (x);
}
prev=NULL;
cur=first;
while(cur->next!=NULL&&id!=cur->id)
{
prev=cur;
cur=cur->next;
}
if(id==cur->id)
{
printf("\n\nSearch Succesful... The following information
is being deleted");
printf("\nStudent ID : %d",cur->id);
printf("\nStudent Name : %s",cur->st_name);
printf("\nStudent Semester : %d",cur->sem);
prev->next=cur->next;
if(first==cur)
first=first->next;
free(cur);
return first;
}
printf("\nSearch Unsucessful, %d student's id not found in
the list",id);
return first;
}
NODE search_update(NODE first,int id)
{
NODE cur,prev;
if(first==NULL)
{
printf("\nList is empty Search Unsucessful");
return first;
}
prev=NULL;
cur=first;
while(cur->next!=NULL&&id!=cur->id)
cur=cur->next;
if(id==cur->id)
{
printf("\n\nSearch Succesful...\nEnter the new student's
semester ");
scanf("%d",&cur->sem);
printf("\nThe student's updated record is as follows:\n\n");
printf("\nStudent ID : %d",cur->id);
printf("\nStudent Name : %s",cur->st_name);
printf("\nStudent Semester : %d",cur->sem);
return first;
}
printf("\nSearch Unsucessful, %d student's id not found in
the list",id);
return first;
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *prev;
struct node *next;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE) malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return x;
}
NODE insert_front(int item,NODE head)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
cur=head->next;
head->next=temp;
temp->prev=head;
temp->next=cur;
cur->prev=temp;
return head;
}
NODE insert_rear(int item,NODE head)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
cur=head->prev;
head->prev=temp;
temp->next=head;
temp->prev=cur;
cur->next=temp;
return head;
}
NODE delete_front(NODE head)
{
NODE cur,next;
if(head->next==head)
{
printf("\nList is empty");
return head;
}
cur=head->next;
next=cur->next;
head->next=next;
next->prev=head;
printf("\nThe item deleted is %d",cur->info);
free(cur);
return head;
}
NODE delete_rear(NODE head)
{
NODE cur,prev;
if(head->prev==head)
{
printf("\nList is empty");
return head;
}
cur=head->prev;
prev=cur->prev;
head->prev=prev;
prev->next=head;
printf("\nThe item deleted is %d",cur->info);
free(cur);
return head;
}
void display(NODE head)
{
NODE temp;
if(head->next==head)
{
printf("\nDoubly linked List is empty");
return;
}
printf("\nThe contents of the Doubly Linked list is :\n");
for(temp=head->next;temp!=head;temp=temp->next)
printf("\n%d",temp->info);
}
void main()
{
NODE head;
int choice,item,k=0;
head=getnode();
head->next=head;
head->prev=head;
while(k!=1)
{
clrscr();
printf("\n\n\n\t\t\t******MAIN MENU*******");
printf("\n\tENTER 1 --> TO INSERT ITEMS FROM
FRONT INTO THE LIST");
printf("\n\tENTER 2 --> TO INSERT ITEMS FROM
REAR INTO THE LIST");
printf("\n\tENTER 3 --> TO DELETE ITEMS FROM
FRONT OF THE LIST");
printf("\n\tENTER 4 --> TO DELETE ITEMS FROM
REAR OF THE LIST");
printf("\n\tENTER 5 --> TO DISPLAY THE
CONTENTS OF THE LIST");
printf("\n\tENTER 6 --> TO QUIT");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
head=insert_front(item,head);
printf("\nThe list contents after insertion is :\n");
display(head);
break;
case 2:
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
head=insert_rear(item,head);
printf("\nThe list contents after insertion is :\n");
display(head);
break;
case 3:
head=delete_front(head);
printf("\nThe list contents after deletion is :\n");
display(head);
break;
case 4:
head=delete_rear(head);
printf("\nThe list contents after deletion is :\n");
display(head);
break;
case 5:
display(head);
break;
case 6:
k=1;
printf("\nQUITTING FROM THE
PROGRAM!!!");
break;
}
getch();
}
}
head->next=temp;
temp->prev=head;
temp->next=cur;
cur->prev=temp;
return head;
}
void main()
{
NODE head;
int choice,item,k=0;
head=getnode();
head->next=head;
head->prev=head;
while(k!=1)
{
clrscr();
printf("\n\n\n\t\t\t******MAIN MENU*******\n\n\n");
printf("\n\tENTER 1 --> TO INSERT ITEMS FROM
FRONT INTO THE LIST");
printf("\n\tENTER 2 --> TO INSERT ITEMS TO THE
LEFT OF THE KEY");
printf("\n\tENTER 3 --> TO DELETE SPECIFIED
ITEM FROM THE LIST");
printf("\n\tENTER 4 --> TO DISPLAY THE
CONTENTS OF THE LIST");
printf("\n\tENTER 5 --> TO QUIT");
printf("\n\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
head=insert_front(item,head);
printf("\nThe Content of the Double linked list
after insertion is : \n");
display(head);
break;
case 2:
printf("\nEnter the key element to whose left
insertion has to be done : ");
scanf("%d",&item);
flag=0;
head=insert_left(item,head);
if(flag)
{
printf("\nThe Content of the Double linked list
after insertion is : \n");
display(head);
}
break;
case 3:
printf("\nEnter the item to be deleted : ");
scanf("%d",&item);
flag=0;
head=delete_info(item,head);
if(flag)
{
printf("\nThe Content of the Double linked list
after deletion is : \n");
display(head);
}
break;
case 4:
printf("\nThe contents of the Doubly Linked list
is :\n");
display(head);
break;
case 5:
k=1;
printf("\nQUITTING FROM THE
PROGRAM!!!\n\n\nHAVE A NICE DAY");
break;
}
getch();
}
}
5. TREES
Binary tree: A tree with utmost two children for each node.
Complete binary tree: A binary tree in which every level,
except possibly the deepest, is completely filled. At depth
n, the height of the tree, all nodes must be as far left as
possible.
Binary search tree: A binary tree where every node’s left
subtree has keys less than the node's key, and every right
subtree has keys greater than the node's key.
Tree traversal is a technique for processing the nodesof a
tree in some order.
The different tree traversal techniques are Pre-order, In-
order and Post-order traversal.
In Pre-order traversal, the tree node is visited first and the
left subtree is traversed recursively and later right sub-tree
is traversed recursively.
TREE getnode()
{
TREE x;
x=(TREE) malloc(sizeof(struct tree));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return x;
}
TREE insert(int item,TREE root)
{
TREE temp,cur,prev;
char path[20];
int i,len;
temp=getnode();
temp->info=item;
temp->lchild=temp->rchild=NULL;
if(root==NULL)
return temp;
printf("\nEnter the path of insertion : ");
fflush(stdin);
gets(path);
len=strlen(path);
prev=NULL;
cur=root;
for(i=0;i<len&&cur!=NULL;i++)
{
prev=cur;
if(path[i]=='l'||path[i]=='L')
cur=cur->lchild;
else if(path[i]=='r'||path[i]=='R')
cur=cur->rchild;
else
{
printf("\nInvalid path...!!");
free(temp);
return root;
}
}
if(cur!=NULL||i!=len)
{
printf("\nInsertion not possible !...!");
free (temp);
return root;
}
if(path[--i]=='l')
prev->lchild=temp;
else
prev->rchild=temp;
return root;
}
void main()
{
TREE root=NULL;
int choice,item,flag,k=0;
while(k!=1)
{
clrscr();
printf("\n\n\n\t\t\t******MAIN MENU*******");
printf("\n\n\n\tENTER 1 --> TO INSERT INTO THE
TREE");
printf("\n\tENTER 2 --> TO TRAVERSE THE TREE
IN PREORDER");
printf("\n\tENTER 3 --> TO TRAVERSE THE TREE
IN INORDER");
printf("\n\tENTER 4 --> TO TRAVERSE THE TREE
IN POSTORDER");
printf("\n\tENTER 5 --> TO SEARCH FOR AN ITEM
IN THE TREE");
printf("\n\tENTER 6 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
root=insert(item,root);
break;
case 2:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nPreorder traversal is : \n");
preorder(root);
}
break;
case 3:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nInorder traversal is : \n");
inorder(root);
}
break;
case 4:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nPostorder traversal is : \n");
postorder(root);
}
break;
case 5:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nEnter item to be searched : ");
scanf("%d",&item);
flag=0;
search(item,root,&flag);
if(flag==1)
printf("\nSearch successful");
else
printf("\nSearch Unsucessful");
}
break;
case 6:
k=1;
printf("\nTHANK YOU.. HAVE A NICE DAY
QUITTING FROM THE PROGRAM!!!");
break;
}
getch();
}
}
5.2 Implementations
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
#include<process.h>
struct tree
{
int info;
struct tree *lchild;
struct tree *rchild;
};
typedef struct tree *TREE;
TREE getnode()
{
TREE x;
x=(TREE) malloc(sizeof(struct tree));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return x;
}
TREE insert_bst(int item,TREE root)
{
TREE temp,cur,prev;
temp=getnode();
temp->info=item;
temp->lchild=temp->rchild=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item<cur->info)
cur=cur->lchild;
else
cur=cur->rchild;
}
if(item<prev->info)
prev->lchild=temp;
else
prev->rchild=temp;
return root;
}
void main()
{
TREE root=NULL;
int choice,item,flag,k=0;
while(k!=1)
{
clrscr();
printf("\n\n\n\t\t\t******MAIN MENU*******");
printf("\n\n\n\tENTER 1 --> TO INSERT INTO THE
TREE");
printf("\n\tENTER 2 --> TO TRAVERSE THE TREE
IN PREORDER");
printf("\n\tENTER 3 --> TO TRAVERSE THE TREE
IN INORDER");
printf("\n\tENTER 4 --> TO TRAVERSE THE TREE
IN POSTORDER");
printf("\n\tENTER 5 --> TO SEARCH FOR AN ITEM
IN THE TREE");
printf("\n\tENTER 6 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
root=insert_bst(item,root);
break;
case 2:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nPreorder traversal is : \n");
preorder(root);
}
break;
case 3:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nInorder traversal is : \n");
inorder(root);
}
break;
case 4:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nPostorder traversal is : \n");
postorder(root);
}
break;
case 5:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nEnter item to be searched : ");
scanf("%d",&item);
flag=0;
search_bst(item,root,&flag);
if(flag==1)
printf("\nSearch successful");
else
printf("\nSearch Unsucessful");
}
break;
case 6:
k=1;
printf("\nTHANK YOU.. HAVE A NICE DAY
QUITTING FROM THE PROGRAM!!!");
break;
}
getch();
}
}
TREE getnode()
{
TREE x;
x=(TREE) malloc(sizeof(struct tree));
if(x==NULL)
{
printf("\nNO MEMORY!...!");
getch();
exit(1);
}
return x;
}
TREE insert_bst(int item,TREE root)
{
TREE temp,cur,prev;
temp=getnode();
temp->info=item;
temp->lchild=temp->rchild=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(item<cur->info)
cur=cur->lchild;
else
cur=cur->rchild;
}
if(item<prev->info)
prev->lchild=temp;
else
prev->rchild=temp;
return root;
}
void main()
{
TREE root=NULL;
int choice,item,flag,k=0;
while(k!=1)
{
clrscr();
printf("\n\n\n\t\t\t******MAIN MENU*******");
printf("\n\n\n\tENTER 1 --> TO INSERT INTO THE
BINARY SEARCH TREE");
printf("\n\tENTER 2 --> TO FIND THE MAXIMUM
ELEMENT IN THE BINARY SEARCH TREE");
printf("\n\tENTER 3 --> TO FIND THE MINIMUM
ELEMENT IN THE BINARY SEARCH TREE");
printf("\n\tENTER 4 --> TO SEARCH FOR AN ITEM
IN THE BINARY SEARCH TREE");
printf("\n\tENTER 5 --> TO DELETE A SPECIFIED
ITEM FROM BINARY SEARCH THE TREE");
printf("\n\tENTER 6 --> TO TRAVERSE THE
BINARY SEARCH TREE IN PREORDER");
printf("\n\tENTER 7 --> TO TRAVERSE THE
BINARY SEARCH TREE IN INORDER");
printf("\n\tENTER 8 --> TO TRAVERSE THE
BINARY SEARCH TREE IN POSTORDER");
printf("\n\tENTER 9 --> TO QUIT");
printf("\n\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
root=insert_bst(item,root);
break;
case 2:
search_max(root);
break;
case 3:
search_min(root);
break;
case 4:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nEnter item to be searched : ");
scanf("%d",&item);
flag=0;
search_bst(item,root,&flag);
if(flag==1)
printf("\nSearch successful");
else
printf("\nSearch Unsucessful");
}
break;
case 5:
printf("\nEnter the item to be deleted : ");
scanf("%d",&item);
delete_bst(item,root);
break;
case 6:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nPreorder traversal is : \n");
preorder(root);
}
break;
case 7:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nInorder traversal is : \n");
inorder(root);
}
break;
case 8:
if(root==NULL)
printf("\nTree is empty");
else
{
printf("\nPostorder traversal is : \n");
postorder(root);
}
break;
case 9:
k=1;
printf("\nTHANK YOU.. HAVE A NICE DAY
QUITTING FROM THE PROGRAM!!!");
break;
}
getch();
}
}
Selection Sort:
#include<stdio.h>
#include<conio.h>
void selection(int a[],int n)
{
int i,j,temp,sum,pass;
sum=0,pass=0;
for(i=0;i<n-1;i++)
{
pass++;
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
sum++;
}
}
}
printf("\nTotal number of passses = %d",pass);
printf("\nTotal number of exchanges = %d",sum);
}
void main()
{
int a[100],n,i;
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d",&n);
printf("\nEnter %d elements of the array : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a,n);
printf("\nArray in the sorted order is : \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Bubble Sort:
#include<stdio.h>
#include<conio.h>
void bubble(int a[],int n)
{
int i,j,temp,sum,exch,pass,flag;
sum=0,exch=0,pass=0;
for(i=1;i<n;i++)
{
flag=0;
exch=0;
pass++;
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
exch++;
sum++;
flag=1;
}
}
printf("\nNumber of exchanges in pass %d =
%d",pass,exch);
if(flag==0)
break;
getch();
}
printf("\nTotal number of passses = %d",pass);
printf("\nTotal number of exchanges = %d",sum);
}
void main()
{
int a[100],n,i;
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d",&n);
printf("\nEnter %d elements of the array : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a,n);
printf("\nArray in the sorted order is : \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Merge Sort:
#include<stdio.h>
#include<conio.h>
void simple_merge(int a[],int low,int mid,int high)
{
int i,j,k,c[30];
k=i=low;
j=mid+1;
while((i<=mid) && (j<=high))
{
if(a[i]<a[j])
c[k++]=a[i++];
else
c[k++]=a[j++];
}
while(i<=mid)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<=k-1;i++)
a[i]=c[i];
}
Insertion Sort:
#include <stdio.h>
#include<conio.h>
void ins_sort(int a[],int n)
{
int i,j,item;
for(i=0;i<n;i++)
{
item=a[i];
j=i-1;
while(j>=0&&item<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=item;
}
}
void main()
{
int i,n,arr[100];
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d",&n);
printf("\nEnter %d elements ",n);
for(i=0;i<n;i++)
{
printf("\nEnter %d element : ",i+1);
scanf("%d",&arr[i]);
}
printf("\nBefore Sorting : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
ins_sort(arr,n);
printf("\nAfter Sorting : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
getch();
}
Heap Sort:
#include<stdio.h>
#include<conio.h>
create(int a[],int n)
{
int i,k,item,j;
for(k=2;k<=n;k++)
{
item=a[k];
i=k;
j=i/2;
while(j!=0&&item>a[j])
{
a[i]=a[j];
i=j;
j=i/2;
}
a[i]=item;
}
}
void main()
{
int n,i,j,a[20],temp;
clrscr();
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter array elements : \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=n;i>0;i--)
{
create(a,i);
temp=a[1];
a[1]=a[i];
a[i]=temp;
}
printf("sorted array:\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
getch();
}
Quick Sort:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
/* sort everything inbetween `low' <-> `high' */
void quicksort(int arr[], int low, int high)
{
int i=low;
int j=high;
int y=0;
/* compare value */
int z=arr[(low+high)/2];
/* partition */
do
{
/* find element above ... */
while(arr[i]<z)
i++;
/* find element below ... */
while(arr[j]>z)
j--;
if(i<=j)
{
/* swap two elements */
y=arr[i];
arr[i]=arr[j];
arr[j]=y;
i++;
j--;
}
}while(i<=j);
/* recursive call */
if(low < j)
quicksort(arr, low, j);
if(i < high)
quicksort(arr, i, high);
}
void main()
{
int array[SIZE];
int i;
clrscr();
printf("\n\n\n\t******PROGRAM TO SORT THE
ELEMENT OF THE ARRAY USING QUICK
SORT*****");
printf("\n\n\nEnter %d elements into the array ",SIZE);
for(i=0;i<SIZE;i++)
{
printf("\nEnter %d element : ",i+1);
scanf("%d",&array[i]);
}
/* print the original array */
printf("Before quicksort: ");
for(i=0;i<SIZE;i++)
{
printf(" %d ",array[i]);
}
printf("\n");
quicksort(array,0,(SIZE - 1));
/* print the `quicksorted' array */
printf("After quicksort: ");
for(i=0;i<SIZE;i++)
{
printf(" %d ",array[i]);
}
printf("\n");
getch();
}
SHELL SORT
#include<stdio.h>
#include<conio.h>
void shell_sort(int n,int a[])
{
int i,j,depth,item;
for(depth=(n-1)/2;depth>0;depth/=2)
{
for(i=0;i<n;i+=depth)
{
item=a[i];
j=i-depth;
while(j>=0 &&item<a[j])
{
a[j+depth]=a[j];
j=j-depth;
}
a[j+depth]=item;
}
}
}
void main()
{
int i,n,arr[100];
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d",&n);
printf("\nEnter %d elements ",n);
for(i=0;i<n;i++)
{
printf("\nEnter %d element : ",i+1);
scanf("%d",&arr[i]);
}
printf("\nBefore Sorting : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
shell_sort(n,arr);
printf("\nAfter Sorting : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
getch();
}