You are on page 1of 7

LIST ADT

1. What is a data structure?

A data structure is a particular way of storing and organizing data in a computer so that it can be used
efficiently.

2. What is an ADT?

An abstract data type (ADT) is a mathematical model for a certain class of data structures that have
similar behavior. An abstract data type is defined indirectly, only by the operations that may be
performed on it

3. Define List.

List is an ordered collection of elements.

4. What is a linked list?

A linked list is a data structure consisting of a group of nodes which together represent a sequence. In a
linked list, each node is composed of a data and a pointer/link to the next node in the sequence.

5. What is an array? Compare it with linked list.

Array is a data structure which can store a fixed-size sequential collection of elements of the same type.

6. Comparison of Arrays and linked lists

Both Arrays and Linked List can be used to store data of similar types, but they both have some
advantages and disadvantages over each other.

Following are the points in favour of Linked Lists.

(1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and
in practical uses, upper limit is rarely reached.

(2) Inserting a new element in an array of elements is expensive, because room has to be created for the
new elements and to create room existing elements have to shifted.

For example, suppose we maintain a sorted list of IDs in an array id[].

id[] = [1000, 1010, 1050, 2000, 2040, .....].

1
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the
elements after 1000 (excluding 1000).

Deletion is also expensive with arrays until unless some special techniques are used. For example, to
delete 1010 in id[], everything after 1010 has to be moved.

So Linked list provides following two advantages over arrays


1) Dynamic size
2) Ease of insertion/deletion

Linked lists have following drawbacks:


1) Random access is not allowed. We have to access elements sequentially starting from the first node.
So we cannot do binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
3) Arrays have better cache locality that can make a pretty big difference in performance.

7. What are the various types of linked lists? Explain their concept.

A linked list is a self referential structure which contain a member field that point to the same structure
type. In simple term, a linked list is collections of nodes that consists of two fields, one containing the
information about that node, item and second contain the address of next node. Such a structure is
represented as follows:

struct node
{ int data;
struct node *next;
};

Linked lists are of following types:

Singly Linked List: In a singly linked list each node in the list stores the contents of the node
and a pointer or reference to the next node in the list. It does not store any pointer or reference to the
previous node. It is called a singly linked list because each node only has a single link to another node.
To store a single linked list, you only need to store a reference or pointer to the first node in that list.
The last node has a pointer to nothingness to indicate that it is the last node.

Doubly Linked List: A singly linked lists can only be traversed from first node to last node. A doubly linked
list allows the list to also be traversed from last node to first node. Each node in a doubly linked list
consists of two pointers. one pointing to the previous node and other pointing to the next node.

2
Circular Linked List: In a circular linked list, the next pointer of the last node points to the start.

8. Explain various operations that can be performed on a list.

Following set of operations can be performed on a linked list.

 Creating the list


 Inserting an element into the list
 Deleting an element from the list
 Displaying the elements of a list

Creating the list: Following are the steps to create a list. these 2 steps are to be repeated for n times for
creating a list of n elements.

1. create a node.
2. add it to the list.

creating a node in turn can be done by the following steps.

1. Allocate memory dynamically for the new node


2. Read the data from keyboard for new node
3. Set the next pointer of new node to NULL.

For adding the created node to the list do:

1. Check if there are any nodes in the list


2. If there are no nodes in the list then make new node start/ head node of the list.
3. Else, make the next pointer of last node in the list point to new node
4. Make new node, the last node in the list

Inserting an element into the list

1. Check whether the position specified is appropriate or not. If not, abort insert by displaying
error message.

3
2. Create a new node.
3. Assign element to be inserted as data part the new node.
4. If position the element is to be inserted in is first, make next pointer of new node point to
starting node of list and make new node the start/header node of the list.
5. Else, traverse the list starting from first node till the node after which new node is to be
inserted. call it prev. set next pointer of new node to point to the next node of prev. set next
pointer of prev point to the new node.

Deleting an element from a list

1. Traverse the list starting from start/header node till the node whose data part matches with
element to be deleted. call that node current.
2. Traverse the list starting from start/header node till the previous node of current. call it prev.
3. Set next pointer of prev point to the next node of current.
4. Delete current node.
5. if there is no node whose data part matches with element to be deleted, abort by displaying the
message “Element Not Found”.

Displaying the elements of a list

Traverse the list starting from start/header node till the last node of the list and print the data part of
each node while traversing.

9. Mention the difference between linear and non linear data structures.

Linear Data Structure:-A data structure in which every element has got exactly two neighbors or two
adjacent elements except two elements having exactly one data element. In a linear data structure, data
is arranged in linear order.

4
Non-Linear Data Structure:- In non linear data structure, elements are allowed to have more than two
adjacent elements. In a non linear data structure, data is arranged in non linear order.

10. What is the difference between static and dynamic memory allocation?

Dynamic memory allocation is done at runtime. static memory allocation is done before run time, but
the values of variables may be changed at run time.

Static memory allocation saves running time.


Dynamic memory allocation stores it’s memory on heap, and the static memory allocation stores it’s
data in the “data segment” of the memory.

11. Discuss in detail about polynomial ADT.

A polynomial ADT is a homogeneous ordered list of pairs <coefficient, exponentt>, where each exponent
is unique. Operations that can be performed on a polynomial ADT include

 Creation of polynomial
 Displaying the terms of polynomial
 Addition,
 Multiplication

Polynomial ADT can be implemented either by using a singly linked list or a doubly linked list, where
each node consists of two data components for representing coefficient and exponent as shown below.

Each node in the list corresponds to a term in the polynomial. In the node shown above 4 can be
considered as coefficient and 2 as exponent. So, the above node represents the term 4X^2.

Creation and display operations are same as creation and display operations of singly linked list.

Addition:

Let p and q be the two polynomials represented by 2 singly linked lists.

1. while p and q are not null, repeat step 2.

2. a. If powers of the two terms are equal then insert the sum of the terms into the sum Polynomial.
Advance p. Advance q.

b. Else if the power of the first polynomial > power of second, then insert the term from first polynomial
into sum polynomial. Advance p.

c. Else insert the term from second polynomial into sum polynomial. Advance q

5
3. Copy the remaining terms from the non empty polynomial into the sum polynomial.

Multiplication:

Let temp, p and q be the two polynomials represented by 3 singly linked lists. p and q are input
polynomials. temp is a polynomial holding intermediate product.

1. Repeat until q is not null

2. initialize temp to null

3.for each term in p, multiply the coefficients and add the exponents of p and current term of q. store
the result in temp.

4. Add the intermediate result in temp to product polynomial by calling polynomial addition.

5. advance q. goto step 1.

12. Explain in detail about the doubly linked list ADT.

A doubly linked list is a list that contains links to next and previous nodes. Unlike singly linked
lists where traversal is only one way, doubly linked lists allow traversals in both ways. A generic
doubly linked list node can be designed as:

struct node {
void* data;
struct node* next;
struct node* prev;
} *node;

Inserting to a Doubly Linked Lists


Suppose a new node, newnode needs to be inserted after the node current

CURR NEW
The following code can then be written
newnode next = currentnext; currentnext = newnode;
newnodeprev = current; (newnodenext) prev = newnode;

Deleting a Node from a Doubly Linked Lists

Suppose a new node, current needs to be deleted

CURR
The following code can then be written

6
node* N = currentprev
N next = currentnext;
(Nnext) prev = N;
free(current);

Doubly linked lists (DLL) are also widely used in many applications that deals with dynamic memory
allocation and deallocation. Although an additional pointer is used to allow traversal in both ways, DLL’s
are ideal for applications that requires frequent insertions and deletions from a list.

You might also like