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

You are given three skeleton files: list.cpp, list.h and main.cpp. The files lis

ID: 3661506 • Letter: Y

Question

You are given three skeleton files: list.cpp, list.h and main.cpp. The files list.cpp and list.h contain a skeleton of a simple linked list implementation. The structure of a node of the linked list is as follows struct Node int value struct Node *next: // pointer field // data field where each node contains 2 fields: an integer value and a pointer to the next node in the list. Your program needs to read in a sequence of numbers from the user input, two numbers at a time until receiving 0 0 For every two numbers the user enters, the first one is the value to be added to the linked list, and the second one indicates whether the value should be added to the front or the end of the list. If the second number is 1, then the first number should be added to the front of the list. Otherwise, it should be added to the end of the list Except the last two numbers, all other are non-zero Finally, your program needs to print the values in the linked list from the first to the last node For example, if the user enters the following sequence 12 2 34 2 21 1 56 2 38 1 94 1

Explanation / Answer

main.cpp

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include<cstdio>
#include"list.h"
int main(){
    Node *list=NULL;
    int input,pos;
    int size;
  
    do{
        std::scanf("%d%d",&input,&pos);
   
      if(pos==1)
      AddToFront(list,input);
      else
      AddToEnd(list,input);
        }while(input!=0);
    size=Count(list);
    std::printf("list has %d numbers",size);
    PrintList(list);
    FreeList(list);
    }

list.cpp

#include<cstdio>
#include<cstdlib>
#include"list.h"
Node *MakeNode(int value)
{
    Node *pNode=new Node;
    pNode->value=value;
    pNode->next=NULL;
    return pNode;
}
void PrintList(struct Node *list)
{
    while(list)
    {
        printf("%d",list->value);
        list=list->next;
    }
    printf(" ");
}
int Count(struct Node *list){
    int count=0;
    while(list){
        count++;
        list=list->next;
    }
    return count;
}
void AddToEnd(Node *pList,int value){
    while(pList)
    pList=pList->next;
    Node *n=MakeNode(value);
    pList->next=NULL;
    }
  
    void AddToFront(Node *pList,int value)
    {
        Node *n=MakeNode(value);
        n->next=pList;
        pList=n;
      
    }
    void FreeList(Node *list)
    {
        free(list);
    }

list.h

#ifndef LIST_H
#define LIST_H
struct Node{
int value;
struct Node *next;
};
void AddToEnd(Node **pList,int value);
void AddToFront(Node **pList,int value);
int Count(Node *list);
/*void FreeList(Node *list);*/
void PrintList(Node *list);
#endif

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