PROGRAM MUST WORK WHEN COMPILING WITH gcc -ansi -pedantic –Wall assignment10.c -
ID: 3822388 • Letter: P
Question
PROGRAM MUST WORK WHEN COMPILING WITH
gcc -ansi -pedantic –Wall assignment10.c -o assignment10
C SKELETON FILE:
/*
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[])
{
return 0;
}
/* definition of function insert_node */
NODE* insert_node(NODE *ptr, NODE *new)
{
}
/* definition of function find_node */
NODE* find_node(NODE *ptr, int n)
{
}
/* definition of function delete_node */
NODE* delete_node(NODE *ptr, int n, int *success_flag_ptr)
{
}
/* definition of function print_backward_iteration */
void print_backward_iteration(NODE *ptr)
{
}
/* definition of function print_backward_recursion */
void print_backward_recursion(NODE *ptr)
{
}
Build nked 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 inte r 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 nt 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 youto implement both strategies. You should provide the user with a menu with six options to pick from: Insert integer into linked list Find integer in linked list 3 Delete integer from linked list Print out integers backward using the iterative strategy Print out integers backward using the recursive strategy 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 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 thelist, 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 1/2 A success flag p tr pointer to an integer should be used to indicate whether the node was successfully deleted or not. Refer to Lecture 9, Slide 20 Your program should have five functions (operations on linked lists) that implement respectively: i) 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.Explanation / Answer
The functions are as follows:
1) NODE* insert_node(NODE* ptr,NODE *new,int n)
{
new->data=n;
new->next=ptr;
return new;
}
2) NODE* find_node(NODE* ptr,int n)
{
if(ptr==NULL)
return;
else if(ptr->data==n)
return ptr;
find_node(ptr->next);
}
3) NODE* delete_node(NODE* ptr, int n,int *success_flag_ptr)
{
NODE* p=NULL,*q=ptr;
*success_flag_prt=1;
while(ptr!=NULL)
{
if(ptr->data==n)
{
if(p==NULL)
return ptr;
else
{
p->next=ptr->next;
return q;
}
p=ptr;
ptr=ptr->next;
}
&success_flag_ptr=0;
return;
}
4) void print_backward_iteration(NODE *ptr)
{
NODE* p,*q,*root;
p=(NODE*)malloc(sizeof(NODE));
root=p;
p->data=ptr->data;
p->next=NULL;
ptr=ptr->next;
while(ptr!=NULL) // storing the linked list in another list in backward direction
{
q=(NODE*)malloc(sizeof(NODE));
q->data=ptr->data;
q->next=p;
p=q;
ptr=ptr->next;
}
while(root!=NULL)
{
printf("%d ",root->data);
root=root->next;
}
}
5) void print_backward_recursion(NODE *ptr)
{
if(ptr==NULL)
return;
print_backward_recursion(ptr->next);
printf("%d ",ptr->data);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.