C++ help me to do this code please. All, For those of you that are interested, I
ID: 3826160 • Letter: C
Question
C++ help me to do this code please.
All,
For those of you that are interested, I am going to offer an Extra Credit assignment. This assignment will involve creating a LinkedList in C++. The requirements are:
Must have a Node.h/Node.cpp class with proper accessor/mutator functions to perform operations
Node class must use a Class Template that allows the LinkedList to use any data type
Main method must invoke at least 2 instances of a Node that uses different data types (ie., int, double, string etc.)
Main method must also accept the size of the both lists from user input
Files to include:
Node.h
Node.cpp
main.cpp
README
You can refer to chapters 8/13 as good starting points for the assignment. This is a chance to make some points you may have missed in a quiz for those of you that are interested in helping your grade a bit.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void deleteNode(struct node *head, struct node *n)
{
if(head == n)
{
if(head->next == NULL)
{
printf("There is only one node. The list can't be made empty ");
return;
}
head->data = head->next->data;
n = head->next;
head->next = head->next->next;
free(n);
return;
}
struct node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return;
}
prev->next = prev->next->next;
free(n);
return;
}
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;
}
void printList(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf(" ");
}
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.