linked list in C

mi_sah@yahoo.co.in's Avatar, Join Date: Mar 2011
Newbie Member
hello everybody.. I need a simple linked list coding in C. Can u help me?.
Thankx
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Moved to C-C++ forum.

And See Sort Linked List
0
bglanzer's Avatar, Join Date: Mar 2011
Newbie Member
Here is a simple overview of linked list I will just cover some of the basics.

Code:
 
template<typename nodeType>
class List
{
public:
     List();
     ~List();
 
     void PushFront(const nodeType&);
     void PushBack(const nodeType&);
     void PopFront();
     void PopBack();
     void Clear();
private:
     Node<nodeType> *pHead;  //Always points to the first node
     Node<nodeType> *pTail;   //Always points to the last node
};
 
template<typename nodeType>
List::List():pHead(0),pTail(0){}
 
template<typename nodeType>
List::~List()
{
     Clear();
}
 
template<typename nodeType>
void List::PushFront(const nodeType &node)
{
    Node<nodeType> *nodePtr=new Node<nodeType>(node);
 
    if(isEmpty())
       pHead=pTail=nodePtr;
    else
     {
           nodePtr->pNext=pHead;
           pHead=nodePtr;
      }
}
 
template<typename nodeType>
void List::PopFront()
{
    Node<nodeType> *nodePtr=pHead;
    pHead=pHead->pNext;
 
    delete nodePtr;
}
 
template<typename nodeType>
void List::Clear()
{
   Node<nodeType> *nodePtr;
 
   while(pHead!=pTail)
    {
         nodePtr=pHead;
         pHead=pHead->pNext;
 
         delete nodePtr;
    }
    //delete tail
   delete pHead;
   pHead=0;
   pTail=0;
}
PushBack and PopBack should be pretty simple a node is simply something as simple as this

Code:
 
template <typename type>
class Node
{
public:
   Node();
   ~Node();
 
   Node<type> *pNext;
   Node<type> *pPrev;
};
I hope that this helps

Brendon Glanzer
UAT student