Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   linked list in C (http://www.go4expert.com/forums/linked-list-c-t25140/)

mi_sah@yahoo.co.in 4Mar2011 09:15

linked list in C
 
hello everybody.. I need a simple linked list coding in C. Can u help me?.
Thankx

shabbir 4Mar2011 10:14

Re: linked list in C
 
Moved to C-C++ forum.

And See Sort Linked List

bglanzer 4Mar2011 12:37

Re: linked list in C
 
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


All times are GMT +5.5. The time now is 21:53.