Linked List 2011

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

  1. Move temporary pointer to the next node
  2. 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

  1. move temporary pointer to the next node
  2. Repeat step 5 & 6 until the temporay pointer reaches null
  3. 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;

}

}

The following function witty() is a part of some class. What will be the output of the function witty() when the value of n is SCIENCE and the value of p is 5. Show the dry run/working. [5]

void witty ( String n, int p)

{

if (p<0)

System.out.println(”  “);

else

{

System.out.printin( charAt(p) + ”  .  “);

witty(n,p-1);

System.out.print(n.charAt(p));

}

}

void witty ( String n, int p)

{

if (p<0)

System.out.println(”  “);

else

{

System.out.println( n.charAt(p) + “.”);

witty(n,p-1);

System.out.print(n.charAt(p));

}

}

This entry was posted in Linked list. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *