Вы находитесь на странице: 1из 6

LAB ACTIVITY 4: Linked List

Lab Activity :3

Allocated Time : 90 minutes

 Pre-Test : 10 menit
 Practices : 60 menit
 Post-Test : 20 menit
 dst

Total Score : 100%

 Pre-Test : 20 %
 Practices : 50 %
 Post-Test : 30 %
 dst

After completing this lab activity, students are expected to understand how to create linked list, how
to impelement operations on Linked List such as Insert, Delete, isEmpty, Display, etc.

a. Ability to create Linked List
b. Ability to create data and methods of class Linked List


Linked list is a collection of elements, where each element has data and a pointer to the next
element of the linked list. The first element of linked list is pointed by head. It means that head
stores the address of the first element of linked list.

Ilustrations of linked list.

Basic Operations on Linked List:
1. Insert Element
Inserting new element on Linked List can possibly happen in three different situation,
insertion on Empty List, insertion on the middle of List, insertion on the last data on Linked

2. Delete Element
Deleting element on a certain position of Linked List

3. Check if List is Empty List or not

If the Linked List is empty, the head should be NULL.

4. Display List
Display all elements of Linked List

5. Find Data on List

Return the index where the data is located in Linked List


a) Computer
b) Dev C++


1. Open Dev C++ and create a new file named “List.cpp”

2. Next, We will create a class called Node and specify the data and methods as follows.

#include <iostream>
using namespace std;

class Node {
double data; // data
Node* next; // pointer to next

2. Create another class called List, a List have head and also several methods
In the constructor of List, head is assigned with NULL. Because when a List is created, it
does not have element.

class List {

List(void) { head = NULL; } // constructor
~List(void); // destructor

bool IsEmpty() { return head == NULL; }

Node* InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);

Node* head;

3. Create isEmpty method to check if a List is empty or not.

bool List::IsEmpty()
return (head == NULL)

4. Create InsertNode method as follows. Dont copy paste the code otherwise it will turns to
error. Type your own code.

Node* List::InsertNode(int index, double x) {

if (index < 0) return NULL;

int currIndex = 1;
Node* currNode = head;
while (currNode!=NULL && index > currIndex) {
currNode = currNode->next;
if (index > 0 && currNode == NULL) return NULL;

Node* newNode = new Node;

newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
else {
newNode->next = currNode->next;
currNode->next = newNode;
return newNode;

5. Create a new method called AddLastNode(double x)

This function basically add a new Node behind the last element of the Linked List. So you
have to traverse/enumerate all the element to find the last element of the Linked List. After
you find the last element of the linked list, add the new Node there.

Void AddLastNode (double T2)

...... // type your code here, print the result

6. Implement DeleteNode as follows. It will delete the node which consist of data x.

int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
else {
head = currNode->next;
delete currNode;
return currIndex;
return 0;
7. Create DisplayList() method. This method should read all data in the linked list and
display them. Hint: Read the data of the first element and so forth, until you reach the last


8. Create main program as follows

int main ()
List L1;
Node *N1;
Node *N2;
Node *N3;
Node *N4;

cout<<L1.isEmpty() <<endl; //should return true because L1 is just created

N1 = L1.InsertNode(5.0);
N2 = L1.InsertNode(6.0);
N3 = L1.InsertNode(15.0);
N4 = L1.InsertNode(15.0);



Create more methods on your Linked List

1. Add a method to display data which greater than 10.0
2. Add a method “DeleteLast()” to delete last element of Linked List.
3. Add a method to Join two Linked Lists!! (clue: last element of the
first list should be connected to the first element of another List)