DS Minor1 StudyDoc
DS Minor1 StudyDoc
B. V. Bhoomaraddi.
College of Engineering
and Technology
Overview
• Introduction
• Declaring pointers
• Pointer variables
• Pointer expression and arithmetic
• Passing arguments to functions using pointers
• Pointers and arrays
• Passing an array to a function
17/04/2023 2
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
INTRODUCTION
17/04/2023 3
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Introduction
• Each variable in a program occupies a part of
the computer’s memory:-
– An integer variable occupies 2 bytes of memory
– A character variable occupies 1 byte
– A float occupies 4 bytes
– An array[4][2] of integers occupies 8 * 2 = 16 bytes
• Since data (int/char/float/double) is stored in
memory, it has an address of storage
associated with it
17/04/2023 4
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Address of a variable
• The location or the chunk of memory used to
store a variable is called the address of that
variable.
• An address is some kind of number similar to
house numbers in a street that is used to
locate the information stored in that particular
variable
• Where do we refer to the address of a
variable?
17/04/2023 K. L. E. Technological University, Hubli-31.
5
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
04/17/2023 6
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Memory representation
int i=150; address of i 0x1056
0x1057 150
17/04/2023 8
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
*
int ptr; /* defines a pointer to an integer variable*/
ptr= &i; /* assign the address of i to pointer */
printf(”%d”, *ptr); /* prints contents of variable i */
17/04/2023 K. L. E. Technological University, Hubli-
31.
11
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
float *ptr;
value
ptr=&value;
printf(”%d”, *ptr); 2 bytes
0x2034 1054
ptr
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
16
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Remember...
• Assign the address of a variable to a pointer
using the & (address of operator)
• The type of variable and pointer must be same
• Declare target variable before declaring the
pointer to it
• When *(indirection operator) is placed before
the pointer, the pointer returns the value of
the variable it points to.
Time: 10 minutes
Activity 3: Write printf stmts Type: Individual
1. Print the address of quantity in two ways
a) Using Pointer to quantity
b) Using the address of operator for quantity
2. Print the value of quantity in three ways
a) Using Pointer to quantity
b) Using the variable quantity int quantity, *p, n;
c) Use the regular variable quantity = 179;
3. What is printed for:- p = &quantity;
a) printf(“%p\n”, *&p);
n = *p;
b) printf(“%p\n”, &*p);
19
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
quantity = 179;
p = &quantity;
n = *p;
• Address of quantity
– printf(“address of quantity = %p\n”, &quantity);
– printf(“address of quantity = %p\n”, p);
• Value of quantity
– printf(“Value of quantity = %d\n”, quantity);
– printf(“address of quantity = %d\n”, *p);
– printf(“address of quantity = %d\n”, n);
17/04/2023 20
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Time: 15 minutes
Activity 4 Type: Individual
21
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Activity 4
a) What value is assigned to &a?
b) What value is assigned to &b?
c) What value is assigned to &c;
d) What is the value assigned to pa?
e) What is the value assigned to *pa?
f) What value is represented by & (*pa)?
g) What value is assigned to pb?
h) What value is represented by *pb?
i) What value is assigned to c?
22
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Pointer expressions
• It is possible to add integers to pointers or
subtract integers from pointers as well as to
subtract one pointer from the other pointer.
• We can compare pointers by using relational
operators in the expressions. For example
p1 > p2 , p1==p2 and p1!=p2 are all valid in C.
17/04/2023 24
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
PASSING ARGUMENTS TO
FUNCTIONS USING POINTERS
17/04/2023 K. L. E. Technological University, Hubli-
31.
26
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Example 1
Rule1: void subtract(int *n1, int *n2, int *r);
Rule2: subtract(&n1, &n2, &r);
Rule3:
void subtract(int *a1, int *a2, int *a3)
{
*a3 = *a1 - *a2;
}
31
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Practise Programs
1. Program to calculate area of circle using
pointers
2. Program to add, subtract, multiply and divide
numbers using pointers
3. Program to find the biggest of three numbers
using pointers
4. Swap two numbers HOMEWORK
5. Determine GCD and LCM of two numbers.
HOMEWORK
17/04/2023 32
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
17/04/2023 33
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
17/04/2023 36
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
1 int SUM(void)
2 { int i, n;
3 int arr[10], *parr;
4 parr = arr;
5 printf(“\n Enter the number of elements : “);
6 scanf(“%d”, &n);
7 for(i=0; i < n; i++)
8 scanf(“%d”, (parr+i));
9 for(i=0; i < n; i++)
10 printf(“\n arr[%d] = %d”, i, *(parr+i));
}
17/04/2023 K. L. E. Technological University, Hubli-
31.
37
KLE Society’s
B. V. Bhoomaraddi.
College of Engineering
and Technology
Structures
II Semester
18ECSP102– Problem Solving with Data Structures
Structures
2021-2022
1
Structures
Content
Introduction to Structures
Defining Structures
Declaring Structure variables
Accessing members of Structure
Structures with functions
Array of Structures
Pointers to Structures
2
Structures
Class Task- 1
Structure
Structure is a user defined data type in C.
It is used to group heterogeneous items into a single data type.
Declaring a structure:
struct <tag_name>
{
data_type member1; Each attribute
is a member of
data_type member2; structure.
…
};
Structures
};
Structures
Class Task-2
Write a structure for your mobile.
1. Identify the attributes .
2. Group all the attributes.
3. Give a name to the group.
4. Identify the appropriate data types for the above listed attributes.
5. Create a structure for your mobile.
Structures
Task
Structure Variable(s):
Ex:Mobile
Method 2 : Declare Variables using
Method 1 : Immediately after Structure struct Keyword
Template
struct mobile struct mobile
{ {
char model_name[10]; char model_name[10];
float price; float price;
char os_name[20]; char os_name[20];
float os_version; float os_version;
}samsung; };
struct mobile samsung;
Declaring Multiple Structure Variables
Declaring Multiple Structure Variables struct mobile
struct mobile {
{ char model_name[10];
char model_name[10]; float price;
float price; char os_name[20];
char os_name[20]; float os_version;
float os_version;
}samsung, nokia, htc; };
struct mobile samsung, nokia,
htc;
Structures
Class task-3
structure_variable.member
Structures
struct cricket_player
{ Order of
char player_name[20]; initialization
char team_name[20]; must map with
float average; order of
int highest_score; member
int centuries; declarations in the
int ODI_rank; structure
}p1= {“Virat”, “INDIA”, 59.76,183, 39, 1};
Structures
Class Task-4
• Initialize all the members of cell_phone structure.
Structures
Aliasing
Registered name of Cricket player may have:
firstname, middlename and last name.
typedef Structures
{ {
… float average;
typedef Structures
Class Task-5
• Create a structure for cell-phone and pass the individual structure
elements to the function
Structures
Class Task-6
• Convert the previously discussed Cricket_player structure
into modular C program to read the details, store and display
them on screen.
Structures
Exercises
1)Calculate difference between two time periods.
3) Write a program to add two distances in inch-feet using structure. the values of
the distances is to be taken from the user.
How can you create a Cricket Team for World Cup 2021?
Structures
Array of Structures
struct cricket_player
{
char player_name[20];
char team_name[20];
float average;
int highest_score;
int centuries;
int ODI_rank;
};
struct team player[11];
Structures
0 1 2 … 98 99
An array of structures: Multiple types of data in each array element.
0 1 2 … 98 99
Structures
Class Task-7
Write a modular C program to store details of 10 students and list the details of
the student who scored maximum marks.
Structures
Summary
2) Write a program to store movie details such as name, producer, director , release year and
production house .
a) sort the structure using release year.
b) display structures using director.
c) display structures using production house.
Structures
Nested Structure
Nested structure in C is nothing but structure within structure.
struct structure1
{
datatype member1;
datatype member2;
…
};
struct structure2
{
datatype member1;
datatype member2;
…
struct structure1 obj;
};
Structures
struct player {
}; }
main() {
struct achievements {
struct player p, *p1;
{
p1=&p;
int man_of_matches; read (p1);
int man_of_series; display (p1);
int no_of_centuries; }
}; void read( struct player *p1)
{
struct player
printf ("Enter cricket team name , ODI rank, and achievements – Man of matches,
{ Man of the series and number of centuries \n");
char team_name[20]; scanf(" %s%d%d%d%d", p1->team_name, &p1->ODI_rank, p1->achieved.
man_of_matches, &p1->achieved. man_of_series, &p1->achieved. no_of_centuries);
int ODI_rank;
}
// structure within structure void display( struct player *p1)
struct achievements achieved; {
}; printf(" team name is: %s\n ODI rank is : %d\n man of the Matches: %d\n Man
of the Series: %d\n Number of Centuries: %d\n ", p1-> team_name, p1-
void read( struct player *); >ODI_rank, p1->achieved. man_of_matches, p1-> achieved. man_of_series, p1-
>achieved. no_of_centuries);
void display( struct player *);
}
Structures
Ex:Structure
Nested Mobile Example Using Pointers
Ex: Mobile mob_data_ptr = &mob_data;
{ display(mob_data_ptr);
float app_version; }
char app_name[20]; void read( struct mobile *mob_data)
} {
struct mobile printf ("Enter mobile version , name and app version, name \n");
{ scanf(" %f%s%f%s", &mob_data->mob_version, mob_data-
>mob_name, &mob_data->app_data.app_version, mob_data-
float mob_version; >app_data.app_name);
char mob_name[50]; }
// structure within structure
void display( struct mobile *mob_data)
struct application app_data;
{
};
printf(" mobile version is: %d\n mobile name is: %s\n app version
void read( struct mobile*); is: %d\n app name is: %s \n ", mob_data->mob_version, mob_data-
void display( struct mobile *); >mob_name, mob_data->app_data.app_version, mob_data-
>app_data.app_name);
main()
}
{
struct mobile mob_data, *mob_data_ptr;
Structures
2. Design a modular C program to use typedef structure for mobile strucutre, store data into
members and display them on screen.
KLE Technological University
II Semester
18ECSP102– Problem Solving with Data Structures
2021-2022
1
Dynamic Memory Allocation
Static memory
Static memory
Advantages:
Static allocation is done at compile time when you know the size of
the array.
The memory size allocated to “data” is static. But it is possible to
change content of a static structure without increasing the memory
space allocated to it.
Global variables are declared “ahead of time,” such as fixed array.
Lifetime of Static allocation is the entire runtime of program.
It has efficient execution time.
Dynamic Memory Allocation
Static memory
Disadvantages:
In case more static data space is declared than needed, there is waste of
space.
In case less static space is declared than needed, then it becomes impossible
to expand this fixed size during run time.
Dynamic Memory Allocation
malloc()
“malloc” or “memory allocation” method in C is used to dynamically allocate
a single large block of memory with the specified size.
Returns a pointer of type void which can be cast into a pointer of any form.
ptr = (cast-type*) malloc(byte-size);
Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, malloc will allocate 400 bytes of memory.
However, if the space is insufficient, allocation fails and returns a NULL
pointer.
Dynamic Memory Allocation
malloc()
Dynamic Memory Allocation
calloc()
“calloc” or “contiguous allocation” method in C is used to dynamically
allocate the specified number of blocks of memory of the specified type. It
initializes each block with a default value ‘0’.
ptr = (cast-type*)calloc(n, element-size);
Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each
with the size of the float
If space is insufficient, allocation fails and returns a NULL pointer.
Dynamic Memory Allocation
calloc()
Dynamic Memory Allocation
realloc()
The function realloc use to change the size of the memory block and it alter the
size of the memory block without loosing the old data, it is called reallocation of
memory.
ptr = realloc(ptr, x);
Dynamic Memory Allocation
free()
free(ptr);
Dynamic Memory Allocation
free()
Dynamic Memory Allocation
Dynamic memory
Advantages:
Data structures can grow Dynamic Allocation is done at run time.
Shrink to fit changing data requirements.
We can allocate (create) additional storage whenever we need them.
We can de-allocate (free/delete) dynamic space whenever we are done
with them.
We can always have exactly the amount of space required - no more,
no less.
Disadvantages:
As the memory is allocated during runtime, it requires more time.
Memory needs to be freed by the user when done. This is important as
it is more likely to turn into bugs that are difficult to find.
Dynamic Memory Allocation
Thank You
KLE Technological University
Files
II Semester
18ECSP102– Problem Solving with Data Structures
Files
2021-2022
1
Files
Introduction
Directory files
Different types of file store different types of information.
Files
Types of files
sequentially.
1. Create the stream via a pointer variable using the FILE structure.
Ex: FILE *p;
2. Open the file, associating the stream name with the file name.
3. Read or write the data.
4. Close the file.
Files
File Functions
Files
Naming a file
file is identified by its name.
This name is divided into two parts
File Name
It consists of alphabets and digits.
Special characters are also supported, but it depends on the operating
system we use.
Extension
It describes the file type
Files
File Pointer
The file pointer is a pointer variable which can be store the address of a
special file that means it is based upon the file pointer a file gets opened.
Declaration of a file pointer:-
FILE* var;
Note:
The last byte of a file contains the end-of-file character (EOF),
While reading a text file, the EOF character can be checked to know the
end.
Files
File Modes
Mode Meaning
r • Open a text file for reading only. If the file doesn’t exist, it returns null.
w • Opens a file for writing only.
• If file exists, than all the contents of that file are destroyed and new fresh blank
file is copied on the disk and memory with same name
• If file dosen’t exists, a new blank file is created and opened for writing.
• Returns NULL if it is unable to open the file
File Modes
Files
Additional Modes
r+ :open for reading and writing, start at beginning
w+ : open for reading and writing (overwrite file)
a+ :open for reading and writing (append if fileexists)
Three special file streams are defined in the <stdio.h> header
– stdin :reads input from the keyboard
– stdout :send output to the screen
– stderr :prints errors to an error device (usually also the screen)
Files
Opening a file
Before performing any type of operation, a file must be opened and for this
fopen() function is used.
Closing a file
To close a file and dis-associate it with a stream (file pointer), use fclose()
function.
fclose(FILE *fp)
fclose() returns 0 if the file is closed successfully.
In order to mark the end of a text file, a special character EOF is stored at the
end.
Files
getc()
putc()
putc (variable,file pointer);
write a single character to the output file, pointed to by fp.
Example:
FILE *fp;
char ch;
putc (ch,fp);
Files
fprintf()
fprintf (fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(“out.txt”,”w”);
fprintf (fp, "%d %f %c", i, x, ch);
Files
fscanf()
fscanf (fp,"string",identifiers);
Example:
FILE *fp;
fp=fopen(“input.txt”,”r”);
int i;
fscanf (fp,“%d",i);
Files
Application of files
Mr. Ram works in KLETU as a covid task force manager and wants to know
the student list who have taken the covid vaccination. To find out the student
list, he uses a file to store the students' information and then print the
student list who have taken the covid vaccination. Please help Mr. Ram to
solve the problem.
Files
Thank You
Chapter - 1
Introduction to Data Structures
(Sample Programs)
Syllabus
• Pointers
• Structures
• Files
1 Pointers
1.1 Pointer: Declaration, Initialization, Accessing
1 # include < stdio .h >
2
3 int main ()
4 {
5 int x1 =2 , x2 =3 , y1 =0 , y2 = -23;
6
7 int * p1 , * p2 ;
8
9 int * q1 , q2 ;
10
11 p1 = & x1 ;
12 p2 = & x2 ;
13
14 q1 = & y1 ;
15 q2 = & y2 ;
16
17 printf ( " \ nx1 =% d " ,* p1 ) ;
18 printf ( " \ nx2 =% d " ,* p2 ) ;
19 printf ( " \ ny1 =% d " ,* q1 ) ;
20 printf ( " \ ny2 =% d " ,* q2 ) ;
21
22 return 0;
23 }
31 else
32 {
33 if (* p2 > * p3 )
34 {
35 printf ( " % d is the largest number " , * p2 ) ;
36 }
37 else
38 {
39 printf ( " % d is the largest number " , * p3 ) ;
40 }
41 }
42
43 return 0;
44 }
3 int main ()
4 {
5 int num1 =2 , num2 =3 , sum =0 , mul =0 , div =1;
6 int * ptr1 , * ptr2 ;
7
8 ptr1 = & num1 ;
9 ptr2 = & num2 ;
10
11 sum = * ptr1 + * ptr2 ;
12
13 mul = sum * * ptr1 ;
14
15 * ptr2 +=1;
16
17 div = 9 + * ptr1 / * ptr2 - 30;
18
19 printf ( " \ nsum =% d " , sum ) ;
20 printf ( " \ nproduct =% d " , mul ) ;
21 printf ( " \ ndiv =% d " , div ) ;
22
23 return 0;
24 }
8 int main ()
9 {
10 float radius , area ;
11
12 printf ( " \ nEnter the radius of Circle >\ t " ) ;
13 scanf ( " % f " , & radius ) ;
14
15 // area = 3.14 * radius * radius ;
16 area (& radius , & area ) ;
17 printf ( " \ nArea of Circleis >\ t % f " , area ) ;
18
19 return 0;
20 }
31
32 printf ( " \ nElements for array b >\ t " ) ;
33 for ( i =0; i < n ; i ++)
34 printf ( " % d " , *( b + i ) ) ;
35
36 return 0;
37 }
2 Structures
2.1 Structures: Declaration, Initialization, Accessing
1 # include < stdio .h >
2 # include < string .h >
3
4 // Structure Definition
5 struct stud
6 {
7 char name [20];
8 int roll ;
9 float marks ;
10 };
11
12 int main ()
13 {
14 // Structure Declaration
15 struct stud s ;
16
17 // Accessing and Initializing Members
18 strcpy ( s . name , " I am Don " ) ;
19 s . roll = 420;
20 s . marks = 0;
21
22 printf ( " \ nStudent details are > " ) ;
23 printf ( " \ nNAME =% s \ tROLL =% d \ tMARKS =% f " , s . name , s . roll , s . marks ) ;
24
25 return 0;
26 }
17 // 2 nd variable
18 e2 . id =102;
19 strcpy ( e2 . name , " VTU " ) ;
20
21 printf ( " \ n1st employee id > % d " , e1 . id ) ;
22 printf ( " \ n1st employee name > % s \ n " , e1 . name ) ;
23
24 printf ( " \ n2nd employee id > % d \ n " , e2 . id ) ;
25 printf ( " \ n2nd employee name > % s \ n " , e2 . name ) ;
26
27 return 0;
28 }
22 return 0;
23 }
8
9 // Function Declaration : structure as parameter
10 void display ( struct stud ) ;
11
12 int main ()
13 {
14 struct stud s ;
15
16 printf ( " \ nEnter name , roll , marks > " ) ;
17 scanf ( " % s % d % f " ,s . name , & s . roll , & s . marks ) ;
18
19 display ( s ) ;
20
21 return 0;
22 }
23
24 void display ( struct stud t )
25 {
26 printf ( " \ nStudent details are > " ) ;
27 printf ( " \ nNAME =% s \ tROLL =% d \ tMARKS =% f " , t . name , t . roll , t . marks ) ;
28
29 return ;
30 }
36 }
16 x = read () ;
17
18 printf ( " \ nName > % s " , x . name ) ;
19 printf ( " \ nRoll no > % d " , x . roll ) ;
20 printf ( " \ nMarks > % f " , x . marks ) ;
21
22 return 0;
23 }
24
25 struct stud read ()
26 {
27 struct stud s ;
28 printf ( " \ nEnter name , roll and marks > " ) ;
29 scanf ( " % s % d % f " , s . name , & s . roll , & s . marks ) ;
30
31 return s ;
32 }
46
47 printf ( " \ nAverage runs > " ) ;
48 scanf ( " %0.2 f " , & c . pstats . average ) ;
49
50 return c ;
51 }
52
53 void display ( struct cricketer c )
54 {
55 printf ( " \ nName > % s " , c . name ) ;
56 printf ( " \ nAge > % d " , c . age ) ;
57 printf ( " \ nMatches > % d " , c . pstats . matches ) ;
58 printf ( " \ nAverage runs > % f " , c . pstats . average ) ;
59 }
4 struct colDetail
5 {
6 int cid ;
7 char cname [50];
8 };
9
10 struct studDetail
11 {
12 int id ;
13 char name [20];
14 float percentage ;
15
5 {
6 char * p ;
7 int n ;
8
9 printf ( " \ nWhat is the length of your string > " ) ;
10 scanf ( " % d " , & n ) ;
11
12 // allocate memory dynamically
13 p = ( char *) malloc ( n * sizeof ( char ) ) ;
14
15 if ( p == NULL )
16 {
17 printf ( " \ nError - unable to allocate required memory " ) ;
18 exit (101) ;
19 }
20 else
21 {
22 printf ( " \ nEnter the string > " ) ;
23 scanf ( " % s " , p ) ;
24 }
25
26 printf ( " \ nYour dynamically allocated string is % s " , p ) ;
27
14 if ( p == NULL )
15 {
16 printf ( " Error ! unable to allocate memory " ) ;
17 exit (101) ;
18 }
19
31 return 0;
32 }
33 free ( p ) ;
34
35 return 0;
36 }
4 File Management
4.1 File Read through – fgetc
1 # include < stdio .h >
2
3 int main ()
4 {
5 FILE * fp ;
6 char ch ;
7
8 fp = fopen ( " 1. c " ," r " ) ;
9
10 while ( 1 )
11 {
12 ch = fgetc ( fp ) ;
13 if ( ch == EOF )
14 break ;
15 printf ( " % c " , ch ) ;
16 }
17
18 fclose ( fp ) ;
19
20 return 0;
21 }
25 fclose ( fp ) ;
26
27 printf ( " \ nData successfully read from file test . txt " ) ;
28 printf ( " \ nThe file is now closed . " ) ;
29
30 return 0;
31 }
21
22 fclose ( fp ) ;
23
24 printf ( " \ nData successfully written in file test . txt " ) ;
25 printf ( " \ nThe file is now closed . " ) ;
26
27 return 0;
28 }
Problem Statement: Varun Finchi is a manger of BCR franchise team, where he has to
select 3 players to make squad full for playing in PPL League. He contacts Lord Hassan, an
analyst of BCR team, where he has all the player details. Varun Finchi selects the players as
(1) who have highest runs in their career, (2) one who has least runs in his career and (3) one
more player who has name “Rajat”. Please help Varun Finchi to select players for BCR team.
• To select the highest and lowest scoring player we have to sort the players.
• To select “Rajat” player we have to search the player from the pool.
1.2 Ambiguity
Yes, there are two ambiguity in the question. First one is how many players are there in the
pool?. Second one is which are the other player details to consider, apart from runs?
We remove the ambiguity in assumption section.
1.4 Known
• There is pool of players.
• Every player has some details, including runs scored in his career.
1.5 Unknowns
• Highest run scorer.
1.6 Assumptions
For the given problem as there are two ambiguities, we thus have to make two assumptions.
• For first ambiguity, we assume that user will give the pool size.
• For second ambiguity, we assume that we will take 3 player details: name, jersey number
and runs scored.
1.7 Constraints
Player name Rajat has to be selected. Thus, he has to be there in the player pool. Also, pool
should have a minimum of 3 players.
• selectPlayers() ← To copy the selected players from pool structure to selected structure.
2. Select the first and last player from sorted list, to obtain the highest and lowest scoring
player.
3. Search the player pool from rajat and copy his details to the selected list.
13 STEP-3: Copy the highest and lowest player details to selected structure.
14 selected[0] ← players[0]
15 selected[1] ← players[1]
16 STEP-4: Search for player named “Rajat” and copy the details to selected structure.
// Linear Search
17 for i ← 0 to n − 1 do
18 if strcmp(players[j].name , “Rajat”) is ture? then
19 selected[2] ← Rajat player details.
20 STEP-5: Display the selected player details.
21 STEP-6: END
5 Lessons Learnt
The BCR team manager’s problem is solved using the sorting and searching algorithms.
Through descending order sort, we can obtain the players with highest runs scorers to low-
est runs scorers. Therefore we select find and last index players. We perform linear search
for player name with Rajat. Therefore we select and display the selected 3 players as per the
manager’s needs.
6 Solution Documentation
Enter number of player details to read > 7
Enter Player name, jersey number, runs scored
virat 1 40
dhoni 2 50
rohit 3 29000
ashwin 12 12000
ishant 34 2554
raina 20 12455
Rajat 10 4832
The sorted players from pool are:
NAME JERSEY RUNS
———————————-
rohit 3 29000
raina 20 12455
ashwin 12 12000
ishant 34 2554
dhoni 2 50
The selected players are:
NAME JERSEY RUNS
———————————-
rohit 3 29000
Rajat 10 4832
virat 1 40
7 C Program
7.1 Without Using Functions
1 # include < stdio .h >
2 # include < string .h >
3
4 // Structure Definition
5 struct player
6 {
7 char name [20];
8 int jerseyNo , runs ;
9 };
10
11 int main ()
12 {
13 struct player pool [10] , selected [10] , temp ;
14 int i , n , j ;
15
16 printf ( " \ nEnter no . of players > " ) ;
17 scanf ( " % d " ,& n ) ;
18
19 for ( i = 0; i < n ; i ++ )
20 {
21 printf ( " \ nEnter player % d details ( name , jersey number , runs ) >\ t " ,i
+1) ;
22 scanf ( " % s % d % d " , pool [ i ]. name , & pool [ i ]. jerseyNo , & pool [ i ]. runs ) ;
23 }
24
25 // Bubble Sort
26 for ( i = 0; i < n ; i ++ )
27 {
28 for ( j = 0; j < n -i -1; j ++ )
29 {
30 if ( pool [ j ]. runs < pool [ j +1]. runs )
31 {
32 temp = pool [ j ];
33 pool [ j ] = pool [ j +1];
34 pool [ j +1] = temp ;
35 }
36 }
37 }
38
39 selected [0] = pool [0];
40 selected [1] = pool [n -1];
41
42 // Linear Search
43 for ( i = 0; i < n ; i ++ )
44 {
45 if ( strcmp ( pool [ i ]. name , " rajat " ) == 0 )
46 {
47 selected [2] = pool [ i ];
48 }
49 }
50
51 printf ( " \ nPlayers in the sorted pool are : " ) ;
52 printf ( " \ nNAME \ tJERSEY \ tRUNS \ t \ n " ) ;
53 printf ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\ n " ) ;
54 for ( i = 0; i < n ; i ++ )
55 {
11 // Function Definition
12 void read ( int , struct player *) ;
13 void display ( int , int , struct player [] , struct player []) ;
14 int search ( int , char [] , struct player []) ;
15 void sort ( int , struct player *) ;
16 int select ( struct player [] , struct player * , int , int ) ;
17
18 int main ()
19 {
20 struct player pool [10];
21 struct player selected [10];
22 struct player * p ;
23 int n ;
24 int pos ;
25 int cnt ;
26
27 printf ( " \ nEnter no . of players > " ) ;
28 scanf ( " % d " ,& n ) ;
29
30 p = pool ;
31
32 read (n , p ) ;
33
34 sort (n , p ) ;
35
36 pos = search (n , " rajat " , p ) ;
37
38 cnt = select ( pool , selected , n , pos ) ;
39
40 display (n , cnt , pool , selected ) ;
41 }
42
43
44 void read ( int l , struct player * ply )
45 {
46 int i ;
47
48 for ( i = 0; i < l ; i ++ )
49 {
50 printf ( " \ nEnter player % d details ( name , jersey number , runs ) >\ t " ,i
+1) ;
51 scanf ( " % s % d % d " , ( ply + i ) -> name , &( ply + i ) -> jerseyNo , &( ply + i ) -> runs ) ;
52 }
53 }
54
55 void sort ( int l , struct player * ply )
56 {
57 int i , j ;
58 struct player temp ;
59
60 // Bubble Sort
61 for ( i = 0; i < l ; i ++ )
62 {
63 for ( j = 0; j < l -i -1; j ++ )
64 {
65 if ( ( ply + j ) -> runs < ( ply + j +1) -> runs )
66 {
67 temp = *( ply + j ) ;
68 *( ply + j ) = *( ply + j +1) ;
69 *( ply + j +1) = temp ;
70 }
71 }
72 }
73 }
74
75
76 int search ( int l , char key [] , struct player pool [])
77 {
78 int i ;
79 // Linear Search
80 for ( i = 0; i < l ; i ++ )
81 {
82 if ( strcmp ( pool [ i ]. name , " rajat " ) == 0 )
83 {
84 return i ;
85 }
86 }
87 return -1;
88 }
89
90 int select ( struct player pool [] , struct player *s , int l , int pos )
91 {
92 int cnt = 0;
93
94 * s = pool [0];
95 *( s +1) = pool [l -1];
96
97 cnt = 2;
98
99 if ( pos != -1)
100 {
101 *( s +2) = pool [ pos ];
102 cnt ++;
103 }
104 else
105 printf ( " \ n Rajat player not found in the pool ! " ) ;
106
107 return cnt ;
108 }
109
110 void display ( int l , int cnt , struct player p [] , struct player s [])
111 {
112 int i ;
113
114 printf ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\ n " ) ;
115 printf ( " \ nThe sorted players from pool are : " ) ;
116 printf ( " \ nNAME \ tJERSEY \ tRUNS \ t \ n " ) ;
117 printf ( " -\n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
118 for ( i = 0; i < l ; i ++ )
119 {
120 printf ( " % s \ t %3 d \ t %3 d \ n " , p [ i ]. name , p [ i ]. jerseyNo , p [ i ]. runs ) ;
121 }
122
5.2 Files
5.2.1 Understanding the Problem
Problem Statement: Sangamesh once a student, now works for Contineo. His boss has
asked him to implement a database for 3rd sem students. Sangamesh had studied about flat
file databases from SPattar sir. In flat file systems, files are used for storing the data. Help
sangamesh to implement the database.
General Overview
• We need to use file management operations to implement the flat file database.
Sub-problems Yes, we have two sub-problem in the question. One is to read the data from
file and other is to write the data to the file.
Ambiguity Yes, there are two ambiguity in the question. First one is how many students’
data are to be stored? Second one is which student details are to be stored.
Extra Information Yes, there are extraneous information in the question. Sangamesh,
Contineo and SPattar are extra information.
Known
• Data is to be stored and accessed from the file (read and write).
Assumptions
• For simplicity, we will take only two data items of students: name and marks.
Constraints Which type of file to use? As the data to be stored can be large, we can use
binary files. This will reduce the storage space and the read and write operations are also quick.
Problem Solving Strategy To store the data, we need files, and to access the data, we need
the data members of the structure. Thus, we can solve the problem using Natural Language
strategy, i.e., based on Q and A or query technique of database.
Identification of Functions
• readFile() ← To access the students details from file, it makes use of displayStruct().
• readStruct() ← To read the students’ details from user and give it to writeFile().
Did we overlook anything? No. We solved all the sub-problems. Also, the extra informa-
tion was eliminated.
Did we choose the correct strategy? Yes. As the problem was based on natural language,
we identified the students’ details and used correct file operations to store and access the data.
Did we employ that strategy correctly? Yes. We used binary files to store the data.
Also, fread() and fwrite() library functions are used to store and read the data from the binary
file.
5.2.7 C Program
32 }
33
34 void writeFile ( char fName [10] , int n )
35 {
36 int i ;
37 struct stud s ;
38 FILE * fptr ;
39
40 fptr = fopen ( fName , " wb " ) ;
41
42 if ( fptr == NULL )
43 {
44 printf ( " Error ! opening file " ) ;
45 exit (101) ;
46 }
47
48 for ( i = 0; i < n ; i ++ )
49 {
50 printf ( " \ nEnter details for student % d " , i +1) ;
51 s = readStruct () ;
52 fwrite (& s , sizeof ( struct stud ) , 1 , fptr ) ;
53 }
54
55 fclose ( fptr ) ;
56 }
57
58 struct stud readStruct ()
59 {
60 struct stud s ;
61
62 printf ( " \ nEnter Student name > " ) ;
63 scanf ( " % s " , s . name ) ;
64
65 printf ( " \ nEnter Student marks > " ) ;
66 scanf ( " % d " , & s . marks ) ;
67
68 return s ;
69 }
70
71 void readFile ( char fName [10] , int n )
72 {
73 struct stud s ;
74 int i ;
75 FILE * fptr ;
76
77 fptr = fopen ( fName , " rb " ) ;
78
II Semester
18ECSP102– Problem Solving with Data Structures
2021-2022
1
Data Structures
Motivation
A data structure is a special way of organizing and storing data in a computer
so that it can be used efficiently.
2
Data Structures
Classification
3
KLE Technological University
II Semester
18ECSP102– Problem Solving with Data Structures
List
2021-2022
4
List
Motivation
6
List
Basic operations
1. Inserting: Insert a new data in the list
2. Deleting : To remove the existing data from the list
3. Iterating a list: Navigation of list
4. Searching : Find out a specific data in the list
7
List
Motivation
9
List
Disadvantages of arrays
• Fixed in size.
• Insertions and Deletions are time consuming process.
Linked list is a sequence of data items where each data item has the
related information. Every data item is interconnected with each
other. 10
List
Linked List
A linked list is a data structure in which data items can be added
and removed during run time of the program.
head
11
Linked List
Types
Depending on the way in which the links are used to maintain
adjacency, several different types of linked lists are possible.
1. Singly-linked list
2. Circular Singly-linked list
3. Doubly linked list
4. Circular Doubly linked list
12
Linked List
13
Linked List
14
Linked List
15
Linked List
17
Singly Linked List
18
Singly Linked List
V. Summary
Summarize the way you have solved the problem.
20
Singly Linked List
21
Singly Linked List
22
Singly Linked List
23
Singly Linked List
How to begin?
To start with, we have to create a node (the first node), and make head
point to it.
Algorithm: To create a node
Step 1: Start
Step 2: Declare a pointer for node.
Step 3: Allocate the memory for node.
Step 4: Check whether memory is allocated.
Step 5: Return the address of node.
Step 6: Stop
24
Singly Linked List
25
Singly Linked List
26
Singly Linked List
28
Singly Linked Lists
29
Singly Linked Lists
30
Singly Linked List
31
Singly Linked Lists
32
Singly Linked List
33
Singly Linked List
34
Singly Linked Lists
35
Singly Linked List
36
Singly Linked Lists
37
Singly Linked List
38
Singly Linked List
39
Singly Linked List
OUTPUT
Name Rank Age Address
Kiran 581 18 Bangalore
Raj 105 18 Hubballi
Amar 252 18 Hubballi
Abdul 187 19 Karwar
40
Singly Linked List
42
Singly Linked List
43
Singly Linked List
44
Singly Linked List
45
Singly Linked List
46
Singly Linked List
CLASS ACTIVITY – 4
Karnataka CET started admission for first year under graduate program for the year 2021. After the seat
allotment process, candidates who got their seat allotted in KLE Tech. started visiting the campus for
admission. Every candidate provided the his/her basic details for admission process. Management of
KLE Tech. wanted to know the admitted list of candidates after first round. Apply Problem Solving
Framework to create and display the list of candidates.
47
Singly Linked List
48
Singly Linked List
49
Singly Linked List
50
Singly Linked List
Step 1: Start
Step 2: Check whether the List is empty.
if yes, display message that List is empty.
Step 3: Check whether the List has only one node.
if yes, display the data and make head=NULL.
Step 4: Otherwise, Declare two pointers, prev and cur.
Step 5: Assign address of head node to cur.
Step 6: Traverse till the last node of the List. Pointer prev points to
last but one node. Pointer cur points to last node.
Step 7: Disconnect the prev from cur [last node].
Step 8: Display the data of cur [last node].
Step 9: Deallocate the memory of cur [last node].
Step 10: Stop
51
Singly Linked List
52
Singly Linked List
53
Singly Linked List
CLASS ACTIVITY – 5
Karnataka CET started admission for first year under graduate program for the year 2021.
After the seat allotment process, candidates who got their seat allotted in KLE Tech. started
visiting the campus for admission. Every candidate provided the his/her basic details for
admission process. Management of KLE Tech. wanted to know the admitted list of
candidates after first round. Apply Problem Solving Framework to create and display the list
of candidates.
During the second round of admission process, the last candidate from the list who is from
Mangalore, got admission in the Engineering College in his hometown. He withdrew his
admission from KLE Tech. Display the updated list of candidates admitted in KLE Tech. after
withdrawal of last candidate.
55
Singly Linked List
After the above operation, the second node in the Singly linked list
becomes the head node of the list.
56
Singly Linked List
57
Singly Linked List
58
Singly Linked List
59
Singly Linked List
60
Singly Linked List
61
Singly Linked List
PRACTICE PROBLEMS
1. Chicago college Placement Officer Mr. Mahon Raj has all the details of
the students who are placed from the college along with the company
name and passed out year. Since the ABN visit is in March 2021,
Placement Officer has two tasks. The first one displays the students
who are placed in 2020 and the second one displays the student
details who has the highest Package. Please help Mr. Mahon Raj to do
the tasks.
62
Singly Linked List
63
Linked List
64
Circular Singly linked list
Introduction
Circular linked list is a linked list where all nodes are connected to
form a circle.
There is no NULL at the end.
We can traverse the whole list by starting from any node. We just
need to stop when the first visited node is visited again.
head
101 20 30 40
65
Circular Singly linked list
Introduction
Class Activity-1 (Individual)
66
Circular Singly linked list
67
Circular Singly linked list
V. Summary
Summarize the way you have solved the problem.
69
Circular Singly linked list
70
Circular Singly linked list
Create a node
Algorithm: To create a node in Circular List
Step 1: Start
Step 2: Declare a pointer for node.
Step 3: Allocate the memory for node.
Step 4: Check whether memory is allocated.
Step 5: Make the next pointer point to same node (circular!).
Step 5: Return the address of node.
Step 6: Stop
71
Circular Singly linked list
72
Circular Singly linked list
73
Circular Singly linked list
74
Circular Singly linked list
75
Circular Singly linked list
76
Circular Singly linked list
Everyday a Postman starts distribution of the letters at 10.00 am in Lingaraj Nagar area and
returns to BVB CAMPUS post office at 12.00 noon. On first Day of the week, postman has 3
letters to deliver in his allotted area. He prepares a list of visiting houses before leaving the Post
Office and notes down the delivery address of each house. Apply Problem Solving Framework to
create and display the list of houses visited by the postman.
77
Circular Singly linked list
78
Circular Singly linked list
80
Circular Singly linked list
Consider second Day of the week, postman has 1 more additional letter to be delivered compared
to first Day (4 letters) to distribute in his allotted area. The 4th letter belongs to the house which
is located at the end of all the houses which he visited on first Day. Update and Display the list of
houses to be visited by the postman on second Day.
82
Circular Singly linked list
83
Circular Singly linked list
84
Circular Singly linked list
86
Circular Singly linked list
87
Circular Singly linked list
88
Circular Singly linked list
89
Circular Singly linked list
90
Circular Singly linked list
91
Circular Singly linked list
92
Circular Singly linked list
93
Circular Singly linked list
95
Circular Singly linked list
PRACTICE PROBLEMS
1. Wilson is resident of India, returns from America to India for a short visit. He
wishes to go for Brand Factory for shopping. Wilson buys 3 jeans of different
companies each with 40%, 50% and 60% discount and 3 casual shirts of
different companies with 40% discount on each. Apply Problem Solving to
Prepare a bill for Wilson using Circular Singly Linked List which stores the
details of each item purchased. Calculate Total Bill amount and Display the
bill.
97
Circular Singly linked list
98
Chapter - 02
Linked List
(Sample Programs)
Syllabus
• Abstract Data Type
head
2.1.1 Steps
1. Initialize: Starts with assignment of address of first node (i.e., head) to a pointer variable.
This is considered as the current node.
3. Increment: The current node is then updated with the address of next node by assigning
link part of current node to the current pointer.
2.1.2 Algorithm
cur
head 30 40 20 10
cur
head 30 40 20 10
• Searching
Steps
4. Test: Check if cur pointer is NULL. If yes, return the size of list and end the traversal.
2.2 Searching
2.2.1 Steps
1. Initialize: Starts with assignment of address of first node (i.e., head)to a pointer variable.
This is considered as the current node.
2. Searching: The data of the current node is compared with the search key. If they are
equal then search is successful.
3. Increment: If key is not found, then current node is updated with the address of next
node by assigning link part of current node to the current pointer.
4. Test: Check if current pointer is NULL. If yes, end the traversal, else continue with the
second step.
2.2.2 Algorithm
cur Key
20
head 30 40 20 10
cur Key
20
head 30 40 20 10
Key cur
70
head 30 40 20 10
2.3 Insertion
A new node can be inserted in linked list:
• at the Beginning
• at given Position
• at the End
Algorithm
10 head 10
(a) List is empty, new node created. (b) New node made as first node of the list.
new
head 40 30 20 10 50
40 30 20 10
10 head 10
(a) List is empty, new node created. (b) New node made as first node of the list.
new
head 20 10 5
new
head 20 10 5
Algorithm
1 2 3
head 10 20 40
new
30
head 10 20 40
(2) cur− > link = new (1) new− > link = cur− > link
new
30
18 {
19 printf ( " \ nList is empty ! " ) ;
20 }
21 else if ( pos == 1 ) // Insert at 1 st position
22 {
23 new - > link = h ;
24 h = new ;
25 }
26 else // Insert at any position
27 {
28 // Traverse till the required position .
29 cur = h ;
30 i = 1;
31 while ( i < pos )
32 {
33 tmp = cur ;
34 cur = cur - > link ;
35 i ++;
36 }
37
38 // Reassign links .
39 new - > link = tmp - > link ;
40 tmp - > link = new ;
41 }
42
43 displayList ( h ) ;
44 return h ;
45 }
2.4 Deletion
A node can be deleted in the linked list:
• at the Beginning
• at given Position
• at the End
(1) temp
head 10 20 30 40
3. Case-3: If nodes more than 1 node exists, traverse to the last but one node of the list
using cur pointer.
(a) During traversal, store the address of previous node in prev pointer.
(b) Make link of prev node as NULL.
(c) Free the memory of prev pointer.
10 return h ;
11 }
12
13 // Case -2: List has only 1 node .
14 if ( h - > link == NULL )
15 {
16 h = NULL ;
17 return h ;
18 }
19
20 // Case -3: More than 1 node in the list .
21 while ( cur - > link != NULL )
22 {
23 // store previous link node
24 prev = cur ;
25 cur = cur - > link ;
26 }
27
28 // Assign 2 nd last node ’s link to Null
29 prev - > link = NULL ;
30
31 free ( cur ) ;
32
33 return h ;
34 }
prev cur
head 10 20 30 40
2. Case-2: If only one nodes exists, check for position. If position is 1 then delete the node.
3. Case-3: If more than one nodes exists, traverse to the given position in the list using cur
and prev pointer.
37 return h ;
38 }
prev cur
pos 3 cnt 3
head 40 30 20 10
3 Menu-driven Programming
A menu can be created using infinite loop and a switch-case statement.
4 Sample Programs
4.1 Creating a Linked List
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 // Structure Definition
5 struct node
6 {
7 int data ;
8 struct node * link ;
9 };
10
21 {
22 NODE head = NULL ;
23
24 // Create a list with 5 nodes
25 head = insertFront ( head , 10) ;
26 head = insertFront ( head , 20) ;
27 head = insertFront ( head , 30) ;
28 head = insertFront ( head , 40) ;
29 head = insertFront ( head , 50) ;
30
31 // Display the list
32 displayList ( head ) ;
33 }
34
35 // Creates a new node and assigns values to its fields
36 NODE createNode ( int val )
37 {
38 NODE p = ( NODE ) malloc ( sizeof ( NODE ) ) ;
39
40 p - > data = val ;
41 p - > link = NULL ;
42
43 return p ;
44 }
45
46 // Inserts a new node at the beginning of the list
47 NODE insertFront ( NODE h , int val )
48 {
49 NODE new = createNode ( val ) ;
50
51 if ( h == NULL )
52 {
53 printf ( " \ nList is empty , new node ( data : % d ) is first node !\ n " , new - >
data ) ;
54 h = new ;
55 return h ;
56 }
57
58 new -> link = h ;
59 return new ;
60 }
61
62 // Displays the list nodes , by traversing the list
63 void displayList ( NODE h )
64 {
65 NODE temp = h ;
66 int i = 0;
67
68 if ( h == NULL )
69 {
70 printf ( " \ nList is Empty ! " ) ;
71 return ;
72 }
73
74 while ( temp != NULL )
75 {
76 printf ( " %d - > " , temp - > data ) ;
77 temp = temp - > link ;
78 i ++;
79 }
80 printf ( " NULL " ) ;
81 return ;
82 }
4 struct node
5 {
6 int data ;
7 struct node * link ;
8 };
9
46 p - > link = h ;
47 h = p;
48 }
49
50 void displayList ()
51 {
52 struct node * temp ;
53 int i = 0;
54
55 if ( h == NULL )
56 {
From now on, we will only write function declarations in the sample programs. Refer to function
definitions from sections below and write the complete programs.
• Structure Definition – 1.1
• Menu Application – 3
23
24 NODE insertMenu ( NODE ) ;
25 void menu ( NODE ) ;
26
27 // Driver Code -- Main Function
28 void main ()
29 {
30 NODE head = NULL ;
31
32 menu ( head ) ;
33 }
34
35 // Menu for insertion
36 NODE insertMenu ( NODE h )
37 {
38 int opn , d , p ;
39
84
85 // select appropriate option through switch
86 switch ( opn )
87 {
88 case 1: h = insertMenu ( h ) ;
89 break ;
90
91 case 2: displayList ( h ) ;
92 break ;
93
94 case 3: printf ( " \ nBye , Bye !! " ) ;
95 exit (0) ;
96
97 default : printf ( " \ nInvalid Option ! " ) ;
98 break ;
99 }
100 }
101 }
102
103 // Include other function definitions from respective sections
4 // Structure Definition
5 struct node
6 {
7 int data ;
8 struct node * link ;
9 };
10
11 // Renaming the structure
12 typedef struct node * NODE ;
13
14 // Function Decelerations
15 NODE create ( NODE ) ;
16
17 NODE createNode ( int ) ;
18
19 NODE deleteFront ( NODE ) ;
20 NODE deleteEnd ( NODE ) ;
21 NODE deleteAny ( NODE , int ) ;
22 NODE delete ( NODE ) ;
23
24 void displayList ( NODE ) ;
25
26 void menu ( NODE ) ;
27
39 NODE n1 , n2 , n3 , n4 , n5 ;
40
41 n1 = createNode (10) ;
42 n2 = createNode (20) ;
43 n3 = createNode (30) ;
44 n4 = createNode (40) ;
45 n5 = createNode (50) ;
46
47 h = n1 ;
48 n1 - > link = n2 ;
49 n2 - > link = n3 ;
50 n3 - > link = n4 ;
51 n4 - > link = n5 ;
52
53 return h ;
54 }
55
100
101 scanf ( " % d " , & opn ) ;
102
103 // select appropriate option through switch
104 switch ( opn )
105 {
106 case 1: h = create ( h ) ;
107 break ;
108
109 case 2: h = delete ( h ) ;
110 break ;
111
112 case 3: displayList ( h ) ;
113 break ;
114
115 case 4: printf ( " \ nBye , Bye !! " ) ;
116 exit (0) ;
117
118 default : printf ( " \ nInvalid Option ! " ) ;
119 break ;
120 }
121 }
122 }
123
124 // Include other function definitions from respective sections .
34 {
35 NODE head = NULL ;
36
37 menu ( head ) ;
38 }
39
40 // Menu for insertion
41 NODE insert ( NODE h )
42 {
43 int opn , d , p ;
44
95 h = deleteAny (h , p ) ;
96 break ;
97
98 default : printf ( " \ nInvalid Option ! " ) ;
99 break ;
100 }
101
102 return h ;
103 }
104
105 // Function to display menu
106 void menu ( NODE h )
107 {
108 int opn ;
109
110 // Infinite Loop for Menu
111 for ( ; ; )
112 {
113 printf ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
114 printf ( " \n - - - - - - - - - - - - MENU - - - - - - - - - - - - - " ) ;
115 printf ( " \ nEnter > " ) ;
116 printf ( " \ n1 : Insert \ n2 : Delete \ n3 : Display \ n4 : Exit " ) ;
117 printf ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
118
119 scanf ( " % d " , & opn ) ;
120
121 // select appropriate option through switch
122 switch ( opn )
123 {
124 case 1: h = insert ( h ) ;
125 break ;
126
127 case 2: h = delete ( h ) ;
128 break ;
129