Thread: using delete and free

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    Unhappy using delete and free

    i am unable to use "delete" and "free" on dev shed c++. i wonder whether its related to some header file. please help.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Can you post the simplest example which demonstrates the problem, and the exact error message you are getting?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    heres the program

    Code:
    /*c program to represent a queue as a singly linked list*/
    
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct node
    {
         int data;
         struct node *link;
    };
    
    typedef struct node node;
    
    void buildQueue(node *,int );
    void display(node *);
    void deleteQueue(node *);
    
    int main()
    {
         node *start;
         start=NULL;
         buildQueue(start,10);
         buildQueue(start,20);
         buildQueue(start,30);
         buildQueue(start,40);
         
         display(start);
     
         deleteQueue(start);
         deleteQueue(start);
    
         display(start);
    
         getch();
         return 0;
    }
    
    void buildQueue(node *root,int data)
    {
          node *newnode;
          newnode=(node *)malloc(sizeof(node));
          newnode->data=data;
          newnode->link=NULL;
          if(root==NULL)
          {
                root=newnode;
          }
          else
          {
                newnode->link=root;
                root=newnode;
          }
    }
    
    void display(node *root)
    {
         while(root!=NULL)
         {
               printf("%d",root->data);
               root=root->link;
         }
    }
    
    void deleteQueue(node *root)
    {
          node *temp,*prev;
          temp=root;
          while(temp->link!=NULL)
          {
                temp=temp->link;
                prev=temp;
          }
          prev->link=NULL;
          delete temp;
    }
    i get 2 error messages in line number 75:"delete"undeclared and syntax error before"temp"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, delete, as used in this context, is a C++ keyword, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void deleteQueue(node *root)
    {
          node *temp,*prev;
          temp=root;
          while(temp->link!=NULL)
          {
                temp=temp->link;
                prev=temp;
          }
          prev->link=NULL;
          delete temp;                  <--- free(temp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-23-2010, 03:37 PM
  2. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  3. malloc & free in C vs. new & delete in C++
    By groberts1980 in forum C Programming
    Replies: 20
    Last Post: 10-17-2006, 10:00 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. free() or delete?
    By Trauts in forum C++ Programming
    Replies: 7
    Last Post: 05-14-2003, 04:20 PM

Tags for this Thread