A problem with lists

This is a discussion on A problem with lists within the C Programming forums, part of the General Programming Boards category; Hey! I'm pretty much a newbie when it comes to C programming. I have a problem with lists to solve. ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    A problem with lists

    Hey! I'm pretty much a newbie when it comes to C programming. I have a problem with lists to solve. I have ideas, I wrote a code. I believe the idea is pretty good but somehow I can't get it to work. I don't get any compile errors, I just get no results and at some point in the execution I get an error that forces me to stop.
    I'll try to be short with the requirement of the problem. Kids sit in a circle - one of them starts counting to k -> and this kth person is eliminated. Eventually I have to remain only with one child.
    Now...my idea. I placed the names of the kids in a file and in a circular list I simply put numbers from 1 to n representing the kids. I'd remain with one number than I'd take the name of this person from the file. But this last part isn't written yet, only the creation of the list and the game.

    Code:
    #include <conio.h>
    #include <string.h>
    #include <stdio.h>
    
    typedef struct el
    {
         unsigned no; 
         struct el *next ;
    } *adr;
    unsigned n,nr;
    adr bg,end;
    
    void create(adr &bg, adr &end)
    {
         adr crt;
         unsigned nr;
         nr=1;
         while (nr<=n)
         {
               crt=new el;
               crt->no=n;
               if (!bg) bg=crt;
               else end->next=crt;
               crt->next=NULL;
               end=crt;
               nr=nr+1;
         }
    }
    
    void print_list(adr bg)
    {
         adr crt;
         crt=bg;
         while (crt)
         {
               printf("%d ",crt->no);
               crt=crt->next;
         }
    }
    
    void game(unsigned nr, adr &bg)
    {
         adr crt,r;
         unsigned i;
         crt=bg;
         while (crt->next!=bg)
               crt=crt->next;
         while (bg->next!=bg)
         {
               for (i=1;i<nr;i++)
                   crt=crt->next;
               r=crt->next;
               crt->next=crt->next->next;
               if (r=bg) bg=bg->next;
               delete(r); 
         }
    }
    
    int main()
    {
        char ch,s[100],fil_nm[50]="filedir1.txt",aux[20],*gf;
        char arr[100][100],name[20][20];
        FILE *pf;
        pf=fopen(fil_nm,"w");
        printf("\nplease input the information about the files");
        printf("\nthe information about a file will be on a line");
        printf("\nEnd with CTRL+Z\n");
        while ((ch=getc(stdin))!=EOF)
        {
              putc(ch, pf);
              if (int(ch)==13) n=n+1;
        }
        fclose(pf);
        create(bg,end);
        print_list(bg);
        printf("\nPlease chose the number of the child that will start the game: ");
        scanf("%d",&nr);
        game(nr,bg);
        print_list(bg);
        getch();
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    operator new is a C++
    there are also some type mismatches, like assigning pointer to pointer to pointer...

    you should compile your code in the C-mode and fix all erros and warnings
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 06:26 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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