Thread: Problem woth new List

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    16

    Question Problem woth new List

    Problem is that t1=new(list); and the socond is t1=new(list);
    and I dont know why.. please help me...
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    typedef struct list{
      char data;
        struct list *link;
        }list;
    
    
    void create_list(list **);
    void preview_list(list *);
    void clear_memory(list *);
    
    
    
    
    void main()
    { 
      list *p1=NULL,*p2=NULL,*p=NULL;
      list *t1,*t2,*q1,*q2,*tp;
    
    
    
    
       create_list(&p);
       puts("Start");
       preview_list(p);
    
    
       tp=p;
    
    
       while (tp){
          if (tp->data<0){
         t1=new(list); t1->data=tp->data; t1->link=NULL;
         if (p1==NULL) {p1=t1; q1=t1;}
            else {q1->link=t1; q1=t1;}
          }
         else{
            t2=new(list); t2->data=tp->data; t2->link=NULL;
            if (p2==NULL) {p2=t2; q2=t2;}
               else {q2->link=t2; q2=t2;}
         }
          tp=tp->link;
       }
    
    
       puts("\nNegative elements");
       preview_list(p1);
    
    
    
    
       puts("\nPozitive elements");
       preview_list(p2);
    
    
    
    
       clear_memory(p);clear_memory(p1);clear_memory(p2);
    
    
    }
    
    
    void create_list(list **top)
    {  int i,n;
       list *t,*q;
    
    
       srand(time(NULL));
    
    
       puts("Enter number of elements");
       printf("n= "); scanf("%d",&n);
    
    
       t=new(list); t->data=rand()%40-20; t->link=NULL;
       *top=t; q=t;
    
    
     
       for(i=1; i<n; i++){
           t=new(list); t->data=rand()%40-20; t->link=NULL;
           q->link=t; q=t;
       }
    }
    
    
    void preview_list(list *top)
    {
       list *t;
       t=top;
    
    
       while (t)
       {  printf("%d ",t->data);
          t=t->link;
       }
       getch();
    }
    
    
    void clear_memory(list *top)
    { list *t;
    
    
    t=top;
    while(t)
    {
    top=top->link;
    delete(t);
    t=top;
    }
    
    
    }
    those problem i have that i compil my program

    Problem woth new List-png
    Last edited by R0tleSS; 02-03-2018 at 03:37 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    "new" is not a C keyword. You'll need to use malloc()

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    16
    Problem woth new List-454-png

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    delete is also C++, use free() instead.

    Also,
    1. main returns int, not void.
    2. conio.h and getch() are obsolete (have been so for the past 25 years)
    3. The srand() call would be better at the start of main, where you can reasonably assure that you only call it once.
    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.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    In addition to what Salem mentioned, you may want to read the documentation for malloc(). The malloc() function has one parameter (the size, in bytes, of the memory block to allocate) and you're passing it a type (not a value which is what malloc expects). Edit: therefore you probably want to read about the sizeof operator as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help woth cout 3d string array
    By j_d in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2010, 12:19 AM
  2. Problem woth CLK_TCK
    By ramayana in forum C Programming
    Replies: 5
    Last Post: 11-03-2006, 07:31 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Need help woth fractions..
    By snapshooter in forum C Programming
    Replies: 4
    Last Post: 11-17-2004, 06:51 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread