2018-09-28 21:18:24 +08:00
/* The difference between the pointer implementation of linked list and array implementation of linked list:
1. The NULL is represented by - 1 ;
2. Limited size . ( in the following case it is 100 nodes at max ) . But we can reuse the nodes that are to be deleted by again linking it bacj to the list .
*/
2019-08-21 10:10:08 +08:00
# include <iostream>
2018-09-28 21:18:24 +08:00
using namespace std ;
2019-08-21 10:10:08 +08:00
struct Node
{
2018-09-28 21:18:24 +08:00
int data ;
int next ;
} ;
2019-08-21 10:10:08 +08:00
Node AvailArray [ 100 ] ; //array that will act as nodes of a linked list.
int head = - 1 ;
int avail = 0 ;
2018-09-28 21:18:24 +08:00
void initialise_list ( )
{
2019-08-21 10:10:08 +08:00
for ( int i = 0 ; i < = 98 ; i + + )
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
AvailArray [ i ] . next = i + 1 ;
2018-09-28 21:18:24 +08:00
}
2019-08-21 10:10:08 +08:00
AvailArray [ 99 ] . next = - 1 ; //indicating the end of the linked list.
2018-09-28 21:18:24 +08:00
}
2019-08-21 10:10:08 +08:00
int getnode ( ) //This will return the index of the first free node present in the avail list
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
int NodeIndexToBeReturned = avail ;
avail = AvailArray [ avail ] . next ;
2018-09-28 21:18:24 +08:00
return NodeIndexToBeReturned ;
}
2019-08-21 10:10:08 +08:00
void freeNode ( int nodeToBeDeleted ) //This function when called will delete the node with the index presented as an argument, and will put back that node into the array.
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
AvailArray [ nodeToBeDeleted ] . next = avail ;
avail = nodeToBeDeleted ;
2018-09-28 21:18:24 +08:00
}
2019-08-21 10:10:08 +08:00
void insertAtTheBeginning ( int data ) //The function will insert the given data into the front of the linked list.
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
int newNode = getnode ( ) ;
AvailArray [ newNode ] . data = data ;
AvailArray [ newNode ] . next = head ;
head = newNode ;
2018-09-28 21:18:24 +08:00
}
void insertAtTheEnd ( int data )
{
2019-08-21 10:10:08 +08:00
int newNode = getnode ( ) ;
int temp = head ;
while ( AvailArray [ temp ] . next ! = - 1 )
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
temp = AvailArray [ temp ] . next ;
2018-09-28 21:18:24 +08:00
}
//temp is now pointing to the end node.
2019-08-21 10:10:08 +08:00
AvailArray [ newNode ] . data = data ;
AvailArray [ newNode ] . next = - 1 ;
AvailArray [ temp ] . next = newNode ;
2018-09-28 21:18:24 +08:00
}
void display ( )
{
2019-08-21 10:10:08 +08:00
int temp = head ;
while ( temp ! = - 1 )
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
cout < < AvailArray [ temp ] . data < < " -> " ;
temp = AvailArray [ temp ] . next ;
2018-09-28 21:18:24 +08:00
}
2019-08-21 10:10:08 +08:00
cout < < " -1 " < < endl ;
;
2018-09-28 21:18:24 +08:00
}
int main ( )
2019-08-21 10:10:08 +08:00
{
initialise_list ( ) ;
int x , y , z ;
for ( ; ; )
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
cout < < " 1. Insert At The Beginning " < < endl ;
cout < < " 2. Insert At The End " < < endl ;
cout < < " 3. Display " < < endl ;
cout < < " 4.Exit " < < endl ;
cout < < " Enter Your choice " < < endl ;
cin > > z ;
switch ( z )
2018-09-28 21:18:24 +08:00
{
2019-08-21 10:10:08 +08:00
case 1 :
cout < < " Enter the number you want to enter " < < endl ;
cin > > x ;
insertAtTheBeginning ( x ) ;
break ;
case 2 :
cout < < " Enter the number you want to enter " < < endl ;
cin > > y ;
insertAtTheEnd ( y ) ;
break ;
case 3 :
cout < < " The linked list contains the following element in order " < < endl ;
display ( ) ;
break ;
case 4 :
exit ( 0 ) ;
default :
cout < < " The entered choice is not correct " < < endl ;
2018-09-28 21:18:24 +08:00
}
}
}