// ********************************************************* // Header file for
ID: 3654355 • Letter: #
Question
// *********************************************************
// Header file for your main program
// Pointer-based implementation.
//.....
//.....
// *********************************************************
#include <iostream>
#include "List.h"
using namespace std;
int main()
{
List myList, otherList; //list objects to test List implementation
int choice; //user choice
int value; //value to add or value in the deleted node
// ask user to choose operations on list
do
{
//display options
cout<<" "
<<" 1: add a new node at the front of myList "
<<" 2: add a new node at the end of myList "
<<" 3: delete the first node of myList "
<<" 4: delete last node of myList "
<<" 5: display myList "
<<" 6: copy myList to otherList "
<<" 7: display otherList "
<<" 8: quit ";
cout<<"choose your option from the above: ";
cin>>choice;
switch (choice)
{
case 1:
//add a new node at the front of myList
cout<<"item value? ";
cin>>value;
myList.insertFront(value);
break;
case 2:
//add a new node at the end of myList
cout<<"item value? ";
cin>>value;
myList.insertBack(value);
break;
case 3:
//delete the first node of myList
myList.removeFront(value);
cout<<value<<" is removed from myList ";
break;
case 4:
//delete last node of myList
myList.removeBack(value);
cout<<value<<" is removed from myList ";
break;
case 5:
//display myList
myList.printList();
break;
case 6:
//assign myList to otherList
otherList = myList;
break;
case 7:
//display otherList
otherList.printList();
break;
case 8:
break;
} //end of switch
} while (choice <8);
return 0;
} // end main
Explanation / Answer
//use this function in case of (a) List addFront(List list, int value) { List element, temp; if (list == NULL) { element = newElement(); setElement(element, value); element->next = temp; } else { list->next = addEndIterative(list->next, value); } temp = list; return(element); } //use this function for case(b) int addNode( int val, node *head, int pos ){ /* create new node */ node *newNode = (node*) malloc( sizeof(node) ); if ( newNode == NULL ) { fprintf(stderr, "Unable to allocate memory for new node "); exit(-1); } newNode->value = val; /* insert new node */ int i; node *tmp = head; for(i=0 ; inext; if(tmp == NULL){ fprintf(stderr, "End of list reached, invalid position "); } } newNode->next = tmp->next; tmp->next = newNode; return 0; } //for displaying list int printList( node *head ) { node *tmp = head->next; printf("Linked list: "); if( head->next == NULL ){ printf("Empty"); } else{ while( tmp != NULL ){ printf("%d ", tmp->value); tmp = tmp->next; } } printf(" "); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.