Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Program Help [Linked Lists]

Options
  • 22-10-2002 8:48am
    #1
    Closed Accounts Posts: 14,483 ✭✭✭✭


    This post has been deleted.


Comments

  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    /*Posted to boards.ie by
       Typedef [email]cout@eircom.net[/email]
       Vote No to Visual Basic 'Programming'
       list.h
    */
    #ifndef LIST
    #define LIST
    #include "ntlmd.h"
    
    typedef struct list
    {
    	lmd*l;
    	int list_id;
    	struct list*next;
    	struct list*previous;
    }list_t;
    
    void list_add(lmd * o);
    void list_del();
    void list_traverse();
    void list_rmentry(int list_id);
    #endif
    
    /*Posted to boards.ie by
       Typedef [email]cout@eircom.net[/email]
       Vote No to Visual Basic 'Programming'
       list.cpp
    */
    #include "list.h"
    
    list_t*tail=NULL;
    list_t*head=tail;
    void list_add(lmd * o)
    {
     list_t*a;
     static int tracker;
     a=new list_t;
     a->l=o;
     o->list_id=tracker;
     a->previous=tail;
     if(tail!=NULL)
     	tail->next=a;
     a->next=NULL;
     tail=a;
     tracker++;
    return;
    };
    void list_traverse()
    {
     list_t*n;
     n=tail;
    
     printf("Traversing instanciated list of lmd objects now\n");
     while(n!=NULL)
     {
     	
     	printf("node %d\n",n->l->list_id);
    	n=n->previous;
     };
     return;
    };
    void list_del()
    {
     list_t*a;
    
     while(tail!=NULL)
    	{
    	 a=tail;
    	 tail=tail->previous;
     	 delete a;
    	};
     return;
    };
    void list_rmentry(int list_id)
    {
     list_t*w;
     
     w=tail;
     bool done=false;
     
     while(!done)
     {
      if(w->l->list_id!=list_id)
    	 w=w->previous;
      else
       {
        if(w->previous!=NULL)
        	w->previous->next=w->next;
    	
        if(w->next!=NULL)
        	w->next->previous=w->previous;
        else
        	{
    	 tail=w->previous;
        	 w->previous->next=NULL;
    	};
        delete w;
        done=true;
       };
      };
     return;
    };
    
    Right here is a doubley linked list that I am using as part of a project, that umm you can look at and rob code from if it helps.
    Fees to be paid in unmarked, non sequentially numbered bills, care of my Camen Island's account.


  • Registered Users Posts: 246 ✭✭Grayarea


    while(data != -1){
    printf("\nEnter the next number>");
    scanf("%d", &data);
    new_node = (Int_List *)malloc(sizeof(Int_List));
    new_node->number = data;
    to_fill->link = new_node;
    }

    Should read

    while(data != -1){
    printf("\nEnter the next number>");
    scanf("%d", &data);
    new_node = (Int_List *)malloc(sizeof(Int_List));
    new_node->number = data;
    to_fill->link = new_node;
    to_fill = new_node;
    }

    Otherwise you are just overwrighting the first link.


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    [php]
    /*Define Data Sent Variable*/
    #define SENT = -1
    [/php]

    there should be no '=' there, but it's ok since you typed -1 into your code everywhere =)


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Advertisement
  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


Advertisement