Question

In: Computer Science

Sources: linkedlist.cpp #include <iostream> #include "linkedlist.h" using namespace std; LinkedList* NewLinkedList() { LinkedList* llp = new...

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

Solutions

Expert Solution

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;
}


Related Solutions

--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT