Sunteți pe pagina 1din 3

Page 1 of 3

DEPARTMENT OF COMPUTER SCIENCE


Rachna College of Engineering & Technology, Gujranwala
(a constituent college of UET Lahore)

CS 212L Data Structures & Algorithms Lab 02 Handout


Name: Registration No:
Marks: Evaluation Comments:

Lab objectives:
• In this lab you will learn the implementation of pointer based list structure to overcome the
issues of array based list structure
• Batter understanding of pointer operations
• Using structures and pointers to create own data type and batter understanding of memory
usage

Theory / Syntax / Example

1 Node Structure
• List is simply chain of elements
• We created a node structure to represent single element of list
• For single element we need to store data and link together
• End of list is identified using null
• Start of list is saved in a pointer named head
• This structure is self referential structure

Note: if your block or node requires to save different type of data then create node structure
with that data types.

2 Dynamic List Structure


• By connecting different nodes we can implement the list structure
• Take care of pointer and do not lose the reference to any required node
• To Insert elements create the node and then adjust the pointer positions

Session BSCS 2018 2nd Semester September 17, 2019 Teacher: Shahzad Aslam
Page 2 of 3

• To delete the nodes first de-attach the node from the list and adjust the pointers, then use
delete to remove it from memory
• To traverse the list take a temporary pointer pointing to starting node, use loop and move
the pointer position and process the list as required

Lab Tasks
1. Implement the given methods of list structure using node structure and pointers
a) Create node structure that can store integers
b) Create list class, declare data members as discussed and write constructor
• Write main method and create a list object
c) Write display method that display the list each element on new line. Take care
of head (it should not move or any other thing should not change)
• Call this method in main to test
d) Write insert method that accept an integer and place it at the start of list.
Properly adjust the pointer.
• Call this method in main to insert 21, 54, 67, 88, 44, 33, 48, 44, 109,
133 in the list, and display to confirm
e) Write insert method that accept object of node and copy its data to insert at the
start of list. Properly adjust the pointer.
• In main create a node with 189 and call this method to insert and
display to confirm
f) Write removeFirst method that remove first element from the list and return
true if element is removed. Implement the cases as discussed in theory.
Properly take care of pointers.
• Call this method in main to remove first element and display to confirm
g) Write search method that accepts a number and return its position in the list
• Call this method to find 67
h) Write replaceAll method that accepts two numbers, search first number from
the list and replace with second and return how many numbers are replaced
• Call this method to replace 44 with 99 and display to confirm
i) Write get method that accepts an index and return element found at that index
• Call this method to find, 44 and 109
j) Write size method that return number of elements in the list
• Call this method to determine list size

2. Using above class implement the Find Square of task as given in last lab.

Session BSCS 2018 2nd Semester September 17, 2019 Teacher: Shahzad Aslam
Page 3 of 3

Homework:
Create MyString class that will work just like string in C++.
Use list structure inside the MyString class to hold chain of characters properly define the
required node structure.
MyString class should have following methods
Method Description
int length(); Return length of string
int search(char c, int s=0); Search given c from string starting from s position
and return index where it found
int replace(char x, char y) Replace first occurrence of character x to y and
return number of characters replaced
int count(char c); Count c from string and return it
bool replace(char c, int pos); Replace character at index pos with c and return bool
bool remove (char c); Remove first occurrence of c from list
int removeAll(char c, int pos=0); Remove all occurrence of c from list starting from
pos and return ele removed
bool insert(char ch, int pos=0) Insert character ch at specified position in list
bool shuffle(int x, int y) Swap the characters at x, y

Write arrange word game.

1. Create a notedpad file having list of words each on new line


2. Read a word from file, randomly shuffle its characters and display
3. Ask the user to move the characters by asking positions and re-arrange the word (if user say
-1 then he is done)
4. Check that he made correct word
5. Repeat from step 2

Advance task:

Write driver program that implement the NotePad – Read Only. Create Options menu

a) Open
b) Save
c) Show Summary
d) Find
e) Find & Replace
f) Find & Replace All

Open: When user select open, then it should read a text file into MyString and display it on screen
Save: Save the MyString to a file
Show Summary: show given
Size: 1025 bytes
Spaces: 345
Find: Take a character and report its position
Find & Replace: Take two characters and find first and replace with second

Session BSCS 2018 2nd Semester September 17, 2019 Teacher: Shahzad Aslam

S-ar putea să vă placă și