Linked List

Discussion in 'C' started by e4321, Jan 23, 2009.

  1. e4321

    e4321 New Member

    Joined:
    Jan 13, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    This is a short program for a linked list. I am not sure if I got the process correctly. Any help is much appreciated.
    Code:
     
    #pragma once
    class LinkedList
    {
    public:
     LinkedList(void);
     ~LinkedList(void);
     void add(char c){
      Node temp=new Node;
      Node temp2=new Node;
      temp->token=c;
      if(strPtr==NULL){
       strPtr=temp;
       strPtr->link=NULL;
      }
      else{
       for(temp=strPtr; temp->link!=NULL; temp=temp->link){}
       temp2->token=c;
       temp->link=temp2;
       temp2->link=NULL;
      }
     }
     
     void remove(Node node){
      Node temp;
      temp=strPtr;
      if(temp==node){
       strPtr=NULL;
       delete temp;
      }
      else if (strPtr!=NULL){
       for(temp=strPtr; temp->link!=node; temp=temp->link){}
       temp=temp->link->link;
       delete temp->link;
      }
     }
    private:
     struct Node{
      char * token;
      Node * link;  
     } 
     Node * stPtr=NULL;
    };
     
    Last edited by a moderator: Jan 23, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    There's no main function so it's impossible to know how you're using it. Write a main function, create a list, add some items at varying points in the list (top, middle, end), delete some items (also from varying places including the endpoints), display the resulting list and see if it's what you expect. If not then you've got a problem.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice