Question
The program This project is an exercise in linked lists. It will allow the user to order a number of pizzas and then place them
The program
This project is an exercise in linked lists. It will allow the user to order a number of pizzas and then place them in a delivery list. The program will ask the user if he or she wishes a new pizza. If the user answers yes the program will ask for toppings. When the toppings have all been entered the pizza will be placed in a delivery list. When the user has finished ordering pizzas, the pizzas in the delivery list will be displayed in reverse order from the order in which they were entered. Running the program may look like:
djn@djn-ThinkPad-T410 ~/classes/cs262 $ pizza new pizza? (y/n) y toppings for new pizza: topping: mushrooms topping: pepperoni topping: extra cheese topping: new pizza? (y/n) x try again new pizza? (y/n) y toppings for new pizza: topping: pepperoni topping: green pepper topping: new pizza? (y/n) y toppings for new pizza: topping: ice cream topping: ketchup topping: whipped cream topping: new pizza? (y/n) n deliveries: pizza 1 has 3 topping(s) ice cream ketchup whipped cream pizza 2 has 2 topping(s) green pepper pepperoni pizza 3 has 3 topping(s) extra cheese mushrooms pepperoni djn@djn-ThinkPad-T410 ~/classes/cs262 $
The program will represent pizzas as a set of strings, a string for each topping. Sets, in turn, will be represented as linked lists. When a pizza has been ordered it will be placed into another linked list -- a list of sets.
Sets
A set of strings will be represented by a struct:
typedef struct { node *head; int count; } set;
where node is defined as
typedef struct node { char *data; struct node *next; } node;
You will provide functions to operate on sets:
set *createset(). Creates (with dmalloc() -- see below) a set, initializes its fields and returns a pointer to the set. int insert(char *str, set *s). Places a new string in the set by inserting a new node into its linked list and increments count. The toppings will be inserted into the pizza set in alphabetical order (use strcmpi() below for this). Duplicate strings (ignoring case) will not be allowed in the set. Returns whether the insertion was successful. void printset(set *s). Displays the elements of the set with each string on a new line.
The code for these functions will be placed in a file set_
You will also want to add some more functions to set_
/* compares strings for alphabetical ordering */ int strcmpi(char *s, char *t) { while (*s && tolower(*s) == tolower(*t)) { s++; t++; } return tolower(*s) - tolower(*t); } /* allocates memory with a check for successful allocation */ void *dmalloc(int size) { void *p = malloc(size); if (!p) { printf("memory allocation failed "); exit(1); } return p; }
You must always check the pointer returned by malloc() for successful memory allocation. In your program use the dmalloc() function above in place of malloc() to ensure that this is always done. You may also find it convenient to write a function char *stringcopy(char *s) which creates (with dmalloc()) a copy of the string parameter.
Flow of control
Your program will ask the user if he or she wants to create a new pizza and accept 'y' or 'n' for a response. If the user enters anything else the program will continue asking until 'y' or 'n' is entered. If the user answers yes the program will accept toppings until the user enters an empty string. The pizza is represented by a set of strings and each time the user enters a topping it is added to the pizza's string set. When the toppings have all been entered the pizza (or set, that is) is added to the delivery list. This is repeated until the user gets bored.
The delivery list is another linked list, this one a list of sets. It uses nodes of the type:
typedef struct deliverynode { set *data; struct deliverynode *next; } deliverynode;
Insert each new set as the first element in the list. When the user has finished entering pizzas, the pizzas (sets) will be displayed as in the above examples with a count of their toppings. The code for the prompting and basic program flow shall be placed in a file named project3_
Notes
Toppings can use more than one word (as separated by white space) such as "green pepper". This means that you must take input for toppings using fgets() rather than with scanf(). fgets() and scanf()do not mix well in the same program. This means that when the user's input of a single character ('y', 'n') is read it should be read into a string using fgets(). A less attractive, but acceptable for informal (only) programming, is to read a character with scanf("%c%*c", &ch). The %*c causes scanf() to read one more character (the user's ' ') and discard it.
free() must be used as appropriate.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started