Operations in Linked List

A singly linked list contains of a number of nodes in which each node has a next pointer to the following element. Insertion, deletion and traversal are the basic operations in a linked list. Insertion can be at the beginning, the middle or at the end. Same is the case for deletion.

Summary

A singly linked list contains of a number of nodes in which each node has a next pointer to the following element. Insertion, deletion and traversal are the basic operations in a linked list. Insertion can be at the beginning, the middle or at the end. Same is the case for deletion.

Things to Remember

  1. A singly linked list contains of a number of nodes in which each node has a next pointer to the following element.
  2.  Insertion, deletion and traversal are the basic operations in a linked list.
  3. Insertion can be at the beginning, the middle or at the end. 
  4. Same is the case for deletion. 

MCQs

No MCQs found.

Subjective Questions

Q1:

What do you mean by Urbanization ?


Type: Short Difficulty: Easy

Show/Hide Answer
Answer: <p>Urbanization can be defined as the growth in the proportion of total population in the urban places . In simple word, it is the process of development of urban areas and concentration of population in thoseurban areas.</p>

Q2:

What do you mean by Metropolitan (Mahanagarpalika) ? 


Type: Short Difficulty: Easy

Show/Hide Answer
Answer: <p>Mahanagarpalika is a municipality with a minimum population size of 3,00,000 annual revenue of a least Rs.400 million , facilities of electricity , drinking water , communication paved main and subsidiary roads , provision of specialized health services etc.</p>

Q3:

What are the classification of Urban area? 


Type: Long Difficulty: Easy

Show/Hide Answer
Answer: <p>Urban areas are classified into the different area on the basis of population size, space, and social and economic structure. According to the Municipality, Act 1992 and the local self- governance Act 1999 have categorized the existing municipalities as mahanagarpalika, upa- mahanagarpalika and nagarpalika. Following are the classification of Urban area :</p> <p></p> <ul><li><strong>Municipality (Nagarpalika):</strong> To be in the list of Nagarpalika, the VDCs or area should have minimum population size 20, 00 in the Terai, 10,000 in the hill or mountain region. The annual revenue of the area should be about Rs. 5 million in the terai and 500,000in the hill or the mountain region. The VDCs should have the facilities like electricity, road, drinking water, communication and other similar urban facilities. At present in our country, there are 205 Municipalities.</li> <li><strong>Sub- metropolitan (Upa- Mahanagarpalika):</strong> To be in the list of Mahanagarpalika, the area should have the minimum population size of 1,00,000. The annual revenue should be atleast Rs.100 million and it should already receive the status of a nagarpalika. Similarly taking about facilities, it should have facilities of electricity, drinking water, communication, paved roads, education and health services of a high standard. The area should have the general infrastructure for national and international sports events and provision of public parks and city hall.</li> <li><strong>Metropolitan (Mahanagarpalika):</strong> To be in the list of Mahanagarpalika, the area should have minimum population size of 3,00,000. The annual revenue should be atleast Rs.400 million. It should have the facilities like proper facilities of electricity, drinking water, communication, paved main and subsidiary roads. There should be good provision of health services, essential infrastructure for international sports events. The area should have atleast one university with adequate opportunities for higher education in different fields. To be classified as Mahanagarpalika, the area should have already received the status of a Upa- mahanagarpalika</li> </ul>

Q4: What is urbanization?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: Urbanization is the process of converting rural areas into urban areas with the provision of various kinds of urban centric services and facilities.

Q5: What are the three important factors of urbanization?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The three important factors of urbanization are urban behaviour, structure and demography.

Q6: What is the literal meaning og word 'Urbal'?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The literal meaning of word 'Urbal' is civilized society.

Q7: How many municipalities are there in Nepal right now?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: There are 217 municipalities in Nepal right now.

Q8: What are the three categories of municipalities?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The three categories of municipalites are Nagarpalika, Upa- Mahanagarpalika and Mahanagarpalika.

Q9: What should be the minimum population size in the Terai area to be in the list of municipality?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The minimum population size in the Terai area should be about 20, 000 to be in the list of Municipality.

Q10: What should be the minimum population size to be in the list of Sub- metropolitan?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The minimum population size should be 1,00,000 to be in the list of Sub- metropolitan.

Q11: Write any two facilities that an area should have to be in the list of Metropolitan?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: Any two facilities that an area should have to be in the list of Metropolitan are essential infrastructure for international sports events and atleast one established university.

Q12: What should be the annual revenue of a particular area to be in the list of Metropolitan?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The annual revenue of a particular area should be about Rs.400 million to be in the list metropolitan.

Q13: What status should be received by the particular area to be in the list of Upa- Mahanagarpalika?
Type: Very_short Difficulty: Easy

Show/Hide Answer
Answer: The particular area should have received the status of a nagarpalika to be in the list of Upa- Mahanagarpalika.

Videos

The Effects of Urbanization and Infrastructure Development on the Environment
TOTAL MUNICIPALITIES IN NEPAL REACH 99
Operations in Linked List

