/* Program to implement a circular queue as a linked list. */


#include

#include

#include

/* structure containing a data part and link part */

struct node

{

int data ;

struct node * link ;

} ;

void addcirq ( struct node **, struct node **, int ) ;

int delcirq ( struct node **, struct node ** ) ;

void cirq_display ( struct node * ) ;

void main( )

{

struct node *front, *rear ;

front = rear = NULL ;

addcirq ( &front, &rear, 10 ) ;

addcirq ( &front, &rear, 17 ) ;

addcirq ( &front, &rear, 18 ) ;

addcirq ( &front, &rear, 5 ) ;

addcirq ( &front, &rear, 30 ) ;

addcirq ( &front, &rear, 15 ) ;

clrscr( ) ;

printf ( “Before deletion:\n” ) ;

cirq_display ( front ) ;

delcirq ( &front, &rear ) ;

delcirq ( &front, &rear ) ;

delcirq ( &front, &rear ) ;

printf ( “\n\nAfter deletion:\n” ) ;

cirq_display ( front ) ;

}

/* adds a new element at the end of queue */

void addcirq ( struct node **f, struct node **r, int item )

{

struct node *q ;

/* create new node */

q = malloc ( sizeof ( struct node ) ) ;

q - > data = item ;

/* if the queue is empty */

if ( *f == NULL )

*f = q ;

else

( *r ) - > link = q ;

*r = q ;

( *r ) - > link = *f ;

}

/* removes an element from front of queue */

int delcirq ( struct node **f, struct node **r )

{

struct node *q ;

int item ;

/* if queue is empty */

if ( *f == NULL )

printf ( “queue is empty” ) ;

else

{

if ( *f == *r )

{

item = ( *f ) - > data ;

free ( *f ) ;

*f = NULL ;

*r = NULL ;

}

else

{

/* delete the node */

q = *f ;

item = q - > data ;

*f = ( *f ) - > link ;

( *r ) - > link = *f ;

free ( q ) ;

}

return ( item ) ;

}

return NULL ;

}

/* displays whole of the queue */

void cirq_display ( struct node *f )

{

struct node *q = f, *p = NULL ;

/* traverse the entire linked list */

while ( q != p )

{

printf ( “%d\t”, q - > data ) ;

q = q - > link ;

p = f ;

}

}


Sorting a Structure on Names using Bubble Sort


Aim: To write a C Program that asks user an ID, name, age and salary of 5 employees. Store all the ID’s into one array and sort the array in asscending order. Then, to display the records in asscending order search for each elements from the array into records and display the corresponding records.

/* Sorting of a structure on names using bubble sort */

#include < stdio.h >
#include < conio.h >

void main( )
Read more »


XML Standards: Relationship between XML and the World Wide Web Consortium


Relationship between XML and the World Wide Web Consortium

The W3C has an active XML Working Group. Microsoft was one of the co-founders of this group in June 1996, and since then numerous industry players have joined, including Netscape Communications Corp, IBM and Oracle…
Read more »


C code to sort a linked list by swapping data


C Program to sort a linked list by swapping data

#include
#include
#include

/* structure containing a data part and link part */
struct node
{
int data ;
struct node *link ;
} *newnode, *start, *visit ;

void getdata( ) ;
void append ( struct node **, int ) ;
void displaylist( ) ;
int count ( struct node * ) ;
void selection_sort ( int ) ;
void bubble_sort ( int ) ;

void main( )
{
int n ;

getdata( ) ;

clrscr( ) ;
printf ( “Linked List Before Sorting: ” ) ;
displaylist( ) ;

n = count ( start ) ;

selection_sort ( n ) ;
printf ( “\nLinked List After Selection Sorting: ” ) ;
displaylist( ) ;
getch( ) ;

getdata( ) ;
clrscr( ) ;
printf ( “Linked List Before Sorting: ” ) ;
displaylist( ) ;

n = count ( start ) ;

bubble_sort ( n ) ;
printf ( “\nLinked List After Bubble Sorting: ” ) ;
displaylist( ) ;
getch( ) ;
}

void getdata( )
{
int val, n ;
char ch ;
struct node *new ;

clrscr( ) ;

new = NULL ;
do
{
printf ( “\nEnter a value: ” ) ;
scanf ( “%d”, &val ) ;

append ( &new, val ) ;

printf ( “\nAny More Nodes (Y/N): ” ) ;
ch = getche( ) ;
} while ( ch == ‘y’ || ch == ‘Y’ ) ;

start = new ;
}

/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
struct node *temp ;
temp = *q ;

if ( *q == NULL ) /* if the list is empty, create first node */
{
*q = malloc ( sizeof ( struct node ) ) ;
temp = *q ;
}
else
{
/* go to last node */
while ( temp - > link != NULL )
temp = temp - > link ;

/* add node at the end */
temp - > link = malloc ( sizeof ( struct node ) ) ;
temp = temp - > link ;
}

/* assign data to the last node */
temp - > data = num ;
temp - > link = NULL ;
}

/* displays the contents of the linked list */
void displaylist( )
{
visit = start ;

/* traverse the entire linked list */
while ( visit != NULL )
{
printf ( “%d “, visit - > data ) ;
visit = visit - > link ;
}
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */
while ( q != NULL )
{
q = q - > link ;
c++ ;
}

return c ;
}

void selection_sort ( int n )
{
int i, j, k, temp ;
struct node *p, *q ;

p = start ;
for ( i = 0 ; i link ;

for ( j = i + 1 ; j data > q - > data )
{
temp = p - > data ;
p - > data = q - > data ;
q - > data = temp ;
}
q = q - > link ;
}
p = p - > link ;
}
}

void bubble_sort ( int n )
{
int i, j, k, temp ;
struct node *p, *q ;

k = n ;
for ( i = 0 ; i link ;

for ( j = 1 ; j data > q - > data )
{
temp = p - > data ;
p - > data = q - > data ;
q - > data = temp ;
}
p = p - > link ;
q = q - > link ;
}
}
}
/*End of C program */


C Code to merge two linked list (restricting common elements to occur only once)


Here’s a handy C Program to merge two linked list, restricting common elements to occur only once

Note: Std Header Declarations Omitted

/* structure containing a data part and link part */
struct node
{
int data ;
struct node *link ;
} ;

void add ( struct node **, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void merge ( struct node *, struct node *, struct node ** ) ;
Read more »


C code to reverse a linked list


C Program to reverse a linked list

#include
#include
#include

/* structure containing a data part and link part */
struct node
{
int data ;
struct node *link ;
} ;

void addatbeg ( struct node **, int ) ;
void reverse ( struct node ** ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;

void main( )
{
struct node *p ;
p = NULL ; /* empty linked list */

addatbeg ( &p, 7 ) ;
addatbeg ( &p, 43 ) ;
addatbeg ( &p, 17 ) ;
addatbeg ( &p, 3 ) ;
addatbeg ( &p, 23 ) ;
addatbeg ( &p, 5 ) ;

clrscr( ) ;
display ( p ) ;
printf ( “\nNo. of elements in the linked list = %d”, count ( p ) ) ;

reverse ( &p ) ;
display ( p ) ;
printf ( “\nNo. of elements in the linked list = %d”, count ( p ) ) ;
}

/* adds a new node at the beginning of the linked list */
void addatbeg ( struct node **q, int num )
{
struct node *temp ;

/* add new node */
temp = malloc ( sizeof ( struct node ) ) ;
temp - > data = num ;
temp - > link = *q ;
*q = temp ;
}

void reverse ( struct node **x )
{
struct node *q, *r, *s ;

q = *x ;
r = NULL ;

/* traverse the entire linked list */
while ( q != NULL )
{
s = r ;
r = q ;
q = q - > link ;
r - > link = s ;
}

*x = r ;
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
printf ( “\n” ) ;

/* traverse the entire linked list */
while ( q != NULL )
{
printf ( “%d “, q - > data ) ;
q = q - > link ;
}
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */
while ( q != NULL )
{
q = q - > link ;
c++ ;
}
return c ;
}

/* End of C program */


C code snippet to add a new node to the asscending order linked list


C Program to add a new node to the ascending order linked list

#include
#include
#include

/* structure containing a data part and link part */
struct node
{
int data ;
struct node *link ;
} ;

void add ( struct node **, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void delete ( struct node **, int ) ;

void main( )
{
struct node *p ;
p = NULL ; /* empty linked list */

add ( &p, 5 ) ;
add ( &p, 1 ) ;
add ( &p, 6 ) ;
add ( &p, 4 ) ;
add ( &p, 7 ) ;

clrscr( ) ;
display ( p ) ;
printf ( “\nNo. of elements in Linked List = %d”, count ( p ) ) ;
}

/* adds node to an ascending order linked list */
void add ( struct node **q, int num )
{
struct node *r, *temp = *q ;

r = malloc ( sizeof ( struct node ) ) ;
r - > data = num ;

/* if list is empty or if new node is to be inserted before the first node */
if ( *q == NULL || ( *q ) - > data > num )
{
*q = r ;
( *q ) - > link = temp ;
}
else
{
/* traverse the entire linked list to search the position to insert the
new node */
while ( temp != NULL )
{
if ( temp - > data link - > data > num ||
temp - > link == NULL ))
{
r - > link = temp - > link ;
temp - > link = r ;
return ;
}
temp = temp - > link ; /* go to the next node */
}
}
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
printf ( “\n” ) ;

/* traverse the entire linked list */
while ( q != NULL )
{
printf ( “%d “, q - > data ) ;
q = q - > link ;
}
}

/* counts the number of nodes present in the linked list */
int count ( struct node *q )
{
int c = 0 ;

/* traverse the entire linked list */
while ( q != NULL )
{
q = q - > link ;
c++ ;
}
return c ;
}
/*End of C program */


Program to maintain a linked list in C


C Program to maintain a linked list

#include
#include

/* structure containing a data part and link part */
struct node
{
int data ;
struct node * link ;
} ;

void append ( struct node **, int ) ;
void addatbeg ( struct node **, int ) ;
void addafter ( struct node *, int, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void delete ( struct node **, int ) ;

void main( )
{
struct node *p ;
p = NULL ; /* empty linked list */

printf ( “\nNo. of elements in the Linked List = %d”, count ( p ) ) ;
append ( &p, 14 ) ;
append ( &p, 30 ) ;
append ( &p, 25 ) ;
append ( &p, 42 ) ;
append ( &p, 17 ) ;

display ( p ) ;

addatbeg ( &p, 999 ) ;
addatbeg ( &p, 888 ) ;
addatbeg ( &p, 777 ) ;

display ( p ) ;

addafter ( p, 7, 0 ) ;
addafter ( p, 2, 1 ) ;
addafter ( p, 5, 99 ) ;

display ( p ) ;
printf ( “\nNo. of elements in the Linked List = %d”, count ( p ) ) ;

delete ( &p, 99 ) ;
delete ( &p, 1 ) ;
delete ( &p, 10 ) ;

display ( p ) ;
printf ( “\nNo. of elements in the Linked List = %d”, count ( p ) ) ;
}
Read more »


C code snippet to perform more string functions


C program for more string functions

#include
#include
#include
#include

int search ( char *, char ) ;
int isequals ( char *, char * ) ;
int issmaller ( char *, char * ) ;
int isgreater ( char *, char * ) ;
char * getsub ( char *, int, int ) ;
char * leftsub ( char *, int n ) ;
char * rightsub ( char *, int n ) ;
void upper ( char * ) ;
void lower ( char * ) ;
void reverse ( char * ) ;
int replace ( char *, char, char ) ;
int setat ( char *, char, int ) ;

void main( )
{
char s1[ ] = “Hello” ;
char s2[ ] = “Hello World” ;
char s3[ ] = “Four hundred thirty two” ;
char ch, *s ;
int i ;

clrscr( ) ;

printf ( “\nString s1: %s”, s1 ) ;

/* check for the first occurrence of a character */
printf ( “\nEnter character to search: ” ) ;
scanf ( “%c”, &ch ) ;
i = search ( s1, ch ) ;
if ( i != -1 )
printf ( “The first occurrence of character %c is found at index no.
%d\n”, ch, i ) ;
else
printf ( “Character %c is not present in the list.\n”, ch ) ;

printf ( “\nString s2: %s”, s2 ) ;

/* compares two strings s1 and s2 */
i = isequals ( s1, s2 ) ;
if ( i == 1 )
printf ( “\nStrings s1 and s2 are identical” ) ;
else
printf ( “\nStrings s1 and s2 are not identical”) ;
i = issmaller ( s1, s2 ) ;
if ( i == 1 )
printf ( “\nString s1 is smaller than string s2″ ) ;
else
printf ( “\nString s1 is not smaller than string s2″ ) ;

i = isgreater ( s1, s2 ) ;
if ( i == 1 )
printf ( “\nString s1 is greater than string s2\n” ) ;
else
printf ( “\nString s1 is not greater than string s2\n” ) ;

/* extract characters at given position */
printf ( “\nString s3: %s”, s3 ) ;
s = getsub ( s3, 5, 7 ) ;
printf ( “\nSub string: %s”, s ) ;
free ( s ) ;

/* extract leftmost n characters */
s = leftsub ( s3, 4 ) ;
printf ( “\nLeft sub string: %s”, s ) ;
free ( s ) ;

/* extract rightmost n characters */
s = rightsub ( s3, 3 ) ;
printf ( “\nRight sub string: %s”, s ) ;
free ( s ) ;

/* convert string to uppercase */
upper ( s3 ) ;
printf ( “\nString in upper case: %s”, s3 ) ;

/* convert string to lowercase */
lower ( s3 ) ;
printf ( “\nString in lower case: %s”, s3 ) ;

/* reverse the given string */
reverse ( s3 ) ;
printf ( “\nReversed string: %s”, s3 ) ;

/* replace first occurrence of one char with new one */
replace ( s1, ‘H’ , ‘M’ ) ;
printf ( “\nString s1: %s”, s1 ) ;

/* sets a char at a given position */
i = setat ( s1, ‘M’, 3 ) ;
if ( i )
printf ( “\nString s1: %s”, s1 ) ;
else
printf ( “\nInvalid position.” ) ;

getch( ) ;
}

/* check for the first occurrence of a character */
int search ( char *str, char ch )
{
int i = 0 ;

while ( *str )
{
if ( *str == ch )
return i ;
str++ ;
i++ ;
}
return -1 ;
}

/* checks whether two strings are equal */
int isequals ( char *s, char *t )
{
while ( *s || *t )
{
if ( *s != *t )
return 0 ;
s++ ;
t++ ;
}
return 1 ;
}

/* checks whether first string is less than second */
int issmaller ( char *s, char *t )
{
while ( *t )
{
if ( *s != *t )
{
if ( *s *t )
return 1 ;
else
return 0 ;
}
s++ ;
t++ ;
}
return 0 ;
}

/* extracts the character at given position */
char * getsub ( char *str, int spos, int n )
{
char *s = str + spos ;
char *t = ( char * ) malloc ( n + 1 ) ;
int i = 0 ;

while ( i < n )
{
t[i] = *s ;
s++ ;
i++ ;
}
t[i] = ” ;

return t ;
}

/* extracts leftmost n characters from the string */
char * leftsub ( char *s, int n )
{
char *t = ( char * ) malloc ( n + 1 ) ;
int i = 0 ;

while ( i < n )
{
t[i] = *s ;
s++ ;
i++ ;
}
t[i] = ” ;

return t ;
}
/* extracts rightmost n characters from the string */
char * rightsub ( char *str, int n )
{
char *t = ( char * ) malloc ( n + 1 ) ;
int l = strlen ( str ) ;
char *s = str + ( l - n ) ;
int i = 0 ;

while ( i = 97 && *s = 65 && *s < = 91 )
*s += 32 ;
s++ ;
}
}

/* reverses a string */
void reverse ( char *str )
{
int l = strlen ( str ) ;
char ch, *t = ( str + l - 1 ) ;
int i = 0 ;

while ( i < l / 2 )
{
ch = *str ;
*str = *t ;
*t = ch ;

str++ ;
t– ;
i++ ;
}
}

/* replaces the first occurrence of char with new char */
int replace ( char *str, char oldch, char newch )
{
while ( *str )
{
if ( *str == oldch )
{
*str = newch ;
return 1 ;
}
str++ ;
}
return 0 ;
}

/* sets a char at a given position */
int setat ( char *str, char ch, int i )
{
if ( i < 0 || strlen ( str ) < i )
return 0 ;
* ( str + i ) = ch ;
return 1 ;
}


C code to allocate memory dynamically for strings, and store their addresses in array of pointers to strings.


C Program to allocate memory dynamically for strings, and store
their addresses in array of pointers to strings.
#include
#include
#include
#include

void main( )
{
char *name[5] ;
char str[20] ;
int i ;

clrscr( ) ; Read more »


C code to search for a string into another string


C Program to search a string in another string

#include
#include
#include

int xstrsearch ( char *, char * ) ;
void show( ) ;

void main( )
{
char s1[ ] = “NagpurKicit” ;
char s2[ ] = “Kicit” ;
int pos ;

clrscr( ) ;

printf ( “String s1: %s\n”, s1 ) ;

printf ( “String s2: %s\n”, s2 ) ;

/* search if s2 is present in s1 */
pos = xstrsearch ( s1, s2 ) ;
printf ( “\nThe pattern string is found at position: %d\n”, pos ) ;

getch( ) ;
}

/* searches for the given pattern s2 into the string s1 */
int xstrsearch ( char * s1, char * s2 )
{
int i, j, k ;
int l1 = strlen ( s1 ) ;
int l2 = strlen ( s2 ) ;

for ( i = 0 ; i < = l1 - l2 ; i++ )
{
j = 0 ;
k = i ;
while ( ( s1[k] == s2[j] ) && ( j < l2 ) )
{
k++ ;
j++ ;
}
if ( j == l2 )
return i ;
}
return -1 ;
}


Code snippet to swap elements of array of pointers to strings.


C Program to swap elements of array of pointers to strings.

#include
#include
#include

#define MAX 6

char *names[MAX] ;
int count ;

int add ( char * ) ;
void swap ( int, int ) ;
void show( ) ;

void main( )
{
int flag ;

clrscr( ) ;

flag = add ( “akshay” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;

flag = add ( “parag” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;

flag = add ( “raman” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;

flag = add ( “srinivas” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;

flag = add ( “gopal” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;

flag = add ( “rajesh” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;
printf ( “\nNames before swapping:\n” ) ;
show ( ) ;

swap ( 2, 3 ) ;
printf ( “\nNames after swapping:\n” ) ;
show ( ) ;

getch( ) ;
}

/* adds given string */
int add ( char *s )
{
if ( count < MAX )
{
names[count] = s ;
count++ ;
return 1 ;
}
else
return 0 ;
}

/* swaps the names at given two positions */
void swap ( int i, int j )
{
char *temp ;
temp = names[i] ;
names[i] = names[j] ;
names[j] = temp ;
}

/* displays the elements */
void show ( )
{
int i ;
for ( i = 0 ; i < count ; i++ )
puts ( names[i] ) ;
}


C Program to check entered name in the master list


C Code to check entered name in the master list

#include
#include
#include

#define MAX1 6
#define MAX2 10

char masterlist[MAX1][MAX2] ;
int count ;

int add ( char *s ) ;
int find ( char *s ) ;

void main( )
{
char yourname[MAX2] ;
int flag ;

clrscr( ) ;
flag = add ( “akshay” ) ;
if ( flag == 0 )
printf ( “\nUnable to add string” ) ;

flag = add ( “parag” ) ;
if ( flag == 0 )
printf ( “\nUnable to add string” ) ;

flag = add ( “raman” ) ;
if ( flag == 0 )
printf ( “\nUnable to add string” ) ;

flag = add ( “srinivas” ) ;
if ( flag == 0 )
printf ( “\nUnable to add string” ) ;

flag = add ( “gopal” ) ;
if ( flag == 0 )
printf ( “\nUnable to add string” ) ;

flag = add ( “rajesh” ) ;
if ( flag == 0 )
printf ( “Unable to add string” ) ;

printf ( “Enter your name: ” ) ;
gets ( yourname ) ;
flag = find ( yourname ) ;
if ( flag == 1 )
printf ( “Welcome, you can enter the palace\n” ) ;
else
printf ( “Sorry, you are a trespasser” ) ;

getch( ) ;
}

/* adds string to the array */
int add ( char *s )
{
if ( count < MAX1 )
{
if ( strlen ( s ) < MAX2 )
{
strcpy ( &masterlist[count][0], s ) ;
count++ ;
return 1 ;
}
}

return 0 ;
}

/* finds the given string */
int find ( char *s )
{
int flag = 0, i ;

for ( i = 0 ; i < count ; i++ )
{
if ( strcmp ( &masterlist[i][0], s ) == 0 )
{
flag = 1 ;
break ;
}
}

return flag ;
}


Code snippet to perform basic operations on string


C program to perform some basic operations on string

#include
#include
#include

int xstrlen ( char * ) ;
void xstrcpy ( char *, char * ) ;
void xstrcat ( char *, char * ) ;
int xstrcmp ( char *, char * ) ;
void show ( char * ) ;

void main( )
{
char s1[ ] = “kicit” ;
char s2[ ] = “Nagpur” ;
char s3[20] ;
int len ;

clrscr( ) ;

printf ( “\nString s1: %s”, s1 ) ;
len = xstrlen ( s1 ) ;
printf ( “\nlength of the string s1: %d”, len ) ;

printf ( “\nString s2: %s”, s2 ) ;

xstrcpy ( s3, s1 ) ;
printf ( “\nString s3 after copying s1 to it: %s”, s3 ) ;

xstrcat ( s3, s2 ) ;
printf ( “\nString s3 after concatenation: %s”, s3 ) ;

if ( xstrcmp ( s1, s2 ) == 0 )
printf ( “\nThe strings s1 and s2 are similar” ) ;
else
printf ( “\nThe strings s1 and s2 are not similar” ) ;

getch( ) ;
}
Read more »


C-Program to multiply two polynomials.


Note: To avoid redundancy from now, i will not be posting the headers like etc, as they’re to be taken for granted.

#define MAX 10

struct term
{
int coeff ;
int exp ;
} ;

struct poly
{
struct term t [10] ;
int noofterms ;
} ;

void initpoly ( struct poly *) ;
void polyappend ( struct poly *, int, int ) ;
struct poly polyadd ( struct poly, struct poly ) ;
struct poly polymul ( struct poly, struct poly ) ;
void display ( struct poly ) ;

void main( )
Read more »


                                      Get custom programming done and find projects at GetAFreelancer.com!