Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Assignment Problem A company wants to create an automated system that reads input from a file and creates a linked list based on the commands
Assignment Problem
A company wants to create an automated system that reads input from a file and creates a linked list based on the commands in the file. The file contains the following structure:
Action Required: This is a character that states which action to be done on the linked list
Data optional: This is a one characterdigit value that will be processed.
Action Character
An action character can be one of the following:
A: Adds a character to the linked list. The characters to be added follows this command the Data part
B: This moves the "current" cursor in the linked list back one character
D: This deletes the node "current" is pointing to
Data
Data is a one characterdigit value. This item is optional when the Action Character is B or D but is required if the Action Character is A
Sample Run
If the input file contained the following commands:
A A
A I
A N
B
D
A E
If we run the program, it should process each line as follows:
Read A A Add the letter A to the linked list. The linked list now contains A Current pointer points to A
Read A I. Adds the letter I to the linked list. The linked list now contains AI Current pointer points to I.
Read A N Adds the letter N to the linked list. The linked list now contains AIN. Current pointer points to N
Read B This moves the Current pointer back one node. Now Current pointer points to I.
Read D This deletes the node Current pointer points to The linked list now contains AN Current is now pointing to A
Read A E This adds E to the linked list, the linked list now contains AEN. Current is pointing to E
Hint
If you are creating a singly linked list, your node class would be:
class Node
public:
char c;
Node next;
;
If you are creating a doubly linked list, your node class would be:
class Node
public:
char c;
Node next;
Node prev;
;
Things to Take Into Consideration
You have to be aware of several situations when processing the input file. Here are a few of them:
The First Character. Remember, the first character you read is the one that's use to create the linked list. So the linked contains nothing nullptr until the first character is read.
You Can't Move Beyond the First Node. The Current pointer can not move beyond the first node. So if you read a character, then you read a B command, your Current pointer will point to that character. If you read B again, nothing happens because your "Current" pointer is already pointing to the first node.
For example, look at this input file:
A
A
B
B
B
B
The above file creates a linked list with nodes and current, points to Once the first B is read, Current moves to node when the second B is read, current won't move, it will stay pointing to node Similarly reading the second and third B won't do anything to the pointer, since the pointer is already pointing to the first node.
Three Scenarios to Add. When adding a character, take into consideration that you might be adding the new data at the beginning of the linked list, in the middle of the linked list, or at the end of the linked list.
Three Scenarios to Delete. When deleting a character, take into consideration that you might be deleting a character at the beginning of the linked list the first node or in the middle of the linked list, or at the end of the linked list the last node
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started