Using the basic Linked List program, (1) Create a linked list from file input da
ID: 3697634 • Letter: U
Question
Using the basic Linked List program, (1) Create a linked list from file input data (2) Print the linked list, with 10 values per line Add new functions: (1) A function gets user input of a value to look for in the list, and reports how many times it appears (could be 0) (2) A function counts the nodes in a list and reports how many there are. (3) A function gets user input of a value to delete, and deletes the first node found which has that value. (Does it matter whether that node is first, last, or in the middle of the list?) The main program calls the functions and shows that they workExplanation / Answer
This is the c program for creating and traversing and deleting and priting of the singly linked list.
save it using appropriate name and run using gcc compiler on the linux environment.
gcc prog_name.c
./a.out
-------------------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
/* structure of a linked list node */
struct node
{
int data;
struct node *next;
};
void deleteNode(struct node *head, struct node *n)
{
// When node to be deleted is head node
if(head == n)
{
if(head->next == NULL)
{
printf("There is only one node. The list can't be made empty ");
return;
}
/* Copy the data of next node to head */
head->data = head->next->data;
// store address of next node
n = head->next;
// Remove the link of next node
head->next = head->next->next;
// free memory
free(n);
return;
}
// When not first node, follow the normal deletion process
// find the previous node
struct node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
// Check if node really exists in Linked List
if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return;
}
// Remove node from Linked List
prev->next = prev->next->next;
// Free memory
free(n);
return;
}
/* Utility function to insert a node at the begining */
void push(struct node **head_ref, int new_data)
{
struct node *new_node =
(struct node *)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
/* Utility function to print a linked list */
void printList(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf(" ");
}
/* Driver program to test above functions */
int main()
{
struct node *head = NULL;
push(&head,3);
push(&head,2);
push(&head,6);
push(&head,5);
push(&head,11);
push(&head,10);
push(&head,15);
push(&head,12);
printf("Given Linked List: ");
printList(head);
printf(" Deleting node %d: ", head->next->next->data);
deleteNode(head, head->next->next);
printf(" Modified Linked List: ");
printList(head);
printf(" Deleting first node ");
deleteNode(head, head);
printf(" Modified Linked List: ");
printList(head);
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.