SlideShare a Scribd company logo
1 of 9
Download to read offline
For this homework, you will write a program to create and manipulate a simple linked list. For
each node in the linked list you will generate a random number, create a node that holds that
number, and insert that node into its sorted position in the linked list. Once the nodes are
inserted, you will traverse the list, printing the value held in each node. Then you will clean up
the list (deleting all the nodes) and exit the program. You will also learn about a tool that you can
use to help you check for memory errors in your code.
Specifications:
Define a struct appropriate for holding a random integer. This struct will also contain a "next"
pointer to reference a separate instance of the struct.
You may use the typedef keyword to give your struct a separate typename if you wish, but this is
not a requirement.
The program will read command line arguments. The first argument is the program name (as it
will always be) followed by a random number seed, the number of random numbers to generate
and ending with the Maximum Possible Value of the Random numbers generated (i.e. argc
should be 4).
Your program will use a "dummy node" for the linked list. In other words, the head of the list
will be an actual node, but the data field in that node will not be used for sorting purposes.
Your program must contain the following functions:
main() (for obvious reasons)
insertNodeSorted() - You may implement this either of two ways.
One way is to use the head of the list as one parameter and the integer value as the second. The
function will allocate memory for the node, initialize it and then insert it in the proper location in
the list.
The other way is to pass the head of the list as one parameter and a previously allocated and
initialized node as the other. The existing node is inserted in the proper location in the list.
printList() - Takes the head of the list as its only parameter, traverses the list, printing out the
data in sorted order.
deleteList() - Takes the head of the list as its only parameter, traverses the list, deleting all nodes.
The basic algorithm of your program is
Use a loop (based upon command line input values) that:
Generates a random number
Prints the number to stdout
Creates a node that contains that random number
Inserts the new node in sorted order into the existing list
Print the sorted linked list
Delete the linked list
You will use a Makefile to compile your program
Solution
#include
#include
#include
struct test_struct
{
int val;
struct test_struct *next;
};
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
struct test_struct* create_list(int val)
{
printf(" creating list with headnode as [%d] ",val);
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf(" Node creation failed  ");
return NULL;
}
ptr->val = val;
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
struct test_struct* add_to_list(int val, bool add_to_end)
{
if(NULL == head)
{
return (create_list(val));
}
if(add_to_end)
printf(" Adding node to end of list with value [%d] ",val);
else
printf(" Adding node to beginning of list with value [%d] ",val);
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf(" Node creation failed  ");
return NULL;
}
ptr->val = val;
ptr->next = NULL;
if(add_to_end)
{
curr->next = ptr;
curr = ptr;
}
else
{
ptr->next = head;
head = ptr;
}
return ptr;
}
struct test_struct* search_in_list(int val, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
printf(" Searching the list for value [%d]  ",val);
while(ptr != NULL)
{
if(ptr->val == val)
{
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
if(true == found)
{
if(prev)
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}
int delete_from_list(int val)
{
struct test_struct *prev = NULL;
struct test_struct *del = NULL;
printf(" Deleting value [%d] from list ",val);
del = search_in_list(val,&prev);
if(del == NULL)
{
return -1;
}
else
{
if(prev != NULL)
prev->next = del->next;
if(del == curr)
{
curr = prev;
}
else if(del == head)
{
head = del->next;
}
}
free(del);
del = NULL;
return 0;
}
void print_list(void)
{
struct test_struct *ptr = head;
printf(" Printing list Start  ");
while(ptr != NULL)
{
printf(" [%d]  ",ptr->val);
ptr = ptr->next;
}
printf(" Printing list End  ");
return;
}
int main(void)
{
int i = 0, ret = 0;
struct test_struct *ptr = NULL;
print_list();
for(i = 5; i<10; i++)
add_to_list(i,true);
print_list();
for(i = 4; i>0; i--)
add_to_list(i,false);
print_list();
for(i = 1; i<10; i += 4)
{
ptr = search_in_list(i, NULL);
if(NULL == ptr)
{
printf(" Search [val = %d] failed, no such element found ",i);
}
else
{
printf(" Search passed [val = %d] ",ptr->val);
}
print_list();
ret = delete_from_list(i);
if(ret != 0)
{
printf(" delete [val = %d] failed, no such element found ",i);
}
else
{
printf(" delete [val = %d] passed  ",i);
}
print_list();
}
return 0;
}
*********************
out put
Printing list Start
Printing list End
creating list with headnode as [5]
Adding node to end of list with value [6]
Adding node to end of list with value [7]
Adding node to end of list with value [8]
Adding node to end of list with value [9]
Printing list Start
[5]
[6]
[7]
[8]
[9]
Printing list End
Adding node to beginning of list with value [4]
Adding node to beginning of list with value [3]
Adding node to beginning of list with value [2]
Adding node to beginning of list with value [1]
Printing list Start
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Printing list End
Searching the list for value [1]
Search passed [val = 1]
Printing list Start
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Printing list End
Deleting value [1] from list
Searching the list for value [1]
delete [val = 1] passed
Printing list Start
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Printing list End
Searching the list for value [5]
Search passed [val = 5]
Printing list Start
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Printing list End
Deleting value [5] from list
Searching the list for value [5]
delete [val = 5] passed
Printing list Start
[2]
[3]
[4]
[6]
[7]
[8]
[9]
Printing list End
Searching the list for value [9]
Search passed [val = 9]
Printing list Start
[2]
[3]
[4]
[6]
[7]
[8]
[9]
Printing list End
Deleting value [9] from list
Searching the list for value [9]
delete [val = 9] passed
Printing list Start
[2]
[3]
[4]
[6]
[7]
[8]
Printing list End

More Related Content

Similar to For this homework, you will write a program to create and manipulate.pdf

a) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdfa) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdfMAYANKBANSAL1981
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdfaathmaproducts
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfeyewaregallery
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdfaathiauto
 
Please the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfPlease the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfadmin463580
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
Discuss about Header Node And also write a program for unordered si.pdf
Discuss about Header Node  And also write a program for unordered si.pdfDiscuss about Header Node  And also write a program for unordered si.pdf
Discuss about Header Node And also write a program for unordered si.pdfeyevision3
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfezhilvizhiyan
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project FileDeyvessh kumar
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 

Similar to For this homework, you will write a program to create and manipulate.pdf (20)

a) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdfa) Complete both insert and delete methods. If it works correctly 10.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
Data structure
Data  structureData  structure
Data structure
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
Arrays
ArraysArrays
Arrays
 
Please the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdfPlease the following is the currency class of perious one- class Curre.pdf
Please the following is the currency class of perious one- class Curre.pdf
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Discuss about Header Node And also write a program for unordered si.pdf
Discuss about Header Node  And also write a program for unordered si.pdfDiscuss about Header Node  And also write a program for unordered si.pdf
Discuss about Header Node And also write a program for unordered si.pdf
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 

More from herminaherman

Write an equation for the height of the point P above the ground as .pdf
Write an equation for the height of the point P above the ground as .pdfWrite an equation for the height of the point P above the ground as .pdf
Write an equation for the height of the point P above the ground as .pdfherminaherman
 
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdfWild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdfherminaherman
 
Which of the following is considered to be a distributed denial of s.pdf
Which of the following is considered to be a distributed denial of s.pdfWhich of the following is considered to be a distributed denial of s.pdf
Which of the following is considered to be a distributed denial of s.pdfherminaherman
 
Which of the folllowing statements about the solubility of 1- propan.pdf
Which of the folllowing statements about the solubility of 1- propan.pdfWhich of the folllowing statements about the solubility of 1- propan.pdf
Which of the folllowing statements about the solubility of 1- propan.pdfherminaherman
 
What type of mutation that impacts splicing is a significant contrib.pdf
What type of mutation that impacts splicing is a significant contrib.pdfWhat type of mutation that impacts splicing is a significant contrib.pdf
What type of mutation that impacts splicing is a significant contrib.pdfherminaherman
 
What data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdfWhat data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdfherminaherman
 
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdfUnlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdfherminaherman
 
Use provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdfUse provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdfherminaherman
 
Two four-sided dice are rolled. One die has the letters A through D..pdf
Two four-sided dice are rolled. One die has the letters A through D..pdfTwo four-sided dice are rolled. One die has the letters A through D..pdf
Two four-sided dice are rolled. One die has the letters A through D..pdfherminaherman
 
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
The job of a CODEC (CoderDecoder) is to  Convert an analog voice si.pdfThe job of a CODEC (CoderDecoder) is to  Convert an analog voice si.pdf
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdfherminaherman
 
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdfQuestion 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdfherminaherman
 
Private and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdfPrivate and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdfherminaherman
 
Name the two standards that are supported by major DBMSs for distrib.pdf
Name the two standards that are supported by major DBMSs for distrib.pdfName the two standards that are supported by major DBMSs for distrib.pdf
Name the two standards that are supported by major DBMSs for distrib.pdfherminaherman
 
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdfMaterial Manufacturing Name 3 ways that the properties of thermosett.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdfherminaherman
 
Mean can be computed for variables with ordinal-level measurements..pdf
Mean can be computed for variables with ordinal-level measurements..pdfMean can be computed for variables with ordinal-level measurements..pdf
Mean can be computed for variables with ordinal-level measurements..pdfherminaherman
 
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdfLet u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdfherminaherman
 
IV. Which compound would have the higher water solubility, tetrahydro.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdfIV. Which compound would have the higher water solubility, tetrahydro.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdfherminaherman
 
Interseting discussion topic,want to chime inDescribe a symbol .pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdfInterseting discussion topic,want to chime inDescribe a symbol .pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdfherminaherman
 
IN JAVA Write a program to create a binary data file named RandomInt.pdf
IN JAVA Write a program to create a binary data file named RandomInt.pdfIN JAVA Write a program to create a binary data file named RandomInt.pdf
IN JAVA Write a program to create a binary data file named RandomInt.pdfherminaherman
 
I wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfI wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfherminaherman
 

More from herminaherman (20)

Write an equation for the height of the point P above the ground as .pdf
Write an equation for the height of the point P above the ground as .pdfWrite an equation for the height of the point P above the ground as .pdf
Write an equation for the height of the point P above the ground as .pdf
 
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdfWild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
Wild-type (WT) mice are irradiated, destroying all of their bone marr.pdf
 
Which of the following is considered to be a distributed denial of s.pdf
Which of the following is considered to be a distributed denial of s.pdfWhich of the following is considered to be a distributed denial of s.pdf
Which of the following is considered to be a distributed denial of s.pdf
 
Which of the folllowing statements about the solubility of 1- propan.pdf
Which of the folllowing statements about the solubility of 1- propan.pdfWhich of the folllowing statements about the solubility of 1- propan.pdf
Which of the folllowing statements about the solubility of 1- propan.pdf
 
What type of mutation that impacts splicing is a significant contrib.pdf
What type of mutation that impacts splicing is a significant contrib.pdfWhat type of mutation that impacts splicing is a significant contrib.pdf
What type of mutation that impacts splicing is a significant contrib.pdf
 
What data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdfWhat data indicate that all three germ layers are specified in the b.pdf
What data indicate that all three germ layers are specified in the b.pdf
 
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdfUnlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
Unlike RNA, DNA containsA. adenine.B. uracil.C. phosphate grou.pdf
 
Use provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdfUse provided HTML file.Create JavaScript file to validate user i.pdf
Use provided HTML file.Create JavaScript file to validate user i.pdf
 
Two four-sided dice are rolled. One die has the letters A through D..pdf
Two four-sided dice are rolled. One die has the letters A through D..pdfTwo four-sided dice are rolled. One die has the letters A through D..pdf
Two four-sided dice are rolled. One die has the letters A through D..pdf
 
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
The job of a CODEC (CoderDecoder) is to  Convert an analog voice si.pdfThe job of a CODEC (CoderDecoder) is to  Convert an analog voice si.pdf
The job of a CODEC (CoderDecoder) is to Convert an analog voice si.pdf
 
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdfQuestion 4 Constructive retirement is when A. P buys Ss common stoc.pdf
Question 4 Constructive retirement is when A. P buys Ss common stoc.pdf
 
Private and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdfPrivate and Public Health are working together through many program .pdf
Private and Public Health are working together through many program .pdf
 
Name the two standards that are supported by major DBMSs for distrib.pdf
Name the two standards that are supported by major DBMSs for distrib.pdfName the two standards that are supported by major DBMSs for distrib.pdf
Name the two standards that are supported by major DBMSs for distrib.pdf
 
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdfMaterial Manufacturing Name 3 ways that the properties of thermosett.pdf
Material Manufacturing Name 3 ways that the properties of thermosett.pdf
 
Mean can be computed for variables with ordinal-level measurements..pdf
Mean can be computed for variables with ordinal-level measurements..pdfMean can be computed for variables with ordinal-level measurements..pdf
Mean can be computed for variables with ordinal-level measurements..pdf
 
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdfLet u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
Let u_1, u_2 be finite dimensional subspaces of a vector space V. Sho.pdf
 
IV. Which compound would have the higher water solubility, tetrahydro.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdfIV. Which compound would have the higher water solubility, tetrahydro.pdf
IV. Which compound would have the higher water solubility, tetrahydro.pdf
 
Interseting discussion topic,want to chime inDescribe a symbol .pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdfInterseting discussion topic,want to chime inDescribe a symbol .pdf
Interseting discussion topic,want to chime inDescribe a symbol .pdf
 
IN JAVA Write a program to create a binary data file named RandomInt.pdf
IN JAVA Write a program to create a binary data file named RandomInt.pdfIN JAVA Write a program to create a binary data file named RandomInt.pdf
IN JAVA Write a program to create a binary data file named RandomInt.pdf
 
I wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdfI wanna add the shape creator like rectangular , square , circle etc.pdf
I wanna add the shape creator like rectangular , square , circle etc.pdf
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

For this homework, you will write a program to create and manipulate.pdf

  • 1. For this homework, you will write a program to create and manipulate a simple linked list. For each node in the linked list you will generate a random number, create a node that holds that number, and insert that node into its sorted position in the linked list. Once the nodes are inserted, you will traverse the list, printing the value held in each node. Then you will clean up the list (deleting all the nodes) and exit the program. You will also learn about a tool that you can use to help you check for memory errors in your code. Specifications: Define a struct appropriate for holding a random integer. This struct will also contain a "next" pointer to reference a separate instance of the struct. You may use the typedef keyword to give your struct a separate typename if you wish, but this is not a requirement. The program will read command line arguments. The first argument is the program name (as it will always be) followed by a random number seed, the number of random numbers to generate and ending with the Maximum Possible Value of the Random numbers generated (i.e. argc should be 4). Your program will use a "dummy node" for the linked list. In other words, the head of the list will be an actual node, but the data field in that node will not be used for sorting purposes. Your program must contain the following functions: main() (for obvious reasons) insertNodeSorted() - You may implement this either of two ways. One way is to use the head of the list as one parameter and the integer value as the second. The function will allocate memory for the node, initialize it and then insert it in the proper location in the list. The other way is to pass the head of the list as one parameter and a previously allocated and initialized node as the other. The existing node is inserted in the proper location in the list. printList() - Takes the head of the list as its only parameter, traverses the list, printing out the data in sorted order. deleteList() - Takes the head of the list as its only parameter, traverses the list, deleting all nodes. The basic algorithm of your program is Use a loop (based upon command line input values) that: Generates a random number Prints the number to stdout Creates a node that contains that random number Inserts the new node in sorted order into the existing list Print the sorted linked list
  • 2. Delete the linked list You will use a Makefile to compile your program Solution #include #include #include struct test_struct { int val; struct test_struct *next; }; struct test_struct *head = NULL; struct test_struct *curr = NULL; struct test_struct* create_list(int val) { printf(" creating list with headnode as [%d] ",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf(" Node creation failed "); return NULL; } ptr->val = val; ptr->next = NULL; head = curr = ptr; return ptr; } struct test_struct* add_to_list(int val, bool add_to_end) { if(NULL == head) { return (create_list(val)); } if(add_to_end)
  • 3. printf(" Adding node to end of list with value [%d] ",val); else printf(" Adding node to beginning of list with value [%d] ",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf(" Node creation failed "); return NULL; } ptr->val = val; ptr->next = NULL; if(add_to_end) { curr->next = ptr; curr = ptr; } else { ptr->next = head; head = ptr; } return ptr; } struct test_struct* search_in_list(int val, struct test_struct **prev) { struct test_struct *ptr = head; struct test_struct *tmp = NULL; bool found = false; printf(" Searching the list for value [%d] ",val); while(ptr != NULL) { if(ptr->val == val) { found = true; break; }
  • 4. else { tmp = ptr; ptr = ptr->next; } } if(true == found) { if(prev) *prev = tmp; return ptr; } else { return NULL; } } int delete_from_list(int val) { struct test_struct *prev = NULL; struct test_struct *del = NULL; printf(" Deleting value [%d] from list ",val); del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next; if(del == curr) { curr = prev; } else if(del == head)
  • 5. { head = del->next; } } free(del); del = NULL; return 0; } void print_list(void) { struct test_struct *ptr = head; printf(" Printing list Start "); while(ptr != NULL) { printf(" [%d] ",ptr->val); ptr = ptr->next; } printf(" Printing list End "); return; } int main(void) { int i = 0, ret = 0; struct test_struct *ptr = NULL; print_list(); for(i = 5; i<10; i++) add_to_list(i,true); print_list(); for(i = 4; i>0; i--) add_to_list(i,false); print_list(); for(i = 1; i<10; i += 4) { ptr = search_in_list(i, NULL); if(NULL == ptr) {
  • 6. printf(" Search [val = %d] failed, no such element found ",i); } else { printf(" Search passed [val = %d] ",ptr->val); } print_list(); ret = delete_from_list(i); if(ret != 0) { printf(" delete [val = %d] failed, no such element found ",i); } else { printf(" delete [val = %d] passed ",i); } print_list(); } return 0; } ********************* out put Printing list Start Printing list End creating list with headnode as [5] Adding node to end of list with value [6] Adding node to end of list with value [7] Adding node to end of list with value [8] Adding node to end of list with value [9] Printing list Start [5] [6] [7] [8] [9] Printing list End
  • 7. Adding node to beginning of list with value [4] Adding node to beginning of list with value [3] Adding node to beginning of list with value [2] Adding node to beginning of list with value [1] Printing list Start [1] [2] [3] [4] [5] [6] [7] [8] [9] Printing list End Searching the list for value [1] Search passed [val = 1] Printing list Start [1] [2] [3] [4] [5] [6] [7] [8] [9] Printing list End Deleting value [1] from list Searching the list for value [1] delete [val = 1] passed Printing list Start [2] [3] [4] [5]
  • 8. [6] [7] [8] [9] Printing list End Searching the list for value [5] Search passed [val = 5] Printing list Start [2] [3] [4] [5] [6] [7] [8] [9] Printing list End Deleting value [5] from list Searching the list for value [5] delete [val = 5] passed Printing list Start [2] [3] [4] [6] [7] [8] [9] Printing list End Searching the list for value [9] Search passed [val = 9] Printing list Start [2] [3] [4] [6]
  • 9. [7] [8] [9] Printing list End Deleting value [9] from list Searching the list for value [9] delete [val = 9] passed Printing list Start [2] [3] [4] [6] [7] [8] Printing list End