You are on page 1of 7

LIST

Definition: It is a linear data structure in which elements can be inserted and deleted from
anywhere. The elements in a list are known as nodes.
Primitive Operations: The following is the list of fundamental operations that are valid in a list
Create ; Traverse/Display ; Count ; Search/Find ; Insert; Delete
Insertion or deletion of elements of list can be done in three ways, i.e., beginning of the list, after
some element in the list, and end of the list.
Abstract Data Type of list: In this you have to write definition and list out the primitive operations
with brief explanation of each operation.
Implementation of list: List can be implemented in two ways, namely, array and pointer
implementations. Array implementation of list is known as linear list. Pointer implementation of list
is called linked list. Linked list can be of two types, namely, singly linked list and doubly linked list.
Array Implementation of ordered list: In this implementation let us first define the structure of an
element /node. Assume that we are considering list of students as an example to illustrate the
concept of list where for each student two information kept, namely, regno and name. In array
implementation of list each node will have two parts, namely, info and index. The info part may
have any number of sub-divisions according to the example under consideration. In our example it
has got two sub-divisions one holding regno and another one holding name. The index field of each
node will have index of next node. We also maintain a variable by name head which is made to
point to the first node of the list always. This array implementation of list can be represented by
array of structure as given below.
struct student{
int regno;
char name[20];
int next;
}l[10];
int head, list, temp;
Assume that in this array of structure we are storing information of 5 students randomly.
Thus the list created is as shown below.
regno
name
next
head

0
1
2
3
4
5
6
7
8
9

200

X2

400

X4

100

X1

300
500

X3
X5

M.NAGARAJU, SCSE, VIT, VELLORE

Page 1

Creation: Get the details of first record and choose free space randomly in an array. Place first
record read at it. Store in head the index of first record of the list. Then read next record details,
place at some free space chosen randomly again in an array and place it there. In next field of first
record store index of second record. Read the next record and repeat the above process. The next
field of final record should have null value in it, indicating end of the list. Thus linear list gets created.
Traverse and Count: Consider a variable count with initial value as 0. Move through the list from e
the first element whose index is there in head to the end of it by incrementing count by one for each
element visit. Display each element when it gets visited. Once end of the list is reached, then the
value in count indicates number of elements in a list.
Search / Find: Traverse the list from the first node to the last node by comparing value of the node
to be searched with value of node visited. The moment such a node found then prompt a message
that node is found. Otherwise prompt a message that node is not found.
Insertion: Get the information of the new element to be inserted and store it in some free space
chosen randomly in array of structure. Three cases arises for insertion. They are (i) at the beginning
of the list, (ii) after some element of the list, and (iii)at the end of the list. Explanation and
examples are to be given for each case.
Note: Refer class notes for steps, explanation and examples of three cases of insertion.
Deletion: Get the information of element to be deleted from the list and search for that element in
the list. If encountered delete it by re-adjusting the index field value of pre/post elements of it
accordingly. Here too three cases arises. They are (i) from the beginning of the list, (ii) after some
element of the list, and (iii) from the end of the list. Explanation and examples are to be given for
each case.
Note: Refer class notes for steps, explanation and examples of three cases of deletion.

M.NAGARAJU, SCSE, VIT, VELLORE

Page 2

Pointer Implementation: This implementation of the list is known as linked list. Several linked are
possible, namely, singly linked list, doubly linked list and circular linked list. It is enough to know
about singly linked list.
SINGLY LINKED LIST
Each element in singly linked list is called as node. Its structure is defined using self referential
structure as given below.
struct student{
int regno;
char name[20];
struct sll *next;
};
This structure data type is renamed as node as given below.
typedef struct sll node;
Thus now node serves as data type. Then one can define variable of that data type as shown below.
node *head, *list, *temp;
Now we will allocate memory to the first node dynamically using malloc function and make head
point to it as shown below
list = (node *) malloc(sizeof(node));
head=list;
Read the content of that list node which is as shown below.
scanf(%d,&iistregno); // 300
gets(listname); //X3
The above operations result in the following diagram.
head

list
300

X3

Creation:
Step1)Make a move to next node of the list as below.
list=listnext;
Step2) Allocate space to next node as below.
listnext=(node *) malloc (sizeof (node));
Step3) Read next nodes information as below.
scanf(%d,&iistregno);
gets(listname);
Step4) Repeat step1 and step3 above until node with regno -999 is added. Its next field will have
NULL value indicating end of the list. The following diagram shows a sample of such list.

M.NAGARAJU, SCSE, VIT, VELLORE

Page 3

head

