WIP Borland C Doubly linked list

This is a discussion on WIP Borland C Doubly linked list within the C Programming forums, part of the General Programming Boards category; Hi all! I need to create a programm wich represents doubly linked list. It should do the following 1. Create ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    WIP Borland C Doubly linked list

    Hi all!

    I need to create a programm wich represents doubly linked list. It should do the following
    1. Create new list
    2. Add new structure
    3. Delete selectes dtructure
    4. Load existing list from file
    5.Save list to file.

    It very difficult for me to understand. I've tryed to write some code. But I need somebody to help me to complete my project. I need just a little help.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <alloc.h>
    #include <string.h>
    struct item //the structure
    {char name[10]; int prc; int col; // there are 3 elements in the structure name, prc, col
      struct item *prev; // to  previous structure
      struct item *next; // to next structure
    };
    void build(void); // creates new structure
    void view(item *); // list
    struct item *head,*tail; // pointers
    main()
    {
    //поехали!
      build(); //so we create new structure
      list(head); // then we can list
    free(head);
    }
    void build(void) //create new node function
    {item *p,*pr; //
      pr=NULL;
      printf("Creating new node\n\n");
      do { p=(item *)malloc(sizeof(spis));
        printf("Поле Name: "); //So we begins to fill our structure
        gets(p->name); //fills the name element of the structure item
        p->v1=pred;
        if (pr!= NULL)
          pr->next=p;
        else
          head=p;
        pr=p;
        puts(" Finish- <esc>");
    }
    Can somebody show me how to fill prc and cal elemens of the structure.
    And then show them

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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"

  3. #3
    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;
    }

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    There are some errors I cant resolve. Does this code written for Borland C?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you want help with solving errors for any code tell us what the errors are

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > Does this code written for Borland C?
    Unfortunately for you, Bokarinho chose to write in C99 style, which is the very latest standard for C. Your really old compiler is going to have trouble with this.

    A couple of things to fix
    1. Change all the // comments to /**/ comments.
    2. Node *n = CreateNode(Nname, Prc, Col);
    needs to be split into a declaration and an assignment.

    @ Bokarinho
    > strncpy(n->r.name, name, strlen(name));
    > n->r.name[strlen(name)] = '\0';
    Why not do just strcpy() in this case, since what you have is functionally identical.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by Salem View Post
    > Does this code written for Borland C?
    Unfortunately for you, Bokarinho chose to write in C99 style, which is the very latest standard for C. Your really old compiler is going to have trouble with this.

    A couple of things to fix
    1. Change all the // comments to /**/ comments.
    2. Node *n = CreateNode(Nname, Prc, Col);
    needs to be split into a declaration and an assignment.

    @ Bokarinho
    > strncpy(n->r.name, name, strlen(name));
    > n->r.name[strlen(name)] = '\0';
    Why not do just strcpy() in this case, since what you have is functionally identical.
    Just to break the monotony..............
    LOL

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Ok I resolved practically all my problems with my programm. The code of Bokarinho its just what I want but I should use my code instead Now I need to realyze saving and loading from file. Can somebody help me to develop save and load functions for my code. I need it to complete my project.
    here is my code

    Code:
    include <stdio.h>
    #include <conio.h>
    #include <alloc.h>
    #include <string.h>
    struct item //name of the structure
    {char name[20];int prc; int col; //structure of the thread
      struct item *prev;
      struct item *next;
    };
    void build(void); //builds new list
    void view(item *);//view list
    void add(item *); //add to list
    void save(FILE *);//Save list to file
    struct item *head,*tail;
    main(){int c;
    FILE *in;.....
    
    void build(void)
    {item *p,*pr;
      pr=NULL;
        do { p=(item *)new item;
        clrscr();
        printf("Name а  : ");
        scanf("%s",&p->name);
        printf("Prc. : ");
        scanf("%d",&p->prc);
        printf("\nCol: ");
        scanf("%d",&p->col);
        p->prev=pr;
      if(pr != NULL)
        pr->next=p;
      else
        head=p;
        pr=p;
        printf("\nFinish <esc>");
    }
    while (getch()!=27);
        tail=p;
        tail->next=NULL;
    }
    void view(item *p)
    {int i=1;
    clrscr();
    ");
    if (p==head)
      while (p != NULL)
      {printf("\n%-2d%-15s\t%3d\t%3d",i,p->name, p->prc, p->col);
      i++;
      p=p->next;
        }
      else
        puts("Error ");
        getch();
      }
    I think save funktion should look like this but it just a fought
    Code:
    void save(FILE *);
    p==head;
    in=fopen("data.txt",wb");
    clrscr();
    printf("\nSave");
    while (p != NULL)
    fwrite(&item,sizeof(item),1,in);
    p=p->next;
    getch();
    }
    Last edited by BCWarrior; 09-17-2007 at 03:07 PM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >Can somebody help me to develop save and load functions for my code
    You could save the linked link in two ways
    1. You could just save the content of the each node in to the text file.
    2. You could save each node to a binary file.

    You coud use either of thise tech. To save a linked list.

    Code:
    /* Check out what you need sending here as a prameter */
    void save(....)
    {
         p==head;
         in=fopen("data.bat","wb"); 
         /* Don't use this function */
         clrscr();
         printf("\nSave");
         while (p != NULL)
         {
          fwrite(&item,sizeof(item),1,in);
          p=p->next;
         }
         getchar();
    }
    ssharish

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    I need to save and load from binary file
    This coda has too many errors. And btw what should i send as a parameters?

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well as far as i was aware i dint meant that code would work either. I throught it would just be an idea or so. You will have to work along with that code.

    The parameter might be up to you. If you have declared your head pointer as global then u dont need sending that as a parameter. But other than that, if you think you need sending the FILE pointer you could send that. Or you could just send the file name and the head pointer.

    And in the function u could open the file using the filename sent and starting writing on to.

    ssharish

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Can you help me to make it work? I know this is not so difficult task but its too difficult for me

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Post the code which you have got so far. I mean the save function

    ssharish

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    23
    Ok thats the way I immagine that


    Code:
    struct item //name of the structure
    {char name[20];int prc; int col; //structure of the thread
      struct item *prev;
      struct item *next;
    };
    void build(void); //builds new list
    void view(item *);//view list
    void add(item *); //add to list
    void save(FILE *);//Save list to file prototype
    struct item *head,*tail;
    Then in function main I define a variable for file

    Code:
    main(){int c;
    FILE *in;
    And here the save function itself
    Code:
    void save(FILE *in)
    {
         p==head; //p -mean pointer
         in=fopen("data.txt","wb"); //create or open file data.txt
         while (p != NULL) //and untill the NULL of the list
         {
          fwrite(&item,sizeof(item),1,in); //write one structure size of item to file in binary mode
          p=p->next; //move next structure
         }
         getchar();
    }
    What to send as a parameters? THe code I've posted before is the whole code exept of menu.

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by BCWarrior View Post
    Ok thats the way I immagine that


    Code:
    struct item //name of the structure
    {char name[20];int prc; int col; //structure of the thread
      struct item *prev;
      struct item *next;
    };
    void build(void); //builds new list
    void view(item *);//view list
    void add(item *); //add to list
    void save(FILE *);//Save list to file prototype
    struct item *head,*tail;
    Then in function main I define a variable for file

    Code:
    main(){int c;
    FILE *in;
    And here the save function itself
    Code:
    void save(FILE *in)
    {
         p==head; //p -mean pointer
         in=fopen("data.txt","wb"); //create or open file data.txt
         while (p != NULL) //and untill the NULL of the list
         {
          fwrite(&item,sizeof(item),1,in); //write one structure size of item to file in binary mode
          p=p->next; //move next structure
         }
         getchar();
    }
    What to send as a parameters? THe code I've posted before is the whole code exept of menu.
    I dont know why you are still confused. Its simple as many people told you, i worked with text files so i can easily see the file without using a special program to read binary files or without afterwards loading it into memory to see if it succeeded. So the answer is the last function in my previous code.

    Code:
    //---------------------------------------------------------------------------
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    #define BUFSZ 512
    
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    
    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(void)
    {
         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)
    {
    	Node *n = NULL;
    	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;
         }
    }
    void PrintListToFile(List *l, char *filename)
    {
    	FILE *f = fopen(filename, "w");
    	if(f == NULL)
    	{
    		printf("Error, can not open '%s' file.\n", filename);
    		return;
    	}
    	else
    	{
    		Node *n = l->head;
    		char *Message = "Printing List Nodes.....";
    		fprintf(f,"%s\n", Message);
    		while(n)
    		{
    			if(fprintf(f,"%s\t%d\t%d\n", n->r.name, n->r.prc, n->r.col) > 0)
    				n = n->next;
    		}
    		fclose(f);
        }
    }
    int main(int argc, char *argv[])
    {
      List *l = NULL;
      l = CreateList();
      AddNode(l, "Nick", 24,45);
      AddNode(l, "Tom", 26,85);
      AddNode(l, "Snow", 56,42);
      PrintList(l);
      PrintListToFile(l, "list.txt");
      printf("Hit any Key to continue........\n");
      getch();
      return 0;
    }
    //---------------------------------------------------------------------------

Page 1 of 3 123 LastLast
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, 08:31 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12: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, 01:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21