Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implementation of the List ADT by use of a backing Linked List Program name: FriendLink.java implement the behaviors of the List Abstract Data Type (
Implementation of the List ADT by use of a backing Linked List
Program name: FriendLink.java
implement the behaviors of the List Abstract Data Type ADT by using a java Linked List as storage.
These behaviors are: size get set add remove We will use a singly linked list which stores the names of my friends in the order I want in its nodes.
We implement all the List ADT commands.
ADD index name
Implements the add list behavior
REMOVE index
Implements the remove list behavior
SET index name
Implements the set list behavior
GET index
Implements the get list behavior
PRINT
Prints out the current list and current size of the list
size implemented
Extra command which is not for List but needs to implement
CONTAINS name
Determines if nameis in the list if so prints out the index position of the first occurrence of name. If not, it simply reports that name is not in the list.
Generally, each command and its arguments are typed at the CLI all in one line with one blank space separating the entities. So the input lines are blankspaceseparated Strings.
More on this program
I have only a few friends but occasionally, I will add a new one or even lose one! I will create a java linked list to keep track of the first names of my friends. I want the Linked List to list the friends I have now, but also to grow in case I add any new friends. There is also the possibility of losing a friend sometimes!
So I want a linked list that names all my friends in the order I want them in and I want to implement the ADT List operations in the table above for my list.Unlike the array case, we make no assumptions about how many friends I might have as the linked list can grow as needed.
start the program with an initial list of friends But the first job of your program is to put these names in proper linked order into the nodes of a linked list.
String friend AnnieElbertAndrewHarryCharlesMaryFrankSandyElvis; line
program will use a Scanner to repeatedly input a oneline request presenting any of the commands in the table above and then responds to that command then goes back and reads in another command until an EOF condition is hit. With commands being typed in at the CLI, an EOF condition is created by controlz followed by enter key. The main loop of your program uses a while statement of the form:
whileschasNextLine where sc points to your Scanner
Before starting the loop to input commands the program should print out the initial list and the initial size of the list which is
Your program should define Node and Linked List as in the picture below:
You program starts by creating an empty Linked List object ls as below:
LinkedList ls new LinkedList;
This is ultimately to contain nodes for all the names of my friends. This first job of the program is to populate the linked list one node at a time creating a node for each name in the friend array shown above and linking each to the appropriate next name via its next field. So you go through the friend array element by element, and for each element create a new Node for the friendi as i runs from to friend.length
So you create:
new Nodefriendi
and you make sure the previous node you had links to this.
If the initial list has "Annie" first then the node you make for "Annie" should be the head of the list so
lshead new Nodefriend;line alpha
and ultimately when you get to "Elvis" in the list the node you make for that will be the tail of the list.
To build the initial linked list ls as you sequence through it you will need a general node pointer current to point to the node you are currently on Assuming you used line alpha above you could say
Once the program has built the linked list as above as part of initialization you want to print out the names of all the friends in order by linking through the linked list. Here you can count them as you go along and remember in a variable size how many friends I have. Now you are ready to go into the command reception loop.
Your command reception while loop and your switchcase statement for the various commands these should be exactly based on a static array.
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