answersLogoWhite

0


Best Answer

when you have two nodes and when you want to insert the third node between the 1 & 2 then, store the address of the third in the next pointer of 1st and address of 2nd in the next pointer of 3rd.

by kochu

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

It depends whether the list is singly-linked or doubly-linked. Let's examine the case for singly-linked lists, which are much easier to implement. Some sample code is included at the end of this article.

Before we can make an insertion, we must first locate the insertion point. We do this by traversing the list from the head node, comparing the current node's data with the data we want to add. If it is less than or equal to the current node's data, then we've located the insert point. Otherwise we move to the next node and compare the data again, and repeat until the insert point is found. If there is no next node, then we simply add the new node to the end of the list.

So far so simple. But there is a problem. In order to insert a new node before an existing node (the insert point), we must update the node that currently comes before the existing node; the new node will become that node's next node. However we can only traverse a singly-linked list in one direction, we cannot go back to a previous node without traversing the list again.

There are several solutions to the problem. We could allow the list to handle the traversal, maintaining pointers to previous nodes as required. However, while it is natural to assume that because the list manages the nodes it contains it should also be responsible for the insertion of new nodes within the list, this is not the case at all. The only node the list actually knows anything about is the head node, and that is its sole responsibility; to maintain the head node pointer, creating it if necessary, but nothing more and nothing less.

The nodes themselves are solely responsible for their own data and for maintaining their next pointers, but because of this they are in a far better position to determine where new data should be inserted, and how to insert it. It either comes before them or it comes after them. So we should let the nodes pass new data amongst them and let them sort out where it should actually be inserted, adjusting their own pointers as required.

Once the head node has been created, further insertions can be passed directly to the head node itself, via its own Insert() method. The return value then becomes the new head node for the list (which will either be the same node or a new node altogether). In effect, the current head node tells the list which node is now the head node!

Whenever a node's Insert() method is called, the data that is passed to it is compared with the node's own data to decide if the data should come before or after that node.

If the given data is greater, control of the data passes to the next node and the return value becomes the next node. In other words, the next node informs its previous node what its next node should really be!

However, if the next node is currently NULL, then this node must be the last node. In that case, a new node is created which then becomes this node's next node. The new node is now the last node so its next pointer is set to NULL.

In either of these cases, the method will return a pointer to itself. If the call came from the list, then this node is still the head node. And if the call came from the previous node, then this node remains its next node.

However, if the given data is less than or equal to the node's own data, then it must come before this node. In that case, a new node is created, it's next node is set to this node and the new node is returned. If the call came from the list, then the new node becomes the new head node, otherwise the new node becomes the previous node's next node.

In effect, the node pointers take care of themselves and the nodes decide amongst themselves where data is to be inserted, the only exception being the initial head node which is taken care of by the list itself. Everything as it should be.

// Purpose: Singly-linked list node insertion demo.

// DISCLAIMER: Use at your own risk!

#include <iostream>

// A (very) simple data class to put into the list.

class Data

{

public:

Data(int i):m_iValue(i){}

void Report()const{ std::cout << m_iValue << std::endl; }

bool operator> (const Data & theData){ return( m_iValue > theData.m_iValue ); }

private:

int m_iValue;

};

// The node class.

class Node

{

public:

Node(Data & theData, Node * nextNode):

m_pData(&theData),

m_pNext(nextNode){}

~Node(){if(m_pNext)delete(m_pNext); delete(m_pData);}

Node * Insert(Data & theData);

void Report()const{m_pData->Report(); if(m_pNext) m_pNext->Report();}

private:

Data * m_pData;

Node * m_pNext;

};

Node * Node::Insert(Data & theData)

{

if(theData > *m_pData)

{

if( m_pNext )

m_pNext = m_pNext->Insert(theData);

else

m_pNext = new Node(theData, NULL);

return( this );

}

return( new Node(theData, this));

}

class List

{

public:

List():m_pHead( NULL ){}

~List(){delete(m_pHead);}

void Insert(Data & theData);

void Report(){if(m_pHead) m_pHead->Report();}

private:

Node * m_pHead;

};

