Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a list list1 to store integers and insert 5 nodes to the list as shown above using bool insert(int, type) . Write a function

image text in transcribed

  1. Create a list list1 to store integers and insert 5 nodes to the list as shown above using bool insert(int, type).
  2. Write a function bool printList(List) in main to print all the nodes in a list. This function can be used to print any list that contains different data types. Use the printList function to print the list after insert items in (a).
  3. Write statements to traverse the list and multiple the node value by 2 if the current value is an odd value. Print out the list after the operation.
  4. Write a statement to delete node 4 from the list and print out the list after deletion.
  5. Create another list called list2 to store integer and try to insert the following integers (3, 6, 2, 9, 7) from left to right using the second insert function bool insert(type). Print out the list after insert. Is content in list2 same as list1 in (a)? If not, why is it not the same?

app.cpp

#include #include #include "List.h" //#include "Person.h"

using namespace std;

bool printList(List);

int main() { cout

cout

cout

cout

system("pause"); return 0; }

List.cpp

#include #include "List.h"

using namespace std;

//typedef Person type;

using type = int;

List::List() { head = NULL; count = 0; }

bool List::empty() { if (count==0) return true; return false; }

int List::size() { return count; }

Node *List::find(int position) { Node *cur; if (position > count) return NULL; cur = head; for (int count=1; countnext; return cur; }

bool List::get(int position, type &result) { if (position > count) return false; result = find(position)->item; return true; }

bool List::set(int position, type newItem) { if (position > count) return false; find(position)->item = newItem; return true; }

bool List::insert(int at, type newItem) {// Any simplification can be done on code below? Node *pre, *cur, *tmp = new Node(newItem);

if (at count+1) return false; if (!tmp) return false;

if (empty()) { head = tmp; count++; return true; } if (at == 1) { tmp->next = head; head = tmp; count++; return true; } pre = find(at-1); cur = pre->next; tmp->next = cur; pre->next = tmp; count++; return true; }

bool List::remove(int from) { Node *pre, *cur;

if (from count) return false; if (from == 1) { cur = head; head = head->next; count--; free(cur); return true; } pre = find(from-1); cur = pre->next; pre->next = cur->next; free(cur); count--; return true; }

//insert in ascending bool List::insert(type newItem) { Node *pre, *cur, *tmp;

tmp = new Node(newItem);

if (!tmp) return false; if (empty()) { head = tmp; count++; return true; } count++; if (head->item >= newItem) { tmp->next = head; head = tmp; return true; } pre = head; cur = pre->next; for (; cur != NULL;) { if (cur->item >= newItem) break; pre = cur; cur = cur->next; } tmp->next = cur; pre->next = tmp; return true; }

Node.cpp

#include #include "Node.h"

//typedef Person type;

using type = int;

Node::Node(type newItem) { item = newItem; next = NULL; }

7 9 2 6 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions