Skip to content
VUBuddy

CS301 Midterm Notes Set 2

Data Structures PDF 1.1 MBUpdated 23 Oct 2026
Midterm past paper CS301 Midterm Notes Set 2 PDF 1.1 MB
vubuddy.com
Type
Midterm past paper
Course
CS301 Data Structures
Size
PDF 1.1 MB
Updated
23 Oct 2026
Free download 0 of 3 used today
Download free · 1.1 MB

You get 3 free files a day on the website. Add the Chrome extension for 30 a day.

Unlimited downloads in the Android app Get it free on Google Play

Preview text

Text from pages 1, 2, 3 of 20. Download the file for all 20 pages.

Data Structure CS301

Introduction to Data Structures: Data structures help us to organize the data in the computer, resulting in more efficient programs. An efficient program executes faster and helps minimize the usage of resources like memory, disk.

What does organizing the data mean?

It means that the data should be arranged in a way that it is easily accessible.

Data Structure Philosophy

a data structure may be better than another one in all situations. There are three basic things associated with data structures. A data structure requires:

  1. space for each data item it stores
  2. time to perform each basic operation
  3. programming effort

What is x? x is a name of collection of items. int x[6]; ‘x’ is an array’s name but there is no variable x. ‘x’ is not an lvalue. If some variable can be written on the left-hand side of an assignment statement, this is lvalue variable.

List data structure

The List data structure is among the most generic of data structures. A list is the collection of items of the same type. The items, or elements of the list, are stored in some particular order. List is a set of elements in a linear order.

Operation Name

createList() Create a new list (presumably empty) copy() Set one list to be a copy of another clear(); Clear a list (remove all elements) insert(X, ?) Insert element X at a particular position in the list remove(?) Remove element at some position in the list get(?) Get element at a given position update(X, ?) Replace the element at a given position with X find(X) Determine if the element X is in the list length() Returns the length of the list. If we use the “current” marker, the following four methods would be useful:

Functions

start() Moves the “current” pointer to the very first element tail() Moves the “current” pointer to the very last element next() Move the current position forward one element back() Move the current position backward one element

How to implement the interface. Suppose we want to create a list of integers. For this purpose, the methods of the list can be implemented with the use of an array inside. for simplification purposes, it is good to use the index from 1.

Add Method

To add the new element (9) to the list at the current position, at first, we have to make space for this element. For this purpose, we shift every element on the right of 8 (the current position) to one place on the right. (2, 6, 8, 9, 7, 1) Now in the second step, we put the element 9 at the empty space i.e. position 4.

next Method

We have talked that the next method moves the current position one position forward. In this method, we do not add a new element to the list but simply move the pointer one element ahead.

remove Method

We have seen that the add method adds an element in the list. The remove method removes the element residing at the current position.

find Method

The find (x) function is used to find a specific element in the array. We pass the element, which is to be found, as an argument to the find function. This function then traverses the array until the specific element is found. If the element is found, this function sets the current position to it and returns 1 i.e. true. On the other hand, if the element is not found, the function returns 0 i.e. false. This indicates that the element was not found. Following is the code of this find(x) function in C++. int find (int x)

int j ; for (j = 1; j < size + 1; j++ ) if (A[j] == x ) break ; if ( j < size + 1) // x is found

current = j ; //current points to the position where x found return 1 ; // return true

return 0 ; //return false, x is not found

get() method , used to get the element from the current position in the array. method length( ).This method returns the size of the list. back() method decreases the value of variable current by 1. In other words, it moves the current position one element backward. current -- ; The start() method sets the current position to the first element of the list.

Analysis of Array List

Time is the major factor to see the efficiency of a program.

List using Linked Memory

In an array, the memory cells of the array are linked with each other. It means that the memory of the array is contiguous. In an array, it is impossible that one element of the array is located at a memory location while the other element is located somewhere far from it in the memory. It is located in very next location in the memory. Linked List For the utilization of the concept of linked memory, we usually define a structure, called linked list. To form a linked list, at first, we define a node. A node comprises two fields. i.e. the object field that holds the actual list element and the next that holds the starting location of the next node. A chain of these nodes forms a linked list. We use head and current variable names instead of using the memory address in numbers for starting and the current nodes.

There is the limitation that array being of fixed size can only store a fixed number of elements. Therefore, no more elements can be stored after the size of the array is reached. In order to resolve this, we adopted a new data structure called linked list.

Linked List Operations

The linked list data structure provides operations to work on the nodes inside the list. The Add(9) is used to create a new node in the memory at the current position to hold ‘9’. You must remember while working with arrays, to add an element at the current position that all the elements after the current position were shifted to the right and then the element was added to the empty slot.(Node * newNode = new Node(9);) Hence, the whole statement means:“Call the constructor of the Node class and pass it ‘9’ as a parameter. After constructing the object in memory, give me the starting memory address of the object. That address will be stored in the pointer variable newNode.” When we write class in C++, normally, we make two files (.h and .cpp) for a class. The .h file contains the declarations of public and private members of that class. The public methods are essentially the interface of the class to be employed by the users of this class. The .cpp file contains the implementation for the class methods that has the actual code. But this is not mandatory. In the code given above, we have only one file .cpp, instead of separating into two files. The second method in the above-mentioned class is set() that accepts a parameter of type int while returning back nothing. The accepted parameter is assigned to the internal data member object. The next method is getNext() which returns a pointer to an object of type Node lying somewhere in the memory. It returns nextNode i.e. a pointer to an object of type Node. As discussed above, nextNode contains the address of next node in the linked list. The last method of the class is setNext() that accepts a pointer of type Node, further assigned to nextNode data member of the object. This method is used to connect the next node of the linked list with the current object. It is passed an address of the next node in the linked list. (We normally use the arrow (-> i.e. minus sign and then the greater than sign) to manipulate the structure’s and Class’s data with pointers. So to access the name with sptr we will write: sptr->name; While accessing through a simple variable, use dot operator i.e. s1.name While accessing through the pointer to structure, use arrow operator i.e. sptr- >name;)

Methods of Linked List

the start method that has the following code. // position currentNode and lastCurrentNode at first element void start() { lastCurrentNode = headNode; currentNode = headNode;

We will now see how a node can be removed from the link list. We use the method remove for this purpose. void remove() { if( currentNode != NULL && currentNode != headNode) { (step 1) lastCurrentNode->setNext(currentNode->getNext()); (step 2) delete currentNode; (step 3) currentNode = lastCurrentNode->getNext(); (step 4) size--;

The next method is length() that simply returns the size of the list. The code is as follows: // returns the size of the list int length()

return size;

Analysis of Link List

add

For the addition purposes, we simply insert the new node after the current node. So ‘add’ is a one-step operation. We insert a new node after the current node in the chain. if we have to add an element in the centre of the array, the space for it is created at first. For this, all the elements that are after the current pointer in the array, should be shifted one place to the right.

remove

Remove is also a one-step operation. The node before and after the node to be removed is connected to each other. Update the current pointer. Then the node to be removed is deleted. As a result, the node to be removed is deleted.

find

The worst-case in find is that we may have to search the entire list. In find, we have to search some particular element say x. If found, the currentNode pointer is moved at that node. As there is no order in the list, we have to start search from the beginning of the list. We have to check the value of each node and compare it with x (value to be searched). If found, it returns true and points the currentNode pointer at that node otherwise return false. Suppose that x is not in the list, in this case, we have to search the list from start to end and return false.

back

In the back method, we move the current pointer one position back. Moving the current pointer back, one requires traversing the list from the start until the node whose next pointer points to current node. Our link list is singly linked list i.e. we can move in one direction from start towards end.

Doubly-linked List

In doubly-link list, a programmer uses two pointers in the node, i.e. one to point to next node and the other to point to the previous node. Now our node factory will create a node with three parts. First part is prev i.e. the pointer pointing to the previous node, second part is element, containing the data to be inserted in the list.

Circularly-linked lists

The next field in the last node in a singly-linked list is set to NULL. The same is the case in the doubly-linked list. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers i.e.

All CS301 files →
CS301 Data Structures All CS301 handouts, past papers, quizzes and MCQs in one place. Open course page

Get more out of VUBuddy, for free

Android app Free on Google Play
  • Unlimited downloads
  • The explanation behind every MCQ answer
  • Study offline on slow data
Get the Android app
Chrome extension Free on the Chrome Web Store
  • 30 downloads a day instead of 3
  • Sign in and buy Pro Semester here
Add to Chrome

One Pro Semester membership works in both the extension and the app.