Operations in Linked List

Generally, "linked list" are referred to a singly linked list. This list contains of a number of nodes in which each node has a next pointer to the following element. The link of the last node in the list is NULL and indicates the end of the list.

Fig: A singly linked list
Fig: A singly linked list

Basic Operations on a linked list

  • Traversing the list
  • Inserting an item in the list
  • Deleting an item from the list

Traversing the list

Let us assume the head points to the first node of the list. To traverse the list, we do the following:

  • Follow the pointers
  • Display the contents of the nodes (or count) as they are traversed
  • Stop when the next pointer points to NULL.
insertll

The listLength() function takes a linked list as input and counts the number of nodes in the list. It can be used for printing the list data with extra print function:

int listLength(struct Node* head){

struct Node* current = head;

int count = 0;

while(current != NULL){

printf(current -> value);

count ++;

current = current -> next;

}

return count;

}

Singly linked list Insertion

It has three cases:

  • Inserting a new node before the head (at the beginning)
  • Inserting a new node after the tail (at the end)
  • Inserting a new node in the middle of the list (random location)

Insertion at the beginning

In this case, a new node is inserted before the current head nodne. Only one next pointer needs to be modified (new node's next pointer) and it can be done in two steps:

  • Update the next pointer of the new node, to point to the current head.
1a

  • Update the head pointer to point to the new node
1b

Insertion at the End

In this case, we need to modify two next pointers (last node's next pointer and new node's next pointer).

  • New node's next pointer points to NULL
2a

  • Last node's next pointer points to the new node
2b

Insertion in the Middle

Let us assume that we are given a position where we want to insert the new node. In this case also, we need to modify two next pointers.

  • If we want to add an element at position 6 then we stop at position 5. That means we traverse 5 nodes and insert the new node. For simplicity, let us assume the fifth is called position node New node points to the next node of the position where we want to add this node.
3a

  • Position node's next pointer now points to the new node.
3b

Let us now write the C code for the insertion for all three cases:

void insertInLinkedList(struct Node **head, int data, int position){

int k = 1;

struct Node *p, *q, *newNode;

newNode = (Node*)malloc(sizeof(struct Node));

if(!newNode){ //Check for memory errors

printf("Memory Error");

return;

}

newNode -> value = data;

p = *head;

if(position == 1) { // Insertion at the beginning

newNode -> next = p;

*head = newNode;

}

else { //Traverse the list until position-1

while ((p != NULL && ( k

k ++;

q = p;

p = p -> next;

}

if ( p == NULL) { // Insertion at the end

q -> next = newNode;

newNode -> next = NULL;

}

else { //Insertion in the middle

q -> next = newNode;

newNode -> next = p;

}

}

}

Singly Linked List Deletion

Similar to insertion, deletion also has three cases:

  • Deleting the first node
  • Deleting the last node
  • Deleting an intermediate node

Deleting the First Node

First node (current node head) is removed from the list in two steps:

  • Create a temporary node which will point to the same node as that of the head.
1a

  • Now, move the head node's pointer to the next node and dispose the temporary node.
1b

Deleting the last node

In this case, the last node is removed from the list. This operation is a bit trickier than removing the first node, because algorithm should find anode, which is previous to the tail first. It can be done in three steps:

  • Traverse the list and whle traversing maintain the previous node address also. By the time we reach the end of list, we will have two pointers, one pointing to the tail node and the other pointing to the node before the tail node.
2a

  • Update previous node's next pointer with NULL
2b

  • Dispose the tail node
2c

Deleting an Intermediate Node

In this case, node to be removed is always located between two nodes. Head and tail links are not updated in this case. Such removal can be done in two steps:

  • As similar to the previous case, maintain previous node while traversing the list. Once we find the node to be deleted, change the previous node's next pointer to next pointer of the node to be deleted.
3a

  • Dispose the current node to be deleted
3b

Code:

void deleteNodeFromLinkedList(struct Node **head, int position) {

int k = 1;

struct Node *p, *q;

if (*head == NULL) {

printf("List Empty");

return;

}

p = *head;

if (position == 1) { //From the beginning

p = *head;

*head = *head -> next;

free (p);

return;

}

else { //Traverse the list until the position from which we want to delete

while ((p!=NULL) && (k<position-1)) {

k++;

q = p;

p = p -> next;

}

if (p == NULL) { //At the end

printf("Position doesn't exist");

}

else { //From the middle

q -> next = p -> next;

free(p);

}

}

}

References

1. Karumanchi, N. "Data Structures and Algorithms Made Easy"

2. Y. Langsam, M. J. Augenstein and A. M Tenenbaum, “Data Structures using C and C++”

3. R. L. Kruse, B. P. Leung, C. L. Tondo, “Data Structure and Program design in C”

Lesson

Linked lists

Subject

Computer Engineering

Grade

Engineering

Recent Notes

No recent notes.

Related Notes

No related notes.