0% found this document useful (0 votes)
38 views10 pages

Linked List ISC

The document outlines various algorithms and methods for manipulating linked lists in Java, including deleting a node, searching for a name, counting nodes, adding a node, and summing odd or even integers. Each section provides a clear algorithm followed by a method implementation for the specified operation. The classes used for the linked list nodes are defined with relevant attributes for each operation.

Uploaded by

me.acasa45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views10 pages

Linked List ISC

The document outlines various algorithms and methods for manipulating linked lists in Java, including deleting a node, searching for a name, counting nodes, adding a node, and summing odd or even integers. Each section provides a clear algorithm followed by a method implementation for the specified operation. The classes used for the linked list nodes are defined with relevant attributes for each operation.

Uploaded by

me.acasa45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

A linked list is formed from the objects of the class

class Node

int info;

Node link;

Write an algorithm OR a Method for deleting a node from a lined list. The
method declaration is given below:

void deleteNode (Node start)

Algorithm to delete a node in a linked list

Steps

1. Start
2. Accept info for the node to be deleted
3. if first nod is the desired one then
set temporary pointer to first node

Move the pointer of the first node to the next node

Set temporary pointer to null

Stop

else

4. Move temporary pointer to the next node


5. if the desired node is fond then
Set the pointer of the previous node to the next of the node to be deleted

Set the temporary pointer node to null

Stop

else
6. move temporary pointer to the next node
7. Repeat step 5 & 6 until the temporay pointer reaches null
8. End
Method to delete a node in a linked list

void deleteNode ( Node start)

Syste,m.out.println(“Enter info for the node to be deleted”);

int n= integer.parseInt(obj.radLine());

Node a = new Node(start)

if(start.info==n)

a.start;

start= start.link;

a.link=null;

else

a=a.link;

Node b = new Node(start);

while( a!=null)

if(a.info==n)

b.link=a.link;

a.link=null;
break;

b=a;

a.a.link;

2. A linked list is formed from the objects of the class

class node

int p;

String n;

node next;

3. Write an algorithm/method to search for a name and display the


contents of that node. The method declaration is given below.

void search(node start, String h)

Algorithm to search fo a node in the linked list

Steps

1. start
2. Set temporary pointer to the first node
3. Repeat steps 4 and 5 until the pointer reaches null
4. if the names matches with the parameter value then Print details, stop
5. else
6. Move temporary pointer to the next node
7. End
Method to search for a node in a linked list
void search( node start, String b)

node a = new node(start)

int f =-1;

while(a!=null && f=-1)

if(a.n equals(b))

f=1;

else

a=.a.next;

if (f==1)

System.out.println(a.p);

else

System.out.println(“Node not found”)’

Write an Algorithm OR a Method to count the number of nodes in the


linked list The ,ethod declaration is given below:

int count (Node prt-start)

Algorithm to count the number of nodes in a linked list

Steps

1. Start
2. Set a temporary pointer to the first node and counter to 0
3. Repeat steps 4 and 5 until the pointer reaches null
4. Increment the counter
5. Move the temporary pointer to the next node
6. Return the counter value
7. End
Method to count for the number of nodes in linked list

int count (node ptr_start)

Node a = new Node(ptr_start);

int c=0;

while (a!=null)

c++;

a= a.next;

return c;

4. A linked list is formed from the objects of the class

class Node

int number;

Node nextNode;

Write an Algoritm OR a Method to add a node at the end of an existing


liked list The method declaration is as follows.

void add node(Node start, int num)


Algorithm to add a node at the end of an existing linked list.

Steps.

1. Start
2. Set temporary pointer to start node
3. Repeat steps 4 until it reaches null
4. move the pointer to the next node
5. create a new node, assign num to number and link to the temporary
node
6. End
Method to add a node at the end of an existing linked list

void add node (node start, int num)

Node A = new Node(start);

while(A!= null)

A= A.nextNode;

Node C = new node();

C.number = num;

C.nextNode = null;

A.next =C;

5. A linked list is formed from the objects of the class:


class Nodes
{
int num;
Nodes next;
}
Write an Algorithm OR a Method to print the sum of nodes that
contains only odd integers of an existing linked list.
The method declaration is as follows:
void NodesCount( Nodes starPtr )

Algorithm to print the sum of the nodes of odd integers in an existing


linked list.
Steps :
1 – Start
2 – Set temporary pointer to start node
3 – Repeat steps 4 & 5 until the pointer reaches null. Display the Count.
Exit.
4 – Check for odd integers and accumulate
5 – Move pointer to the next node
6 – End

Method to print the sum of the nodes of odd integers in an existing linked
list.
void NodeCount( Nodes starPtr)
{
Nodes temp = new Nodes(starPtr)
int c=0;
while (temp!=null)
{
if( temp.num%2 !=0)
c=c+temp.num;
temp=temp.next;
}
System.out.println(c);
}
6. A linked list is formed from the objects of the class Node. The class
structure of the Node is given below:
class Node
{
String name;
Node next;
}
Write an Algorithm OR a Method to search for a given name in the
linked list. The method of declaration is given below:
boolean searchName(Node start, String v)

ALGORITHM:
Step 1. Start
Step 2. Set temporary pointer to the first node
Step 3. Repeat steps 4 and 5 until the pointer reaches null
Step 4. Search for the node. If found display details, exit
Step 5. Move pointer to the next node
Step 6. End
METHOD:
boolean searchName(Node start, String v)
{
Node temp=new Node(start);
while(temp.name.equals(v) = = false && temp.next!=null)
temp=temp.next;
if(temp.next!=null)
return true;
else
return false;
}
7. A linked list is formed from the objects of the class Node. The class
structure of the Node is given below:
class Node
{
int num;
Node next;
}
Write an Algorithm OR a Method to count the nodes that contain
only odd integers from an existing linked list and returns the count.
The method declaration is as follows:
int CountOdd( Node startPtr )

ALGORITHM:
Step 1. Start
Step 2. Set temporary pointer to the first node
Step 3. Repeat steps 4 and 5 until the pointer reaches null. Return count
Step 4. Check for odd and increment the counter.
Step 5. Move pointer to the next node
Step 6. End

METHOD:
int CountOdd(Node startPtr)
{
int c=0;
Node temp=new Node(startPtr);
while(temp != null)
{
if (temp.num % 2 != 0)
c++;
temp=temp.next;
}
return c;
}
8. A linked list is formed from the objects of the class Node. The class
structure of the Node is given below:
class Node
{
int n;
Node link;
}
Write an Algorithm OR a Method to search for a number from an
existing linked list.
The method declaration is as follows:
void FindNode( Node str, int b )

ALGORITHM:
Step 1. Start
Step 2. Set temporary pointer to the first node
Step 3. Repeat steps 4 and 5 until the pointer reaches null. Display
number not found
Step 4. check for number , if found display, exit
Step 5. Move pointer to the next node
Step 6. End algorithm

METHOD:
void FindNode(Node str, int b)
{
Node temp=str;
while(temp!=null)
{
if (temp.n == b)
{
System.out.prinln(b+” is found “);
break;
}
temp=temp.link;
}
}
9. A linked list is formed from the objects of the class Node. The class
structure of the Node is given below:
class Node
{
int num;
Node next;
}
Write an Algorithm OR a Method to find and display the sum of
even integers from an existing linked list.
The method declaration is as follows:
void SumEvenNode( Node str )
ALGORITHM:
Step 1. Start
Step 2. Set temporary pointer to the first node
Step 3. Repeat steps 4 and 5 until the pointer reaches null. Display
sum, exit
Step 4. check for even number, if found accumulate
Step 5. Move pointer to the next node
Step 6. End algorithm

METHOD:
void SumEvenNode(Node str)
{
Node temp=new Node(str);
int s=0;
while(temp!=null )
{
if (temp.num%2==0)
s=s+temp.num;
temp=temp.next;
}
System.out.println(“sum of even integers=”+s);
}
10.

You might also like