You are on page 1of 7

GIFT School of Engineering

and Applied Sciences

Spring 2019

CS-204: Data Structures and Algorithms

Lab-8 Manual
Linked List

v1.0
5/18/2019
Lab-8 Manual 2019

List
A list provides a way to organize data. We can have to-do lists, gift lists, address lists, grocery
lists, even lists of lists. These lists provide a useful way for us to organize our lives, as illustrated
in Figure given below. Each list has a first item, a last item, and usually items in between.
That is, the items in a list have a position: first, second, and so on. An item’s position might be
important to you, or it might not. When adding an item to your list, you might always add it at
the end, or you might insert it between two other items already in the list.

Specifications for the List


Everyday lists such as to-do lists, gift lists, address lists, and grocery lists have entries that are
strings. What can you do to such lists?
1. Typically, you add a new entry at the end of the list.
2. Actually, you can add a new entry anywhere: at the beginning, at the end, or in between
items.
3. You can cross out an entry—that is, remove it.
4. You can remove all entries.
5. You can replace an entry.
6. You can look at all of the entries.
7. You can find out whether the list contains a particular entry.
8. You can count the number of entries in the list.
9. You can see whether the list is empty.
When you work with a list, you determine where an entry is or should be. You probably are not
conscious of its exact position: Is it tenth? Fourteenth? However, when your program uses a list,

Page 1
Lab-8 Manual 2019

a convenient way to identify a particular entry is by the entry’s position within the list. It could
be first, that is, at position 1, or second (position 2), and so on. This convention allows you to
describe, or specify, the operations on a list more precisely.
The following is an initial specification of the list:
1. add(newEntry) : Adds a new entry to the end of a list.
2. add(newPosition, newEntry) : Adds a new entry at a given position in a list.
3. remove(givenPosition) : Removes the entry at a given position from a list.
4. clear() : Removes all entries from a list.
5. replace(givenPosition, newEntry) : Replaces the entry at a given position
in a list with a given entry.
6. getEntry(givenPosition) : Retrieves the entry at a given position in a list.
7. contains(anEntry) : Sees whether a list contains a given entry.
8. getSize() : Gets the number of entries in a list.
9. isEmpty(): Sees whether a list is empty.
10. displayList() : Display all values from the list
11. incSize() : To increment the size by 1
12. descSize() : To decrement the size by 1

Page 2
Lab-8 Manual 2019

Task #1: Wrapper List


1. Read LList.java, Node.java, LListClient.java and
ListInterface.java files carefully.

Note: You may copy these files from the Code folder.

2. Create another class WrapperList.java with the main method.


3. Write a Display method, which display all the entries from the list.

Note: You may use the displayList(ListInterface list) method from the
LListClient.java class provided to you in the code folder.

4. Create a new list IntegerList using the LList class of type Integer.
5. Add 5 new entries in list.
6. Display the list using the displayList method.
7. Add another new entry at position 6.
8. Display the list using the displayList method.
9. Remove an entry at position 2 and then display the list.
10. Replace the value at position 1 with a new entry and display list.
11. Repeat the Same process from point 4 to onwards for the types Double and Character as
well.

Page 3
Lab-8 Manual 2019

Task #2: Student List


1. Implement the class given below

2. Create another class StudentList.java with the main method.


3. Write a Display method, to display all the entries studentList

Note: You may use the displayList(ListInterface list) method from the
LListClient.java class provided to you in the code folder.

4. Create a new list studentList using the LList class.


5. Add 2 new objects of student in the studentList you have created.
6. Display the list using the displayList method.
7. Add another new student at position 3.
8. Display the list using the displayList method.
9. Remove an entry at position 1 and then display the list.
10. Replace the student at position 1 with a new student entry.

Page 4
Lab-8 Manual 2019

Task #3: Update List


Our List is working perfectly but the problem is whenever I add a new entry it always added at
the end of the list or I can use the add a new entry at a position as explained below

If I wanted to add a new entry it will add at the end, for example if I add a new number 120 my
list will look like

Or I can use the position method, for example I wanted to add a new number 200 at position 2
my list will look like

But I wanted to update the list in the way which add the entry at the beginning that means when I
add a new number 300 my list will look like

I can do it by using the Add at position method, but you are not supposed to use that method but
need to update the our LList class by writing some other methods

 addBeginning
 updateBeginning
 removeBeginning

Page 5
Lab-8 Manual 2019

In order to write these methods in LList first you need to add new methods in the Interface as
well.

1. Add three methods addBeginning. updateBeginning and removeBeginning in the


ListInterface.

You may use the following headers for the methods

public void addBeginning(T newEntry);


public T updateBeginning(T newEntry);
public T removeBeginning();
2. Implement these methods in AList class in such a way whenever I call these methods
these will do the things they are supposed to do i.e. adding, updating and removing the
values from the beginning.

Page 6

You might also like