The Dark Side of the Force is a pathway to many abilities some consider to be unnatural.

- Darth Sidious (Star Wars)

 

Main Menu

Home
Articles
SVTechie Blog
Links
Download
Discussion Forum
Photo Gallery
Quick Bites
FAQs

Login






Lost Password?
No account yet? Register

Statistics

We have 2 guests online

SVTechie Recommends


powered_by.png, 1 kB

Text Links


Home
Dynamic Memory Allocation Performance PDF Print E-mail
Written by SVTechie   
Sunday, 09 April 2006
Article Index
Dynamic Memory Allocation Performance
Quick Notes on Memory Allocation
ANSI C Linked List Code
C Linked List Code
Record Collapsing
Pool Based Library Implementation
Conclusion

 

 

This implementation is again, very simple Linked List Implementation in C++. Code structure is same as before except malloc is replaced with new and free is replaced with delete. Following is the code snippet for this algorithm. 


 C++ Based Link List Implementation
 #include <new>

typedef struct nodedef {
        int data;
        struct nodedef *next;
        } node;

int main () {
  node *str, *tmp, *cur;
  int i;

  str = cur = NULL;
  for (i = 0; i < NUM_REQUEST; i++) {

    // Create New Node
    // tmp = (node *) malloc (sizeof(node));
    tmp = new node;
    tmp->data = i;
    tmp->next = NULL;

    // Insert in the list
    if (str == NULL) {
      // First Node
      str = tmp;
    } else {
      // Insert at End (pointed by cur)
      cur->next = tmp;
    }
    cur = tmp;
  }

  // Deallocate each node
  tmp = str;
  for (cur = str->next; cur != NULL; cur = cur->next) {
    // free (tmp);
    delete tmp;
    tmp = cur;
  }

}

 



Last Updated ( Saturday, 06 May 2006 )
 
Next >
Wikipedia | Lightning X Products | N73 | Loans | Mortgage Calculator
© 2008 SVTechie :: Online Resources For Techies BY Techies
Joomla! is Free Software released under the GNU/GPL License.