0% found this document useful (0 votes)
98 views10 pages

Program To Implement An Array.

This document contains C code for implementing various operations on data structures like arrays, linked lists, and strings. It includes functions for insertion, deletion, traversal, sorting, merging arrays, and other basic operations on these data structures. The code is organized into multiple sections with comments explaining the purpose and implementation of each function.

Uploaded by

raysubha123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views10 pages

Program To Implement An Array.

This document contains C code for implementing various operations on data structures like arrays, linked lists, and strings. It includes functions for insertion, deletion, traversal, sorting, merging arrays, and other basic operations on these data structures. The code is organized into multiple sections with comments explaining the purpose and implementation of each function.

Uploaded by

raysubha123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

/* Program to implement an array. */


#include <stdio.h>
#include <conio.h>
#define MAX 5
void insert ( int *, int pos, int num ) ;
void del ( int *, int pos ) ;
void reverse ( int * ) ;
void display ( int * ) ;
void search ( int *, int num ) ;
void main( )
{
int arr[5] ;
clrscr( ) ;
insert ( arr, 1, 11 ) ;
insert ( arr, 2, 12 ) ;
insert ( arr, 3, 13 ) ;
insert ( arr, 4, 14 ) ;
insert ( arr, 5, 15 ) ;
printf ( "\nElements of Array: " ) ;
display ( arr ) ;
del ( arr, 5 ) ;
del ( arr, 2 ) ;
printf ( "\n\nAfter deletion: " ) ;
display ( arr ) ;
insert ( arr, 2, 222 ) ;
insert ( arr, 5, 555 ) ;
printf ( "\n\nAfter insertion: " ) ;
display ( arr ) ;
reverse ( arr ) ;
printf ( "\n\nAfter reversing: " ) ;
display ( arr ) ;
search ( arr, 222 ) ;
search ( arr, 666 ) ;
getch( ) ;
}
/* inserts an element num at given position pos */
void insert ( int *arr, int pos, int num )
{
/* shift elements to right */
int i ;
for ( i = MAX - 1 ; i >= pos ; i-- )
arr[i] = arr[i - 1] ;
arr[i] = num ;
}
/* deletes an element from the given position pos */
void del ( int *arr, int pos )
{
/* skip to the desired position */
int i ;
for ( i = pos ; i < MAX ; i++ )
arr[i - 1] = arr[i] ;

2
arr[i - 1] = 0 ;
}
/* reverses the entire array */
void reverse ( int *arr )
{
int i ;
for ( i = 0 ; i < MAX / 2 ; i++ )
{
int temp = arr[i] ;
arr[i] = arr[MAX - 1 - i] ;
arr[MAX - 1 - i] = temp ;
}
}
/* searches array for a given element num */
void search ( int *arr, int num )
{
/* Traverse the array */
int i ;
for ( i = 0 ; i < MAX ; i++ )
{
if ( arr[i] == num )
{
printf ( "\n\nThe element %d is present at %dth position.", num,
i+1);
return ;
}
}
if ( i == MAX )
printf ( "\n\nThe element %d is not present in the array.", num ) ;
}
/* displays the contents of a array */
void display ( int *arr )
{
/* traverse the entire array */
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i < MAX ; i++ )
printf ( "%d\t", arr[i] ) ;
}
/*Program to merge two 1-D arrays. */
#include <stdio.h>

3
#include <conio.h>
#include <alloc.h>
#define MAX1 5
#define MAX2 7
int *arr ;
int* create ( int ) ;
void sort ( int *, int ) ;
void display ( int *, int ) ;
int* merge ( int *, int * ) ;
void main( )
{
int *a, *b, *c ;
clrscr( ) ;
printf ( "\nEnter elements for first array: \n\n" ) ;
a = create ( MAX1 ) ;
printf ( "\nEnter elements for second array: \n\n" ) ;
b = create ( MAX2 ) ;
sort ( a, MAX1 ) ;
sort ( b, MAX2 ) ;
printf ( "\nFirst array: \n" ) ;
display ( a, MAX1 ) ;
printf ( "\n\nSecond array: \n" ) ;
display ( b, MAX2 ) ;
printf ( "\n\nAfter Merging: \n" ) ;
c = merge ( a, b ) ;
display ( c, MAX1 + MAX2 ) ;
getch( ) ;
}
/* creates array of given size, dynamically */
int* create ( int size )
{
int *arr, i ;
arr = ( int * ) malloc ( sizeof ( int ) * size ) ;
for ( i = 0 ; i < size ; i++ )
{
printf ( "Enter the element no. %d: ", i + 1 ) ;
scanf ( "%d", &arr[i] ) ;
}
return arr ;
}
/* sorts array in ascending order */
void sort ( int *arr, int size )
{

4
int i, temp, j ;
for ( i = 0 ; i < size ; i++ )
{
for ( j = i + 1 ; j < size ; j++ )
{
if ( arr[i] > arr[j] )
{
temp = arr[i] ;
arr[i] = arr[j] ;
arr[j] = temp ;
}
}
}
}
/* displays the contents of array */
void display ( int *arr, int size )
{
int i ;
for ( i = 0 ; i < size ; i++)
printf ( "%d\t", arr[i] ) ;
}
/* merges two arrays of different size */
int* merge ( int *a, int *b )
{
int *arr ;
int i, k, j ;
int size = MAX1 + MAX2 ;
arr = ( int * ) malloc ( sizeof ( int ) * ( size ) ) ;
for ( k = 0, j = 0, i = 0 ; i <= size ; i++ )
{
if ( a[k] < b[j] )
{
arr[i] = a[k] ;
k++ ;
if ( k >= MAX1 )
{
for ( i++ ; j < MAX2 ; j++, i++ )
arr[i] = b[j] ;
}
}
else
{

5
arr[i] = b[j] ;
j++ ;
if ( j >= MAX2 )
{
for ( i++ ; k < MAX1 ; k++, i++ )
arr[i] = a[k] ;
}
}
}
return arr ;
}
/* Program to perform some basic operations on string. */
#include <stdio.h>
#include <conio.h>
#include <string.h>
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( ) ;
}
/* finds the length of the string */
int xstrlen ( char *s )
{
int l = 0 ;
while ( *s )
{
l++ ;
s++ ;
}
return l ;
}
/* copies source string s to the target string t */
void xstrcpy ( char *t, char *s )
{
while ( *s )
{
*t = *s ;
t++ ;
s++ ;
}
*t = '\0' ;
}
/* concatenates the two strings */
void xstrcat ( char *t, char *s )
{
while ( *t )
t++ ;
while ( *s )
*t++ = *s++ ;
*t = '\0' ;
}
/* compares two strings s and t for equality */
int xstrcmp ( char *s, char *t )
{
while ( *s == *t )
{
if ( ! ( *s ) )
return 0 ;
s++ ;
t++ ;
}

7
return ( *s - *t ) ;
}
/* Program to maintain a linked list */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
/* 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 ) ) ;
}
/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
struct node *temp, *r ;
if ( *q == NULL ) /* if the list is empty, create first node */
{
temp = malloc ( sizeof ( struct node ) ) ;

8
temp -> data = num ;
temp -> link = NULL ;
*q = temp ;
}
else
{
temp = *q ;
/* go to last node */
while ( temp -> link != NULL )
temp = temp -> link ;
/* add node at the end */
r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = NULL ;
temp -> link = r ;
}
}
/* 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 ;
}
/* adds a new node after the specified number of nodes */
void addafter ( struct node *q, int loc, int num )
{
struct node *temp, *r ;
int i ;
temp = q ;
/* skip to desired portion */
for ( i = 0 ; i < loc ; i++ )
{
temp = temp -> link ;
/* if end of linked list is encountered */

9
if ( temp == NULL )
{
printf ( "\nThere are less than %d elements in list", loc ) ;
return ;
}
}
/* insert new node */
r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = temp -> link ;
temp -> link = 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 ;
}
/* deletes the specified node from the linked list */
void delete ( struct node **q, int num )
{
struct node *old, *temp ;
temp = *q ;
while ( temp != NULL )
{

10
if ( temp -> data == num )
{
/* if node to be deleted is the first node in the linked list */
if ( temp == *q )
*q = temp -> link ;
/* deletes the intermediate nodes in the linked list */
else
old -> link = temp -> link ;
/* free the memory occupied by the node */
free ( temp ) ;
return ;
}
/* traverse the linked list till the last node is reached */
else
{
old = temp ; /* old points to the previous node */
temp = temp -> link ; /* go to the next node */
}
}
printf ( "\nElement %d not found", num ) ;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy