QuestionQuestion

Building a linked list and traversing it iteratively and recursively

Write a C program that reads integers from the keyboard. Your program should then build a linked list in the heap. Each node should be defined as a structure with two members: an integer and a pointer to the next node in the list. The last node in the linked list points to NULL.
Your program should be able to insert an integer into the front of the list, find an integer and return a pointer to the corresponding node, and delete an integer from the list (if found). Also, your program should be able to use the list to print out the integers in the order in which they were entered. That is, you need to print the tail of the list first. This can be done either by using an iterative strategy or a recursive strategy for printing. We ask you to implement both strategies.

You should provide the user with a menu with six options to pick from:
1 Insert integer into linked list
2 Find integer in linked list
3 Delete integer from linked list
4 Print out integers backward using the iterative strategy
5 Print out integers backward using the recursive strategy
6 Quit
Enter 1, 2, 3, 4, 5, or 6:

Use a switch statement for controlling the menu. The menu is continually displayed until option 6 is entered by the user. You should manage properly an invalid choice.
Options 1, 2, and 3:
* If the user picks option 1, 2 or 3, you should ask him/her to input an integer.
Option 2:
* If the user picks option 2, and the entered integer is not in the list, the find_node function should return NULL to the invoker. Let the user know that the integer is not in the list.
Option 3:
* If the user picks option 3, and the entered integer is in the list, the corresponding node is deleted from the list and a pointer to the first node in the new linked list should be returned. Let the user know that the integer has been found and deleted.
* If the user picks option 3, and the entered integer is not in the list, no node is deleted and a pointer to the first node in the new linked list (which is still the same list) should be returned. Let the user know that the integer has not been found.
* A success_flag_ptr pointer to an integer should be used to indicate whether the node was successfully deleted or not.
Your program should have five functions (operations on linked lists) that implement respectively:

1) the insertion (insert_node),
2) the search (find_node),
3) the deletion (delete_node),
4) the backward print iteratively (print_backward_iteration), and
5) the backward print recursively (print_backward_recursion).
Those functions will be called from main

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/*
   This program builds a linked list and
   traverses it iteratively and recursively.
*/

#include <stdio.h>
#include <stdlib.h>

/* declaration of structure */
typedef struct node {
    int data;
    struct node *next;
} NODE;

/* declaration of functions */
NODE* insert_node(NODE *ptr, NODE *new);
NODE* find_node(NODE *ptr, int n);
NODE* delete_node(NODE *ptr, int n, int *success_flag_ptr);
void print_backward_iteration(NODE *ptr);
void print_backward_recursion(NODE *ptr);

int main(int argc, char *argv[]) {
    /* variables */
    int option;
    int input;
    NODE * head;
    int * success_flag_ptr;
    NODE * new;
    NODE * tmp;
   
    /* initialize variables */
    head = NULL;
    option = 0;
    success_flag_ptr = (int *)malloc(sizeof(int));
    while (option != 6) {...

By purchasing this solution you'll be able to access the following files:
Solution.c.

50% discount

Hours
Minutes
Seconds
$34.00 $17.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats