Vous êtes sur la page 1sur 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

1. 1 OBJECTIVES
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.

1. 2 INDICATOR OF COMPETENCE
a. Ability to create Linked List
b. Ability to create data and methods of class Linked List

1.3 THEORY

Linked list is a collection of elements, where each element has data and a pointer to the next

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
List

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

## 3. Check if List is Empty List or not

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++

2.4. PRACTICES

## 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 {
public:
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 {

public:
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);

private:
};

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

bool List::IsEmpty()
{
}

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

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

if (index < 0) return NULL;

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

## Node* newNode = new Node;

newNode->data = x;
if (index == 0) {
}
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.

{
...... // 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;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
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
element.

void::DisplayList()
{
.....
}

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);
L1.DisplayList();

L1.DeleteNode(6.0);
L1.DisplayList();