Arti Languages Pre Seed Send Ahead Pitchdeck 2024.pdf
algorithms_in_linkedlist (1).pdf
1. Creating a Linked list :
Algorithm :
Create first node and assign the address of the first node to the
pointer ‘start’.
Step : 1 start =getnode( );
Use another pointer to store the address of the first node, called
CURRPTR
Step : 2 currptr=Start;
Accept the item and store it in the INFO field of the node
Step : 3 info[currptr ]= item;
Accept the Choice from the keyboard whether to create another node
or not
Step : 4 if ( choice = ‘Y’)
goto step 5
else
goto step 6
If choice is ‘Y’ to create another node
2. Step : 5 i) Newnode = getnode( );
Newnode which contains the address of the newly created node
ii) link[currptr]=Newnode;
Making a link between the original last node and present newnode
iii) currptr=Newnode;
Address of the Newnode is assigned to the original newnode
iv) info[currptr]=item;
Accepting the item and storing it in the info field of currptr
go to step 4
If choice is ‘N’ to make the LINK field of the last node to NULL
Step : 6 link [currptr] = NULL and
Exit
Step : 7 return
3. • Traversing a linked list :
Algorithm:
Step 1 : Is linked list is empty
if ( start ==NULL)
display (“ linked list is empty “);
exit( )
Step 2 : Assign start value to CURRPTR
currptr=start;
Step 3 : Repeat while(currptr!=NULL)
Process info[currptr]
Step 4 : currptr=link[currptr] /* moving the currptr from one
node to the next node */
Step 5 : Exit
4. • Algorithm for Displaying a list :
• first check list is empty or not
• Step 1 : if ( start == NULL)
– Write (“ list is Empty”)
– return;
Store the Address of first node in another pointer CURRPTR
• Step 2 : set currptr = start;
• Step 3 : Repeat step4 and step5 while ( currptr !=NULL)
• Display items one-by-one from first node until Currptr becomes
NULL
• Step 4 : display ( info[currptr]);
After the display of this element ,Currptr should point to the next node
• Step 5 : currptr=link[currptr];
• Step 6 : Exit
5. • Insert operation at the beginning :
• Algorithm :
• Creating a node that is to be inserted
• step 1 : Newnode =getnode( );
• Enter the item into the Info field of the New node
• step 2 : info [Newnode]= item;
• Assign the start value to the LINK part of the inserted node(New
node).
• It makes a link from ‘New node’ to the previous first node.
• Now start and New node points to the original first node of the list.
• It makes link between start and Newnode.
• step 3 : link [Newnode]=start;
• step 4 : start = Newnode;
Exit;
6. • Algorithm : insert operation at end
Step 1 : if list is empty
if ( start == NULL)
insert at the beginning
exit( )
end if
Step 2 : currptr = start;
Traverse the list to obtain the last node address
Step 3 : while (link [currptr]! = NULL)
currptr = link [ currptr]
end while
Create a newnode and enter the accepted ITEM into its INFO fields
Step 4 : Newnode =getnode( );
Newnode -> info =item;
Make a link between Currptr(last node) and New node
Step 5 : link [currptr] = Newnode;
Step 6 : link [Newnode] = NULL
Step 7 : Exit
7. • Algorithm for Inserting at certain position :
Step 1: Set CURRPTR to Start
Currptr=Start
Step 2: If the item is to be inserted at 1st position
if (pos==1)
Algorithm for inserting at beginning
Else go to step 3
Step 3: If the item to inserted at some position other than 1st position
for ( i=0; i < pos-2;i++)
{
Currptr= LINK[Currptr]
}
Step 4: Create a Newnode
Newnode=getnode();
8. Step 5: Enter the element in the INFO field of Newnode
INFO[Newnode]=ITEM;
Step 6: Make a connection between the Newnode and the next node after
CURRPTR
LINK[Newnode]=LINK[Currptr]
Step 7: Make a link between Currptr and newnode
LINK[Currptr]=Newnode;
Step 8 : Exit
9. • Delete a node from the beginning:
• Algorithm:
Step 1 : check whether the list is empty
if ( start==NULL)
• write “ linked list is empty”
• else goto step 2
Step 2 : Set Currptr to start
Currptr=start;
Step 3 : Assign the link field of the first node to ‘start’ to delete the
original first node
start = link[start];
Step 4 : Free the deleted item
free(Currptr);
Step 5 : Exit
10. • Delete a node At the end:
• Algorithm:
Step 1 : check whether the list is empty
if ( start==NULL)
• write “ linked list is empty”
• else goto step 2
Step 2 : check if the list has only one element
if ( Link[start]==NULL) then
start=NULL;
else goto step 4
Step 3 : free the deleted node
free(start);
Step 4 : Assign start value to Currptr
Currptr=start;
Step 5 : Assign NULL to the another pointer PREVPTR
PREVPTR = NULL
11. Step 6 : Traverse the list until the Currptr points to last node
and PREVPTR points to the last before node
While(Link[Currptr]!=NULL)
{
PREVPTR=Currptr;
Currptr = Currptr -> link;
Step 7 : To make the LINK part of the last node, after
deletion to NULL
PREVPTR->LINK=NULL
Step 8 : Exit
12. • Delete a node at the given position:
• Algorithm:
→ Step 1: Check for the position whether the deletion is at first position
if ( pos==1)
Algorithm for deleting a node at beginning
else goto Step 2
→ Step 2 : Set Currptr to Start
Currptr=start
→ Step 3: set PREVPTR to NULL
PREVPTR = NULL
→Step 4 : if the item to be deleted is not at first position
for ( i=1; i<pos; i++ )
{
PREVPTR=Currptr;
Currptr = Currptr->link
}
13. • → Step 5 : LINK of PREVPTR is assigned with Link of
Currptr i.e. … make a link between the PREVPTR and the
next node of Currptr
PREVPTR → Link = Currptr→link
Step 6 : Exit
14. • Searching in a single linked list
• Algorithm :
• Step 1: set Currptr=start LOC = NULL
• Step 2: Repeat step 3 while (Currptr !=NULL)
• Step 3: if (item == Info [Currptr] )
– Then LOC = Currptr
– And display “ Search Successful “
– Exit
– else
– Currptr = Link [ Currptr ]
[ so Currptr, Now points to the next node ]
• Step 4: if LOC = NULL
Display “ Search unsuccessful “
• Step 5 : Exit
15. • Garbage Collection:
Garbage collection is the automatic reclamation of computer storage
The GC function is to find data objects that are no longer in use and make their
space available by the running program.
So Why Garbage Collection:
A software routine operating on data structure should not have to depend
what other routines may be operating on the same structure.
If the process does not free used memory the unused space is accumulated
until the process terminates.
Garbage collection is considered cheaper than explicit deal location
A good garbage collector shows a program down by factor of 10 percent.
Although it seems a lot , it is only a small price to pay for :
Convenience
Development time
Reliability