Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C Program Help [Linked Lists]

  • 22-10-2002 08:48AM
    #1
    Closed Accounts Posts: 14,483 ✭✭✭✭


    This post has been deleted.


Comments

  • Closed Accounts Posts: 5,563 ✭✭✭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, Registered Users 2 Posts: 247 ✭✭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, Registered Users 2 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