300

200

X3

X2

100

X1

500

X5

400

X4

list
-999

Traverse / Display:
Step1) Initialize list variable with content of head as given below.
list=head;
Step2) Display content of the node. Then move through the list node by node until listnext
becomes NULL. They are as below
listregno; // displays regno
listname; // displays name
list=listnext; //move to next node of the list
Count:
Step1) Have a variable count with initial value as 0.
Step2) Traverse the list as above and for each node visit increment the variable count by one.
Step3) Repeat step2 until last node of the list got visited. The value in the variable count when last
node got visited will specify the number of nodes in the list.

INSERTION OPERATIONS:
For three cases of insertion first let us do the following.
Create and allocate space to new node as shown below.
node * new_node;
new_node=(node *) malloc (sizeof(node));
Read values into new node as shown below
scanf(%d,&new_noderegno); // 600
gets(new_nodename); // X6
The above steps are represented by the following diagram
new_node
600

X6

M.NAGARAJU, SCSE, VIT, VELLORE

Page 4

(i) Insert new node at the beginning of the list:


Let the new node be inserted at the beginning of the list. Then do the following
temp=head;
head=new_node;
new_nodenext=temp;
Note: Show this insertion in diagrammatic manner. Please refer class notes.
(ii) Insert new node after some node of the list:
Let the new node be inserted after the node with regno 200 in the list. Then do the following
Initialize traversal by the following statement.
list=head;
Then traverse the list until node with regno 200 is encountered as below.
list=listnext // until listregno=200
After node with regno 200 is encountered then do the following.
temp=listnext;
listnext=new_node;
new_nodenext=temp;
Note: Show this insertion in diagrammatic manner. Please refer class notes.

(iii) Insert new node at the end of the list:


Let the new node be inserted at the end of the list. Then do the following
Initialize traversal by the following statement.
list=head;
Then traverse through the list until listnextregno is -999 node encountered by the
following statement.
list=listnext // until listnextregno!=-999
Once such a node encountered then does the following.
temp=listnext;
listnext=new_node;
new_nodenext=temp;
Note: Show this insertion in diagrammatic manner. Please refer class notes.

M.NAGARAJU, SCSE, VIT, VELLORE

Page 5

DELETION OPERATIONS:
(i) Delete node from the beginning of the list:
Get the position of the node to be deleted. Let it be the node at the beginning of the list.
Then do the following.
node *temp; // declare temporary node variable
list=head; // initialize list with head
temp=listnext; // temp will have the address of second node of the list
free(list); // removes first node of the list
head=temp; // Head now points to second node which is the new first node of the
list now
Note: Show the above diagrammatically. Please refer class notes.
(ii) Delete node after some node in the list:
Let us assume that we would like to remove a node after node with regno 200. Then do the
following.
node *temp; // declare temporary node variable
list=head; // initialize list with head
Traverse through the list until node with regno 200 is encountered by the following
list=listnext; // until listregno != 200.
Once such a node is encountered then does the following
temp=listnextnext; // temp will have the address of node after node which is to
be deleted.
free(listnext); // removes node after node with regno 200
listnext=temp; // now node with regno 200 points to the node after removed
node.
Note: Show the above diagrammatically. Please refer class notes.

(iii) Delete node at the end of the list:


Let us assume that we would like to remove which is at the end of the list. Then do the following.
node *temp; // declare temporary node variable
list=head; // initialize list with head
Traverse through the list until node with regno -999 is encountered by the following
list=listnext; // until listnextnextregno != -999.
Once such a node encountered then does the following.
temp=listnextnext; // temp will have the address of terminal node of the list
free(listnext); // removes last node of the list
listnext=temp; // establishes connection between node before deleted node and
the terminal node of the list.
Note: Show the above diagrammatically. Please refer class notes.

M.NAGARAJU, SCSE, VIT, VELLORE

Page 6

LINKED STACK
Singly linked list in which both insertion and deletion are done at the end of the list will act as Linked
Stack. Thus linked stack implementation should contain node structure definition and declaration,
dynamic allocation of memory to nodes, reading content of the node, creation of the list, insertion
at the end and deletion from the end parts of singly linked list.

LINKED QUEUE
Singly linked list in which insertion is done at the end and deletion is done from the beginning of of
the list will act as Linked Queue. Thus linked queue implementation should contain node structure
definition and declaration, dynamic allocation of memory to nodes, reading content of the node,
creation of the list, insertion at the end and deletion from the beginning parts of singly linked list.

M.NAGARAJU, SCSE, VIT, VELLORE

Page 7

You might also like