Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sources: linkedlist.cpp #include <iostream> #include \"linkedlist.h\" using name

ID: 3782317 • Letter: S

Question

Sources: linkedlist.cpp

#include <iostream>
#include "linkedlist.h"
using namespace std;

LinkedList* NewLinkedList()
{
LinkedList* llp = new LinkedList;

llp->head = new node;

llp->head->next = 0;

return llp;
}

void InsertFirst(LinkedList* llp, int val)
{
node* p = new node;
p->data = val;
p->next = llp->head->next;
llp->head->next = p;
}

void Print(LinkedList* llp)
{
for (node* p = llp->head->next; p != 0; p = p->next) {
cout << p->data << " ";
}
}

void InsertLast(LinkedList* llp, int val)
{

node* p;

for (p = llp->head; p->next != 0; p = p->next)
{
}

// p now points to the last node in the list

// For you to do: Create a new node, store val in that node, and link the new node after p.


}

Sources: hw1.cpp

#include <iostream>
#include "linkedlist.h"
using namespace std;

int main()
{
LinkedList* llp = NewLinkedList();

InsertFirst(llp, 10);
InsertFirst(llp, 20);
InsertFirst(llp, 30);

cout << endl;
Print(llp);
cout << endl;

InsertLast(llp, 50);
InsertLast(llp, 60);
InsertLast(llp, 70);

cout << endl;
Print(llp);
cout << endl;

return 0;
}

Header: linkedlist.h

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED

struct node {
int data;
node* next;
};

struct LinkedList {
node* head;
};

LinkedList* NewLinkedList();
void InsertFirst(LinkedList* llp, int val);
void Print(LinkedList* llp);
void InsertLast(LinkedList* llp, int val);

#endif // LINKEDLIST_H_INCLUDED

-----------------------------

Your assignment is to complete the function InsertLast in linkedlist.cpp, compile, and run. The correct output should be:

30 20 10

30 20 10 50 60 70

Explanation / Answer

source: linkedlist.cpp

#include <iostream>
#include "linkedlist.h"
using namespace std;
LinkedList* NewLinkedList()
{
LinkedList* llp = new LinkedList;
llp->head = new node;
llp->head->next = 0;
return llp;
}
void InsertFirst(LinkedList* llp, int val)
{
node* p = new node;
p->data = val;
p->next = llp->head->next;
llp->head->next = p;
}
void Print(LinkedList* llp)
{
for (node* p = llp->head->next; p != 0; p = p->next) {
cout << p->data << " ";
}
}
void InsertLast(LinkedList* llp, int val)
{
node* p;
for (p = llp->head; p->next != 0; p = p->next)
{
}

node* temp = new node;
temp->data = val;
temp->next = 0;
p->next = temp;
}

source linkedlist.h

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
struct node {
int data;
node* next;
};
struct LinkedList {
node* head;
};
LinkedList* NewLinkedList();
void InsertFirst(LinkedList* llp, int val);
void Print(LinkedList* llp);
void InsertLast(LinkedList* llp, int val);
#endif // LINKEDLIST_H_INCLUDED

source hw1.cpp

#include <iostream>
#include "linkedlist.h"
#include "linkedlist.cpp"
using namespace std;
int main()
{
LinkedList* llp = NewLinkedList();
InsertFirst(llp, 10);
InsertFirst(llp, 20);
InsertFirst(llp, 30);
cout << endl;
Print(llp);

cout << endl;

InsertLast(llp, 50);
InsertLast(llp, 60);
InsertLast(llp, 70);
cout << endl;
Print(llp);
cout << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote