Thursday, February 18, 2016

Linked List Program In C++

*************************** Linked List Program In C++ ********************************

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Required Header File.
//
#include<Windows.h>
#include<iostream>
using namespace std;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Declare Structure for Linked List
//

typedef struct _NODE
{
DWORD dwNumber;
struct _NODE *pNext;
}NODE, *PNODE;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// Define A Class
//

class LinkedList
{
        //
        // Class Variable declare as private.
        //
PNODE pHead;
PNODE pTail;

       //
       // Function Defination.
       //
       public:
                //
                // Constructor.
               //
        LinkedList()
       {
     pHead = NULL;
     pTail = NULL;
        }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //
                // Call InsertAtFirst Function.
                //
        BOOL InsertAtFirst(DWORD dwNumber)
       {
      PNODE pNewNode = NULL;

      //
      // Allocate Memory For New Node
      //
      pNewNode = (PNODE)malloc(sizeof(NODE));

      if(pNewNode == NULL) // Function Return Value
      {
       //
       // TruePath  : Fail To Return
       //
        return 1;
      }
                      //
                      // Structure Variable Initialisation
                      //
      pNewNode->dwNumber = dwNumber;
      pNewNode->pNext = NULL;

      //
      // To check List is Empty
      //
      if(pHead == NULL)
     {
    //
    // TruePath  : List is Empty
   //
   pHead = pNewNode;
   pTail = pNewNode;
    }
   else
   {
  //
  // List Contains Atleast One Node
 //
 pNewNode->pNext = pHead;

 pHead = pNewNode;
   }
    return 0;

    //
    // Processing Completed
    //
     }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             //
             // Call InsertAtLast Function.
             //

     BOOL InsertAtLast(DWORD dwNumber)
    {
     PNODE pNewNode = NULL;

    //
   // Allocate Memory For New Node
   //
   pNewNode = (PNODE)malloc(sizeof(NODE));

   if(pNewNode == NULL) // Function Return Value
   {
          //
  // TruePath  : Fail To Return
  //
  return 1;
   }
   pNewNode->dwNumber = dwNumber;
   pNewNode->pNext = NULL;

   //
   // To check List is Empty
   //
   if(pHead == NULL)
  {
   //
   // TruePath  : List is Empty
   //
   pHead = pNewNode;
  pNewNode = pTail;
  }
  else
  {
 //
 // List Contains Atleast One Node
 //
 pTail->pNext = pNewNode;

 pTail = pNewNode;
  }
  return 0;

 //
 // Processing Completed
   //
  }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //
 // Delete Node From Linked List
 //
 BOOL DeleteAtFirst()
 {
  PNODE pTemp = pHead;

//
// To Check Linked List is Empty
//
if(pTemp == NULL)
{
//
// TruePath : List Is Empty
//
return 1;
}
else if(pTemp->pNext == NULL)
{
//
// TruePath : list Contain Atleast One Node
//
free(pTemp);
}
else
{
pHead = pHead->pNext;
free(pTemp);
}
return 0;

//
// Processing Completed
//
 }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //
 // Delete First Node From Linked List
 //
 BOOL DeleteAtLast()
 {
PNODE pTemp = pHead;

//
// To Check Linked List is Empty
//
if(pTemp == NULL)
{
//
// TruePath : List Is Empty
//
return 1;
}
else if(pTemp->pNext == NULL)
{
//
// TruePath : list Contain Atleast One Node
//
free(pTemp);
}
else
{
while(pTemp->pNext->pNext != NULL)
{
pTemp = pTemp->pNext;
}
free(pTemp->pNext);
pTemp->pNext = NULL;
pTail = pTemp;
}
return 0;

//
// Processing Completed
//
 }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //
 // Function Return First Node
 //
 PNODE GetFirstNode()
 {
if(pHead == NULL)
{
//
// TreuPath  : List Is Empty
//
return NULL;
}
else
{
//
// TruePath : Return First Node
//
return pHead;
}

//
// Processing Completed
//
  }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   //
   // Function Return Last Node
   //

    PNODE GetLastNode()
  {
if(pHead == NULL)
{
//
// TreuPath  : List Is Empty
//
return NULL;
}
else
{
//
// TruePath : Return First Node
//
return pTail;
}

//
// Processing Completed
//
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Display The Content From Linked List
//

DWORD Display()
{
PNODE pTemp = pHead;
DWORD dwCounter = 0x0;
while(pTemp != NULL)
{
dwCounter++;
cout<<"|"<<pTemp->dwNumber<<"|->";
pTemp = pTemp->pNext;
}
cout<<endl;
return dwCounter;

//
// Processing Completed
//
}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Entry Point Function
//

int main()
{
LinkedList ll;

PNODE pFirstNode;
PNODE pLastNode;
//
// Add Element into List in First Position
//

ll.InsertAtFirst(10);
ll.InsertAtFirst(20);
ll.InsertAtFirst(30);

//
// Display Content From List
//
ll.Display();

//
// Add Element into List in Last Position
//
ll.InsertAtLast(50);
cout<<"\n";
//
// Display Content From List
//
ll.Display();

//
// Delete Element From First Position
//
ll.DeleteAtFirst();
cout<<"\n";

//
// Display Content From List
//
ll.Display();

//
// Add Element into List in Last Position
//
ll.InsertAtLast(100);

cout<<"\n";

//
// Display Content From List
//
ll.Display();

//
// Delete Element From Last Position
//
ll.DeleteAtLast();

cout<<"\n";

//
// Display Content From List
//
ll.Display();

//
// Add Element into List in Last Position
//
ll.InsertAtLast(200);

cout<<"\n";

//
// Display Content From List
//
DWORD dwNumber = ll.Display();
cout<<dwNumber<<endl;

//
// Get First Node From List
//
pFirstNode = ll.GetFirstNode();

cout<<pFirstNode->dwNumber<<endl;

//
// Get Last Node From List
//
pLastNode = ll.GetLastNode();

cout<<pLastNode->dwNumber<<endl;
return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...