0% found this document useful (0 votes)
117 views6 pages

Linked List: Method To Insert (Add) A Node in The Beginning of A Linked List

A linked list is a data structure where each element contains a link to the next element. The document describes methods for common linked list operations like insertion, deletion, traversal, counting nodes, searching, and displaying data. Key methods include insertBeginning to add a node at the start, insertMiddle to add after a specific node, and insertEnd to add at the end. Other methods cover deleting a node, traversing the list, counting nodes, searching by name or value, computing a sum, and displaying all data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views6 pages

Linked List: Method To Insert (Add) A Node in The Beginning of A Linked List

A linked list is a data structure where each element contains a link to the next element. The document describes methods for common linked list operations like insertion, deletion, traversal, counting nodes, searching, and displaying data. Key methods include insertBeginning to add a node at the start, insertMiddle to add after a specific node, and insertEnd to add at the end. Other methods cover deleting a node, traversing the list, counting nodes, searching by name or value, computing a sum, and displaying all data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Linked List

If you use a list shown above represented by an object ‘ptr‘ of a class ‘Node‘, then to handle its contents
the following statements can be applied:

ptr.data // to access the data field of node ptr


ptr.link // to access the link field of node ptr

Single linked lists is a connection of various lists in such a way that each list has the address of the next
list. In this system,control can only proceed in the forward direction but not in backward direction. Lin k
of the last node is NULL which indicates end of the linked list structure.

Method to insert (add) a node in the beginning of a Linked List :

void insertBeginning(Node start, int x)


{
Node temp = new Node();
temp.data = x;
temp.link = start;
start = temp;
}

Method to insert (add) a node after ‘n’ nodes in a Linked List :

void insertMiddle(Node start, int x, int n)


{
Node temp = new Node();
temp.data = x;
Node ptr = new Node();
ptr = start;

int c = 1;
while(c <= n) //'ptr' will access all nodes up to the nth node
{
ptr = ptr.link;
c++;
}

temp.link = ptr.link;
ptr.link = temp;
}

Method to insert (add) a node at the end of a Linked List :


void insertEnd(Node start, int x)
{
Node temp = new Node();
temp.data = x;
Node ptr = new Node();
ptr = start;

while(ptr.link != null) //'ptr' will access all nodes up to the last node
{
ptr = ptr.link;
}

temp.link = null;
ptr.link = temp;
}

Method to delete (remove) a node after ‘n’ nodes in a Linked List :

void deleteNode(Node start, int n)


{
Node ptr = new Node(); //'ptr' is the node which will be deleted
Node ptr1 = new Node(); //'ptr1' is the node just before the node to be
deleted
ptr = start;
ptr1 = ptr;

int c = 0;
while(c <= n)
{
ptr1 = ptr;
ptr = ptr.link;
c++;
}

ptr1.link = ptr.link;
ptr.link = null;
}
Method to Traverse a Single Linked List :
If the class definition of a linked list is:

class Node
{
int data;
Node link;
}

void traversal(Node start)


{
Node ptr = new Node(); //creating a temporary object
ptr = start; //referring it to the first list (start node) of the linked list
while(ptr.link != null)
{

/* process ptr.data here */

ptr = ptr.link;
}
}

Method to count the number of nodes in the linked list:


class Node
{
int item;
Node next;
}

int count(Node ptr_start)


{
int c=0;
Node ptr = new Node();
ptr = ptr_start;
while(ptr.link != null)
{
c++;
ptr = ptr.next;
}
return c;
}
Method to search for a name and display the contents of that node.
class node
{
int p;
String n;
node next;
}

void search(node start, String b)


{
int f=0;
node ptr = new node();
ptr = start;
while(ptr.link != null)
{
if(b.equalsIgnoreCase(ptr.n))
{
f=1;
System.out.println("Content of the node = " + ptr.p);
}
ptr = ptr.next;
}
if(f==0)
System.out.println("Name not found");
}

Method to compute and return the sum of all integer items stored in the
linked list.
class ListNodes
{
int item;
ListNodes next;
}

int listsum(ListNodes start)


{
int sum=0;
ListNodes ptr = new ListNodes();
ptr = start;
while(ptr.next != null)
{
sum = sum + ptr.item;
ptr = ptr.next;
}
return sum;
}
Method to search for a value in a linked list.
class Node
{
int data;
Node link;
}

void search(Node start, int value)


{
int f=0;
Node ptr = new Node();
ptr = start;
while(ptr.link != null)
{
if(ptr.data == value)
{
f=1;
}
ptr = ptr.link;
}
if(f == 1)
System.out.println("Search is Successful");
else
System.out.println("Search is Unsuccessful");
}

Method to display all the data in a linked list.


class Node
{
int data;
Node link;
}

void display(Node start)


{
Node ptr = new Node();
ptr = start;
System.out.println("Data in the linked list are: ");
while(ptr.link != null)
{
System.out.println(ptr.data);
ptr = ptr.link;
}
}

You might also like