In: Computer Science
follow pseudo
1)
Create class called Node
Declare private integer called data (or any other name)
Declare private Node called link (or any other name)
2) Declare constructor
Should be public (ex: Node)
where:
link is equal to null
data is equal to zero
3) Declare another constructor
public Node with parameters integer d, Node n
where:
data is equal to d
link is equal to n
4)
Declare function to set link to next Node
link equal to n
5)
Declare function to set data to current Node
public void setData with parameter integer d
where
data equal to d
6)
Declare function to get data from current Node
public integer getData
return data
7)
Create class called linkedlist (or any other name)
declare private Node start
declare private Node end
declare public integer size
inside of that class declare constructor;
start equal to null
end equal to null
size equal to zero
8)
Declare function to check if list is empty
should be public boolean any name (empty or something else)
return start equal to null
9)
Declare function to get size of list
should be public integer getSize
return size
10)
Declare function to insert an element at
beginning
declare public void insert at start (some name indicating to insert
in the beginning) with parameter integer value
Declare:
Node (object name ex: as) equal to new Node (value, null)
increment size
if condition start equal to null
start equal to as
end equal to start
else condition
as.setLink (start)
start equal to as
11)
Declare function to insert an element at end
Should be public void insert at end (or something) with parameter
integer value
Declare:
Node (object name ex: as) equal to new Node (value, null)
increment size
if condition start equal to null
start equal to as
end equal to start
else condition
as.setLink (as)
end equal to as
12)
Declare function to insert an element at
position
Should be public void insert in position (or something similar)
with paramaters integer value and integer position
Declare:
Node (object name ex: as) equal to new Node (value, null)
Node (called: es) equal to start
Position equal to position minus 1
create for loop where i equal 1 and i less then size and increment
i
inside of that loop create condition
if i equal position
Node temp equal to es.getLink();
es.setLink(as)
as.setLink(temp)
break
//after closed bracket continue
es equal to es.getLink();
//after closed bracket
increment size
13)
Declare function to delete an element at
position
Should be public void delete in position (or something similar)
with parameter integer position
condition if position equal to 1
start = start.getLink();
decrement size
return
condition if position equal to size
Node a (or any other name) equal to start
Node b (or any other name) equal to start
loop while a not equal to end
b equal to a
a equal to a.getLink();
//after closed bracket continue
end equal to b
end.setLink(null)
decrement size
return
//after closed bracket continue
Node aa(or any other name) equal to start
position equal to position minus 1
create for loop where i equal 1 and i less size minus 1 and
increment i
condition if i equal to position
Node temp(or any name you want) equal to aa.getLink();
temp equal to temp.getLink();
aa.setLink(temp);
break
//after closed bracket continue
aa equal aa.getLink();
//after another closed bracket
decrement size
14)
Declare function to display
Should be public void display
sys.print Linked list:
condition if size equal to zero
sys.print empty value
return
condition if start.getLink() equal to null
sys print(start.getData());
return
Node aaa equal to start
sys.print(start.getData() + " - ");
aaa equal to aaa.getLink();
//after closed bracket
sys.print (aaa.getData() + " \n");
15)
Scanner scan = new Scanner(System.in);
linkedList list = new linkedList();
System.out.println("Linked List: ");
char ch;
do
{
System.out.println("\nLinked List Menu\n");
System.out.println("1. Insert at start");
System.out.println("2. Insert at end");
System.out.println("3. Insert in Position");
System.out.println("4. Delete in Position");
System.out.println("5. Check if its empty")
;
System.out.println("6. Get Size of Linked
List");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
...............
........................
.................................
..........................................
#Completing pseudo code
1)
Create class called Node
Declare private integer called data (or any other name)
Declare private Node called link (or any other name)
2)
Declare constructor
Should be public (ex: Node)
where:
link is equal to null
data is equal to zero
3)
Declare another constructor
public Node with parameters integer d, Node n
where:
data is equal to d
link is equal to n
4)
Declare function to set link to next Node
link equal to n
5)
Declare function to set data to current Node
public void setData with parameter integer d
where
data equal to d
6)
Declare function to get data from current Node
public integer getData
return data
7)
Create class called linkedlist (or any other name)
declare private Node start
declare private Node end
declare public integer size
inside of that class declare constructor;
start equal to null
end equal to null
size equal to zero
8)
Declare function to check if list is empty
should be public boolean any name (empty or something else)
return start equal to null
9)
Declare function to get size of list
should be public integer getSize
return size
10)
Declare function to insert an element at beginning
declare public void insert at start (some name indicating to insert
in the beginning) with parameter integer value
Declare:
Node (object name ex: as) equal to new Node (value, null)
increment size
if condition start equal to null
start equal to as
end equal to start
else condition
as.setLink (start)
start equal to as
11)
Declare function to insert an element at end
Should be public void insert at end (or something) with
parameter integer value
Declare:
Node (object name ex: as) equal to new Node (value, null)
increment size
if condition start equal to null
start equal to as
end equal to start
else condition
as.setLink (as)
end equal to as
12)
Declare function to insert an element at position
Should be public void insert in position (or something similar)
with paramaters integer value and integer position
Declare:
Node (object name ex: as) equal to new Node (value, null)
Node (called: es) equal to start
Position equal to position minus 1
create for loop where i equal 1 and i less then size and increment i
inside of that loop create condition
if i equal position
Node temp equal to es.getLink();
es.setLink(as)
as.setLink(temp)
break
//after closed bracket continue
es equal to es.getLink();
//after closed bracket
increment size
13)
Declare function to delete an element at position
Should be public void delete in position (or something similar) with parameter integer position
condition if position equal to 1
start = start.getLink();
decrement size
return
condition if position equal to size
Node a (or any other name) equal to start
Node b (or any other name) equal to start
loop while a not equal to end
b equal to a
a equal to a.getLink();
//after closed bracket continue
end equal to b
end.setLink(null)
decrement size
return
//after closed bracket continue
Node aa(or any other name) equal to start
position equal to position minus 1
create for loop where i equal 1 and i less size minus 1 and increment i
condition if i equal to position
Node temp(or any name you want) equal to aa.getLink();
temp equal to temp.getLink();
aa.setLink(temp);
break
//after closed bracket continue
aa equal aa.getLink();
//after another closed bracket
decrement size
14)
Declare function to display
Should be public void display
sys.print Linked list:
condition if size equal to zero
sys.print empty value
return
condition if start.getLink() equal to null
sys print(start.getData());
return
Node aaa equal to start
sys.print(start.getData() + " - ");
aaa equal to aaa.getLink();
//after closed bracket
sys.print (aaa.getData() + " \n");
15)
Scanner scan = new Scanner(System.in);
linkedList list = new linkedList();
System.out.println("Linked List: ");
char ch;
do
{
tag1:
System.out.println("\nLinked List Menu\n");
System.out.println("1. Insert at start");
System.out.println("2. Insert at end");
System.out.println("3. Insert in Position");
System.out.println("4. Delete in Position");
System.out.println("5. Check if its empty") ;
System.out.println("6. Get Size of Linked List");
int choice = scan.nextInt();
switch (choice)
{
case 1 : Insert an element to be stored at the
beginning of linkedlist
int num=scan.nextInt();
Call the method insert_at_start and pass num as an input to be
stored at the beginning.
i.e call insert_at_start(the value taken input i.e.
num)
Break from the switch statement once the above statement executed using break command.
case 2 : Insert an element to be stored at the end of
linkedlist
int num=scan.nextInt();
Call the method insert_at_end and pass num as an input to be stored
at the end.
i.e call insert_at_end(the value taken input i.e.
num)
Break from the switch statement once the above statement executed using break command.
case 3 : Insert an element to be stored at specific
position in linkedlist
int num=scan.nextInt();
Call the method insert_in_position and pass num as an input to be
stored at the beginning.
i.e call insert_in_position(the value taken input i.e.
num)
Break from the switch statement once the above statement executed using break command.
case 4 : Input an element to be deleted from a
specific position in linkedlist.
int num=scan.nextInt();
Call the method delete_in_position and pass num as an input to be
stored at the beginning.
i.e call delete_in_position(the value taken input i.e.
num)
Break from the switch statement once the above statement executed using break command.
case 5: Call the function empty which has a return type
of boolean to check whether its empty
or not i.e. call empty and store its return bool value in a
variable of type bool.
Print if linked list is empty or not( check the returned bool for 0
or 1)
Break from the switch statement once the above statement executed using break command.
case 6: Call the function getSize which has a return
type of integer to get the size of linkedlist
i.e. call getSize and store return value in a variable.
Print the return value i.e. the current size of the
linkedlist.
Break from the switch statement once the above statement executed using break command.
default: Print the default error message to give correct input from 1 to 6 only.
Close the do-while loop with the condition to check if
user want to enter more or quit the interactive mode
i.e
Run while loop and ask user to confirm more inputs or not(can be done by taking input as Y or N)
if yes, go to tag1, otherwise exit.