(C language) Please help with this question. It\'s asking just to fill out the f
ID: 3727528 • Letter: #
Question
(C language) Please help with this question. It's asking just to fill out the functions for the main .c file.
HEADER FILE:
.C FILE
; . Storing and maintaining a linked list. (12 marks) Initialize a single node from a passed string (2 mark) Add a "push" function to add a node to the front of the list (2 marks) Add a "pop" function to remove the first node of a linked list and return a pointer to said node (2 marks) " Deleting Nodes (2 marks) o Delete a single node and free any allocated memory from inside the node (1 mark) o Delete te entire linked list, delete all nodes iteratively (1 mark) Reverse order of the linked list (4 marks)Explanation / Answer
1.Push and Pop operation of stack using linked list.
output:
2.Delete a given node in Linked List under given constraints
We explicitly handle the case when node to be deleted is first node, we copy the data of next node to head and delete the next node. The cases when deleted node is not the head node can be handled normally by finding the previous node and changing next of previous node. Following is C implementation.
#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;
/* Create following linked list
12->15->10->11->5->6->2->3 */
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);
/* Let us delete the node with value 10 */
printf(" Deleting node %d: ", head->next->next->data);
deleteNode(head, head->next->next);
printf(" Modified Linked List: ");
printList(head);
/* Let us delete the the first node */
printf(" Deleting first node ");
deleteNode(head, head);
printf(" Modified Linked List: ");
printList(head);
getchar();
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.