Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
linkedlist_implentation_usingarray.cpp File Reference

Linked list implementation using Arrays. More...

#include <iostream>
Include dependency graph for linkedlist_implentation_usingarray.cpp:

Classes

class  Node
 

Functions

void initialise_list ()
 
int getnode ()
 
void freeNode (int nodeToBeDeleted)
 
void insertAtTheBeginning (int data)
 
void insertAtTheEnd (int data)
 
void display ()
 
int main ()
 

Variables

Node AvailArray [100]
 array that will act as nodes of a linked list.
 
int head = -1
 
int avail = 0
 

Detailed Description

Linked list implementation using Arrays.

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.

Function Documentation

◆ freeNode()

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.

42  {
43  AvailArray[nodeToBeDeleted].next = avail;
44  avail = nodeToBeDeleted;
45 }

◆ getnode()

int getnode ( )

This will return the index of the first free node present in the avail list

32  {
33  int NodeIndexToBeReturned = avail;
34  avail = AvailArray[avail].next;
35  return NodeIndexToBeReturned;
36 }

◆ insertAtTheBeginning()

void insertAtTheBeginning ( int  data)

The function will insert the given data into the front of the linked list.

50  {
51  int newNode = getnode();
52  AvailArray[newNode].data = data;
53  AvailArray[newNode].next = head;
54  head = newNode;
55 }
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function

79  {
80  initialise_list();
81  int x, y, z;
82  for (;;) {
83  std::cout << "1. Insert At The Beginning" << std::endl;
84  std::cout << "2. Insert At The End" << std::endl;
85  std::cout << "3. Display" << std::endl;
86  std::cout << "4.Exit" << std::endl;
87  std::cout << "Enter Your choice" << std::endl;
88  std::cin >> z;
89  switch (z) {
90  case 1:
91  std::cout << "Enter the number you want to enter" << std::endl;
92  std::cin >> x;
94  break;
95  case 2:
96  std::cout << "Enter the number you want to enter" << std::endl;
97  std::cin >> y;
98  insertAtTheEnd(y);
99  break;
100  case 3:
101  std::cout
102  << "The linked list contains the following element in order"
103  << std::endl;
104  display();
105  break;
106  case 4:
107  return 0;
108  default:
109  std::cout << "The entered choice is not correct" << std::endl;
110  }
111  }
112 
113  return 0;
114 }
std::cout
insertAtTheBeginning
void insertAtTheBeginning(int data)
Definition: linkedlist_implentation_usingarray.cpp:50
getnode
int getnode()
Definition: linkedlist_implentation_usingarray.cpp:32
data
int data[MAX]
test data
Definition: hash_search.cpp:24
std::endl
T endl(T... args)
std::cin
AvailArray
Node AvailArray[100]
array that will act as nodes of a linked list.
Definition: linkedlist_implentation_usingarray.cpp:19