Answered step by step
Verified Expert Solution
Link Copied!

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 character/digit 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 character/digit 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 1
A 2
B
B
B
B
The above file creates a linked list with 2 nodes (1 and 2), current, points to 2. Once the first B is read, Current moves to node (1). when the second B is read, current won't move, it will stay pointing to node (1). 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

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

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions