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, andlink 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 InsertLastin linkedlist.cpp, compile, and run. The correct outputshould be:

30 20 10

30 20 10 50 60 70