void List::Insert(Data & theData)

{

if( m_pHead )

m_pHead = m_pHead->Insert(theData);

else

m_pHead = new Node(theData, NULL);

};

int main(int argc, char* argv[])

{

int iData;

Data * pData;

List LinkedList;

for(;;)

{

std::cout << "Enter a value (0 to quit): ";

std::cin >> iData;

if(!iData)

break;

pData=new Data(iData);

LinkedList.Insert(*pData);

}

LinkedList.Report();

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

void insertNode(struct linked_list *list, const int i, const int data) {

if( list 0 ) {

// create new node

struct linked_list_node *newNode = createLinkedListNode(data);

newNode->next = list->root;

list->root = newNode;

++(list->size);

return;

}

struct linked_list_node *current = list->root;

int currentIndex;

// move to list[i]

for(currentIndex = 0; currentIndex < i; ++currentIndex) {

current = current->next;

}

// create new node

struct linked_list_node *newNode = createLinkedListNode(data);

newNode->next = current->next;

current->next = newNode;

++(list->size);

}

Read more: http://wiki.answers.com/How_to_create_a_single_link_list_in_c#ixzz19Pm5odyF

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

As each implementation is different (assuming you meant a circular singly-linked list) the general algorithm is:

  1. Create a new node, making sure it is not allocated locally in the function and thus will not be destroyed when the function execution finishes
  2. Fill in data
  3. Use the "last node" pointer in the list and copy the "next" pointer location (pointing to the first node) into the new nodes "next" pointer
  4. Set the "last node" "next" pointer to point to the new node
  5. Change the list's "last node" pointer to point to the new node

This should illustrate the algorithm:

struct list_node {

struct data data;

struct list_node *next;

}

struct list {

struct list_node *last;

}

void push(struct list *list, struct data *data){

struct list_node *new_node = (struct list_node*)malloc(sizeof(struct list_node));

new_node->data = *data;

new_node->next = list->last_node->next;

list->last_node->next = new_node;

list->last_node = new_node;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Queues are distinct in that extractions occur at the head while insertions occur at the tail. A standard singly-linked list only maintains a pointer to the head node which means you must traverse the entire list to get to the tail for an insertion. However, with a minor modification, you can add another pointer that points to the tail node, without going to the expense of a doubly-linked list. This then gives constant time access to both the head and the tail, which is essential in a queue.

To insert a new element, you create a node for it, then set the current tail's next node to the new node, then set the tail to the new node. If there is currently no tail, then the list is empty so the new node simply becomes the tail (as well as the head).

To extract an element, make sure there is a non-zero head first. If so, use a temporary pointer to refer to it, then extract its element. Set the head to the temp pointer's next node then destroy the temp pointer. Finally, return the extracted element.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

To insert a node into a doubly linked list, create the following function. The function accepts 4 arguments, a pointer to the list, a pointer to the (new) node, and the nodes that will come before and after it once inserted. If the previous node is NULL, the new node becomes the head of the list. If the next node is NULL, the new node becomes the tail of the list.

Node* insert (List* list, Node* node, Node* prev, Node* next) {

if (!list !node) return 0;

node->next = next;

node->prev = prev;

if (!prev)

list->head = node;

else

prev->next = node;

if (!next)

list->tail = node;

else

next->prev = node;

return node;

}

With this function in place, you can simplify calls to it by creating the following helper functions:

Node* insert_before (List* list, Node* node, Node* next) {

if (!next)

return insert (list, node, list->tail, 0);

else

return insert (list, mode, next->prev, next);

}

Node* insert_after (List* list, Node* node, Node* prev) {

if (!prev)

return insert (list, node, 0, list->head);

else

return insert (list, mode, prev, prev->next);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A singly linked list acts as a dynamically implemented stack in that insertion and deletion take place only from the top.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

first :node b is to be inserted between node a and node c, after nodeb->next=nodea->next;

nodeb>prev=nodea;

nodea->next=nodec;

nodec->prev=nodeb

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can a node be inserted in the middle of a linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


Does each node in a doubly linked list contain a link to the previous as well as the next node?

Yes, each node in a doubly linked list contain a link to the previous as well as the next node. That is the definition of the doubly linked list.


What operation is supported in constant time by the doubly linked list but not by the singly linked list?

examples:- delete this node (identified by a pointer)- insert a new node before this node- replace this node with another node


Explain any two advantages using Single linked list over Doubly linked list and vice-versa?

Advantages of single linked list: # Decrease in storage space per linked list node # Simpler implementation Advantages of double linked list # Decrease in work when accessing a random node # Decrease in work when inserting or deleting a node


How is anode inserted in a linked list?

suppose that a linked list which have 4 nodes. Example:- 10-&gt;20-&gt;30-&gt;25-&gt;50-&gt;NULL there is three way to insert the node in this list. 1. from beginning position 2. from ending position 3. from specific position

Related questions

Can you insert node in the middle of linked list?

Yes, it is easy.


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


Can we use doubly linked list as a circular linked list?

Yes. The tail node's next node is the head node, while the head node's previous node is the tail node.


How do you insert a new node into circular linked list and where the new node will be inserted?

If the node is to be inserted in sorted order, you must traverse the list from the head node or first node to locate the insertion point. You then point the node to its next and previous nodes, either side of the insertion point. Then update those nodes to point at the new node.That is, assuming node is to be inserted between prev and next, you would use the following algorithm (assuming every node has next and prev members):node.next = next;node.prev = prev;next.prev = node;prev.next = node;


Does each node in a doubly linked list contain a link to the previous as well as the next node?

Yes, each node in a doubly linked list contain a link to the previous as well as the next node. That is the definition of the doubly linked list.


What operation is supported in constant time by the doubly linked list but not by the singly linked list?

examples:- delete this node (identified by a pointer)- insert a new node before this node- replace this node with another node


What are the differences between singly-linked doubly-linked and circularly-linked lists?

The difference is how many pointers each node has, and what they are pointing to. A linked list is comprised of "Nodes" each node contains data as well as 1 or more pointers. A singly linked list has one pointer per node, and a doubly linked list has 2 pointers per node. Some programs use several pointers per node. The purpose of these pointers is to hold the list together. In a singly linked list, you can view a node and can then move on to the next node that it is pointing to until you've passed through them all. A doubly-linked list would have a pointer to the next node as well as to the previous node. Thus you can move forward and backward through the list. A circularly-linked list doesn't necessarily have a set number of pointers because it simply means that the last node points to the first node creating a big circle. A non-circularly-linked list would not contain this last to first pointer and thus you would eventually reach the end of the list and stop.


What is singly linked list and doubly linked list?

Singly Linked list Each item in the list is called a node and contains two fields &#61656; Information field - The information field holds the actual elements in the list &#61656; Next address field- The next address field contains the address of the next node in the list. The entire linked list is accessed from an external pointer called the List. Doubly linked list is a collection of node. Each node contains three fields an info field that contains the information stored in the node. The left and right field that contains the address of the node on its left and right. The doubly linked list could be linear, circular and may have a header node.


Explain any two advantages using Single linked list over Doubly linked list and vice-versa?

Advantages of single linked list: # Decrease in storage space per linked list node # Simpler implementation Advantages of double linked list # Decrease in work when accessing a random node # Decrease in work when inserting or deleting a node


what are the differences between singly link list and doubly link list?

singly linked list stores only the address of next node while doubly linked list stores the address of previous node and next node and hence it is called doubly linked list. In singly linked list only forward traversing is possible while in doubly linked list forward and backward traversal is possible.


How is anode inserted in a linked list?

suppose that a linked list which have 4 nodes. Example:- 10-&gt;20-&gt;30-&gt;25-&gt;50-&gt;NULL there is three way to insert the node in this list. 1. from beginning position 2. from ending position 3. from specific position


How to Print data of all nodes of linked list?

for (node=head; node!=null; node=node-&gt;next) printnode(node);