Thread: WIP Borland C Doubly linked list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Start by properly indenting all of your code, adding the appropriate whitespace, checking your backets match up, make sure you declare main correctly as int, and don't cast the result of malloc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Heres a good start.....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct Rec
    {
            char *name;
            int prc;
            int col;
    }Record;
    
    typedef struct node
    {
            Record r;
            struct node *previous;
            struct node *next;
    }Node;
    
    typedef struct list
    {
            Node *head;
            Node *tail;
    }List;
    
    
    List *CreateList()
    {
         List *l = calloc(1, sizeof(List));
         if(l == NULL)
              return NULL;
          else
          {
              l->head = NULL;
              l->tail = NULL;
              
              return l;
          }
    }
    Node *CreateNode(char *name, int Prc, int Col)
    {
         Node *n = calloc(1, sizeof(Node));
         if(n == NULL)
              return NULL;
         else
         {
             n->next = NULL;
             n->previous = NULL;
             
             if(name == NULL)
             {
                     free(n);
                     return NULL;
             }
             else
             {
                 //You may use strdup here.
                 /*n->r.name = strdup(name);*/
                 //Init.
                 n->r.name = malloc((strlen(name) + 1) * sizeof(char));
                 strncpy(n->r.name, name, strlen(name));
                 n->r.name[strlen(name)] = '\0';
                 n->r.prc = Prc;
                 n->r.col = Col;
                 //Return the node.
                 return n;
             }
         }
    }
    //Add a new node to the end of the list
    int AddNode(List *l, char *Nname, int Prc, int Col)
    {
        if(l == NULL)
             return -1;
        if(Nname == NULL)
             return -2;
        Node *n = CreateNode(Nname, Prc, Col);
        if(n)
        {
             if(l->head == NULL)
                        l->head = l->tail = n;
             else
             {
                 n->previous = l->tail;
                 l->tail->next = n;
                 l->tail = n;
             }
             return 0;
        }
        else
            return -3;
    }
    void PrintList(List *l)
    {
         Node *temp = l->head;
         printf("Printing List:\n");
         while(temp)
         {
                    printf("%s\t%d\t%d\n", temp->r.name, temp->r.prc, temp->r.col);
                    temp = temp->next;
         }
    }
    int main(int argc, char *argv[])
    {
      
      List *l = CreateList();
      AddNode(l, "Nick", 24,45);
      AddNode(l, "Tom", 26,85);
      AddNode(l, "Snow", 56,42);
      PrintList(l);
      system("PAUSE");	
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small help on doubly linked list.
    By Depthcharge101 in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2007, 09:31 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM