how to create linked list using c_c++ - codeproject

16
× highlights off 10,578,186 members (65,766 online) Not quite what you are looking for? You may want to try: A Beginner's Guide to the Linked List Windows 7 Goodies in C++: Adding Custom Tasks to Jump Lists Sign in home quick answers discussions features community help creating a linked list project Articles » Languages » C / C++ Language » Howto Article Browse Code Bugs / Suggestions Stats Revisions Alternatives Comments (64) View this article's Workspace Fork this Workspace Connect using Git Share About Article The article describes the fundamental theory about pointers and show how it helps to create linked list Type Article Licence CPOL First Posted 26 Mar 2008 Views 735,035 Downloads 9,222 Bookmarked 73 times VC6 C++ Windows Visual-Studio Dev , + Next Rate this: How to create Linked list using C/C++ By Nasif M., 26 Mar 2008 Download Linked_ list - 2.52 KB Introduction Linked list is one of the fundamental data structures, and can be used to implement other data structures. In a linked list there are different numbers of nodes. Each node is consists of two fields. The first field holds the value or data and the second field holds the reference to the next node or null if the linked list is empty. Figure: Linked list Pseudocode: Collapse | Copy Code Linked list Node { data // The value or data stored in the node next // A reference to the next node, null for last node } The singly- linked list is the easiest of the linked list, which has one link per node. Pointer To create linked list in C/C++ we must have a clear understanding about pointer. Now I will explain in brief what is pointer and how it works. A pointer is a variable that contains the address of a variable. The question is why we need pointer? Or why it is so powerful? The answer is they have been part of the C/C++ language and 4.47 (63 votes) articles How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u... 1 of 16 5/1/2014 6:31 PM

Upload: robert-cross

Post on 24-Nov-2015

45 views

Category:

Documents


1 download

DESCRIPTION

Linked Lists in C++

TRANSCRIPT

  • highlights off

    10,578,186 members (65,766 online)

    Not quite what you are looking for? You may want to try:

    A Beginner's Guide to the Linked List

    Windows 7 Goodies in C++: Adding Custom Tasks to Jump Lists

    Sign in

    home quick answers discussions features community

    helpcreating a linked list project

    Articles Languages C / C++ Language Howto

    Article

    Browse Code

    Bugs / Suggestions

    Stats

    Revisions

    Alternatives

    Comments (64)

    View this article's

    Workspace

    Fork this Workspace

    Connect using Git

    Share

    About Article

    The article describes the

    fundamental theory about

    pointers and show how it

    helps to create linked list

    Type Article

    Licence CPOL

    First Posted 26 Mar 2008

    Views 735,035

    Downloads 9,222

    Bookmarked 73 times

    VC6 C++ Windows

    Visual-Studio Dev , +

    Next

    Rate this:

    How to create Linked list using C/C++By Nasif M., 26 Mar 2008

    Download Linked_list - 2.52 KB

    Introduction

    Linked list is one of the fundamental data structures, and can be used to implement other data

    structures. In a linked list there are different numbers of nodes. Each node is consists of two

    fields. The first field holds the value or data and the second field holds the reference to the next

    node or null if the linked list is empty.

    Figure: Linked list

    Pseudocode:

    Collapse | Copy Code

    Linkedlist Node { data // The value or data stored in the node next // A reference to the next node, null for last node }

    The singly-linked list is the easiest of the linked list, which has one link per node.

    Pointer

    To create linked list in C/C++ we must have a clear understanding about pointer. Now I will

    explain in brief what is pointer and how it works.

    A pointer is a variable that contains the address of a variable. The question is why we need

    pointer? Or why it is so powerful? The answer is they have been part of the C/C++ language and

    4.47 (63 votes)

    articles

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    1 of 16 5/1/2014 6:31 PM

  • Top News

    Wireless charger can power

    40 mobile phones at once

    from 15 feet away

    Get the Insider News free each

    morning.

    Related Videos

    Related Articles

    A Beginner's Guide to the

    Linked List

    Windows 7 Goodies in C++:

    so we have to use it. Using pointer we can pass argument to the functions. Generally we pass

    them by value as a copy. So we cannot change them. But if we pass argument using pointer, we

    can modify them. To understand about pointers, we must know how computer store variable and

    its value. Now, I will show it here in a very simple way.

    Let us imagine that a computer memory is a long array and every array location has a distinct

    memory location.

    Collapse | Copy Code

    int a = 50 // initialize variable a

    Figure: Variable value store inside an array

    It is like a house which has an address and this house has only one room. So the full address is-

    Name of the house: a

    Name of the person/value who live here is: 50

    House Number: 4010

    If we want to change the person/value of this house, the conventional way is, type this code line

    Collapse | Copy Code

    a = 100 // new initialization

    But using pointer we can directly go to the memory location of 'a' and change the person/value

    of this house without disturbing a. This is the main point about pointer.

    Now the question is how we can use pointer. Type this code line:

    Collapse | Copy Code

    int *b; // declare pointer b

    We transfer the memory location of a to b.

    Collapse | Copy Code

    b = &a; // the unary operator & gives the address of an object

    Figure: Integer pointer b store the address of the integer variable a

    Now, we can change the value of a without accessing a.

    Collapse | Copy Code

    *b = 100; // change the value of 'a' using pointer b cout

  • Adding Custom Tasks to Jump

    Lists

    SharePoint 2010 - Create List

    Definition and Instance

    Simple Windows Service

    Sample

    A Skip List in C#

    XListCtrl - A custom-draw list

    control with subitem formatting

    Task List Feature of Visual

    Studio Framework

    Pretty IE Toolbar in C#

    A Beginners Guide to Dialog

    Based Applications - Part One

    Multiple Edit Form for

    Sharepoint List Item

    VS Recent Project List Cleaner

    Falling Blocks

    Tool for Converting VC++2005

    Project to Linux Makefile

    View the current library link

    order in a VC project

    Doubly-Linked List

    Implementation

    The CMake Build Manager

    The Basics of Linked Lists and

    Binary Trees

    StringBuilderPlus Improves

    Upon StringBuilder

    Converting anonymous types

    to any type

    HardLinks - Manage your library

    of common classes

    A permuted index (keyword-

    in-context) generator

    Related Research

    Learn Agile: Ten Tips for

    Launching and Testing High

    Quality Apps for the American

    Market

    Insider Secrets on API Security

    From Experts at Securosis

    [Webinar]

    The answer is affirmative. We can create a pointer of pointer.

    Collapse | Copy Code

    int **c; //declare a pointer to a pointer c = &b; //transfer the address of b to c

    So, we can change the value of a without disturbing variable a and pointer b.

    Collapse | Copy Code

    **c = 200; // change the value of a using pointer to a pointer c cout

  • Essential Keys to Mobile

    Usability

    How to Secure Your Software

    for the Mobile Apps Market

    We can observe that the memory address of a and the content of pointer b is same. The content

    of pointer c and the memory address of b is same.

    Linked list operation

    Now we have a clear view about pointer. So we are ready for creating linked list.

    Linked list structure

    Collapse | Copy Code

    typedef struct node { int data; // will store information node *next; // the reference to the next node};

    First we create a structure node. It has two members and first is int data which will store

    the information and second is node *next which will hold the address of the next node.

    Linked list structure is complete so now we will create linked list. We can insert data in the

    linked list from 'front' and at the same time from 'back. Now we will examine how we can insert

    data from front in the linked list.

    1) Insert from front

    At first initialize node type.

    Collapse | Copy Code

    node *head = NULL; //empty linked list

    Then we take the data input from the user and store in the node info variable. Create a

    temporary node node *temp and allocate space for it.

    Collapse | Copy Code

    node *temp; //create a temporary node temp = (node*)malloc(sizeof(node)); //allocate space for node

    Then place info to temp->data. So the first field of the node *temp is filled. Now

    temp->next must become a part of the remaining linked list (although now linked list is

    empty but imagine that we have a 2 node linked list and head is pointed at the front) So

    temp->next must copy the address of the *head (Because we want insert at first) and we also

    want that *head will always point at front. So *head must copy the address of the node

    *temp.

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    4 of 16 5/1/2014 6:31 PM

  • Figure: Insert at first

    Collapse | Copy Code

    temp->data = info; // store data(first field)temp->next=head; // store the address of the pointer head(second field)head = temp; // transfer the address of 'temp' to 'head'

    2) Traverse

    Now we want to see the information stored inside the linked list. We create node *temp1.

    Transfer the address of *head to *temp1. So *temp1 is also pointed at the front of the linked

    list. Linked list has 3 nodes.

    We can get the data from first node using temp1->data. To get data from second node, we

    shift *temp1 to the second node. Now we can get the data from second node.

    Collapse | Copy Code

    while( temp1!=NULL ){ coutdata

  • Collapse | Copy Code

    node *temp1; // create a temporary nodetemp1=(node*)malloc(sizeof(node)); // allocate space for nodetemp1 = head; // transfer the address of 'head' to 'temp1'while(temp1->next!=NULL) // go to the last node temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'

    Now, Create a temporary node node *temp and allocate space for it. Then place info to

    temp->data, so the first field of the node node *temp is filled. node *temp will be the last

    node of the linked list. For this reason, temp->next will be NULL. To create a connection

    between linked list and the new node, the last node of the existing linked list node *temp1`s

    second field temp1->next is pointed to node *temp.

    Figure: Insert at last

    Collapse | Copy Code

    node *temp; // create a temporary nodetemp = (node*)malloc(sizeof(node)); // allocate space for nodetemp->data = info; // store data(first field)temp->next = NULL; // second field will be null(last node)temp1->next = temp; // 'temp' node will be the last node

    4) Insert after specified number of nodes

    Insert data in the linked list after specified number of node is a little bit complicated. But the

    idea is simple. Suppose, we want to add a node after 2nd position. So, the new node must be in

    3rd position. The first step is to go the specified number of node. Let, node *temp1 is pointed to

    the 2nd node now.

    Collapse | Copy Code

    coutnode_number; // take the node number from user node *temp1; // create a temporary nodetemp1 = (node*)malloc(sizeof(node)); // allocate space for nodetemp1 = head; for( int i = 1 ; i < node_number ; i++ ){ temp1 = temp1->next; // go to the next node if( temp1 == NULL ) { cout

  • Now, Create a temporary node node *temp and allocate space for it. Then place info to

    temp->next , so the first field of the node node *temp is filled.

    Collapse | Copy Code

    node *temp; // create a temporary nodetemp = (node*)malloc(sizeof(node)); // allocate space for nodetemp->data = info; // store data(first field)

    To establish the connection between new node and the existing linked list, new nodes next

    must pointed to the 2nd nodes (temp1) next . The 2nd nodes (temp1) next must pointed to

    the new node(temp).

    Collapse | Copy Code

    temp->next = temp1->next; //transfer the address of temp1->next to temp->nexttemp1->next = temp; //transfer the address of temp to temp1->next

    Figure: Insert after specified number of nodes

    5) Delete from front

    Delete a node from linked list is relatively easy. First, we create node *temp. Transfer the

    address of *head to *temp. So *temp is pointed at the front of the linked list. We want to

    delete the first node. So transfer the address of temp->next to head so that it now pointed to

    the second node. Now free the space allocated for first node.

    Collapse | Copy Code

    node *temp; // create a temporary nodetemp = (node*)malloc(sizeof(node)); // allocate space for nodetemp = head; // transfer the address of 'head' to 'temp'head = temp->next; // transfer the address of 'temp->next' to 'head'free(temp);

    Figure: Delete at first node

    6) Delete from back

    The last node`s next of the linked list always pointed to NULL. So when we will delete the last

    node, the previous node of last nodeis now pointed at NULL. So, we will track last node and

    previous node of the last node in the linked list. Create temporary node * temp1 and

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    7 of 16 5/1/2014 6:31 PM

  • *old_temp.

    Collapse | Copy Code

    // create a temporary nodenode *temp1;temp1 = (node*)malloc(sizeof(node)); // allocate space for nodetemp1 = head; //transfer the address of head to temp1node *old_temp; // create a temporary nodeold_temp = (node*)malloc(sizeof(node)); // allocate space for node while(temp1->next!=NULL) // go to the last node{ old_temp = temp1; // transfer the address of 'temp1' to 'old_temp' temp1 = temp1->next; // transfer the address of 'temp1->next' to 'temp1'}

    Now node *temp1 is now pointed at the last node and *old_temp is pointed at the

    previous node of the last node. Now rest of the work is very simple. Previous node of the last

    node old_temp will be NULL so it become the last node of the linked list. Free the space

    allocated for last lode.

    Collapse | Copy Code

    old_temp->next = NULL; // previous node of the last node is nullfree(temp1);

    Figure: Delete at first last

    7) Delete specified number of node

    To delete a specified node in the linked list, we also require to find the specified node and

    previous node of the specified node. Create temporary node * temp1, *old_temp and

    allocate space for it. Take the input from user to know the number of the node.

    Collapse | Copy Code

    node *temp1; // create a temporary nodetemp1 = (node*)malloc(sizeof(node)); // allocate space for nodetemp1 = head; // transfer the address of 'head' to 'temp1' node *old_temp; // create a temporary nodeold_temp = (node*)malloc(sizeof(node)); // allocate space for nodeold_temp = temp1; // transfer the address of 'temp1' to 'old_temp'

    Collapse | Copy Code

    coutnode_number; // take location

    Collapse | Copy Code

    for( int i = 1 ; i < node_number ; i++ ){ old_temp = temp1; // store previous node temp1 = temp1->next; // store current node }

    Now node *temp1 is now pointed at the specified node and *old_temp is pointed at the

    previous node of the specified node. The previous node of the specified node must connect to

    the rest of the linked list so we transfer the address of temp1->next to old_temp->next.

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    8 of 16 5/1/2014 6:31 PM

  • Now free the space allocated for the specified node.

    Collapse | Copy Code

    old_temp->next = temp1->next; // transfer the address of 'temp1->next' to 'old_temp->next'free(temp1);

    8) Sort nodes

    Linked list sorting is very simple. It is just like ordinary array sorting. First we create two

    temporary node node *temp1, *temp2 and allocate space for it. Transfer the address of first

    node to temp1 and address of second node to temp2. Now check if temp1->data is greater

    than temp2->data. If yes then exchange the data. Similarly, we perform this checking for all

    the nodes.

    Collapse | Copy Code

    node *temp1; // create a temporary nodetemp1 = (node*)malloc(sizeof(node)); // allocate space for node node *temp2; // create a temporary nodetemp2 = (node*)malloc(sizeof(node)); // allocate space for node int temp = 0; // store temporary data value for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next ){ for( temp2 = temp1->next ; temp2!=NULL ; temp2 = temp2->next ) { if( temp1->data > temp2->data ) { temp = temp1->data; temp1->data = temp2->data; temp2->data = temp; } }}

    Conclusion

    From the above discussions, I hope that everybody understands what linked list is and how we

    can create it. Now we can easily modify linked list according to our program requirement and

    try to use it for some real tasks. Those who still have some confusion about linked list, for them

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    9 of 16 5/1/2014 6:31 PM

  • I will now tell you a story.

    Once upon a time, an old man lived in a small village. The old man was very wise and knew many

    solutions about different problems. He had also special powers so he could talk to genie and spirits,

    and sometimes they granted his wish by using their special powers. Oneday a witch with a broom

    came to talk with him and ask difficult and complex issues about global warming. He was very

    surprised but patiently explained her about green house model and gave her advice about using

    biofuel in her broom. The witch was very rude and greedy but she always liked to preach her

    nobility. So at the time of her departure, she wanted to grant only two wishes. The old man asked

    her why she only granted two wishes. He also reminded her that whenever genie comes he granted

    two wishes. What I am look like" the witch asked angrily, A blank check?" The old man brewed a

    plan. He told her that his first wish was to get a super computer. It is granted", the witch

    announced loudly. Then my second wish is to have another two wishes, the old man said very

    slowly. The witch was shell shocked. "It is also granted, the witch said and left the place very quickly

    with her broom.

    You may ask yourself why the witch was surprised. First, the witch granted two witches. One was

    fulfilled and for the second wish, the old man wanted another two wish. Whats the big idea?

    you can ask me, The witch can also fulfill this wish. Certainly, the witch can grant his wish. But

    what will happen if the old man wants to extend his second wish to another two wish set. So, the

    process will never end unless, the old man want to stop. The idea is same for linked list. First

    you have a node where you can imagine that data is the first wish and node*next is the

    second wish by which you can create second node just like first. This process will continue until

    you put NULL in the *next.

    Figure: It looks like the old man had lots of wish.

    References

    Wikipedia, the free encyclopedia.1.

    Pointer in C, from P. Kanetkar.2.

    License

    This article, along with any associated source code and files, is licensed under The Code Project

    Open License (CPOL)

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    10 of 16 5/1/2014 6:31 PM

  • Nasif M.

    Bangladesh

    No Biography provided

    Search this forum Go

    About the Author

    Article Top

    Comments and Discussions

    You must Sign In to use this message board.

    Profile popups Spacing Relaxed Noise Medium Layout Open All Per page 25 Update

    First Prev Next

    Member 10715040 6-Apr-14 11:03

    how to solve this question

    Receive a list (L1) and a number (m) and to return

    another list (L2) that contains an exact copy of the first (m) elements

    from (L1). If the size of (L1) is less than or equal to (m), copy the

    whole list into (L2). In any case, L1 should remain unchanged

    Sign In View Thread Permalink

    Member 10579024 7-Feb-14 2:45

    Sign In View Thread Permalink

    Member 10379678 4-Nov-13 8:00

    Pretty difficult implementation but good learning over all.

    Sign In View Thread Permalink

    Eltaf Hussain 20-Oct-13 4:12

    The traverse section does not display the linkedlist items but runs an infinite loop.

    Actually it does not find the null or Last linkedlist.

    copy a linked list to another list

    assuming i keep the as deep copy

    Good

    Great work!

    The traverse is run to infinite.

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    11 of 16 5/1/2014 6:31 PM

  • Can you please check what would be the problem.

    The code seems to be correct.

    Adeer

    Sign In View Thread Permalink

    Member 10150181 10-Jul-13 18:07

    #include

    #include

    #include

    struct node

    {

    node *link;

    int info;

    };

    node* create(int n)

    { node *a[10];

    for(int i=0;ilink=NULL;

    node* ptr=start;

    while(ptr!=NULL)

    {

    coutptr>info;

    ptr=ptr>link;

    }

    return start;

    }

    void main()

    {

    }

    n i m getting two errors mainly constant expression required and 2nd 1 is cannot convert

    node* to node*[1], i m unable to get it .... any1's can help???

    Sign In View Thread Permalink 1.00/5 (1 vote)

    vipink1berwal 19-Jun-13 22:04

    awesome

    Sign In View Thread Permalink

    kfsone 2-Jun-13 9:02

    While it's a good explanation on how linked lists operate, it utterly fails at being C++

    (malloc instead of new, no constructors, etc) while having just enough non-list related bits

    of C++ to mean it doesn't help someone using C.

    Sign In View Thread Permalink

    Almo2001 31-Jan-14 9:11

    He's right. It's not C++, really.

    linked list

    My vote of 5

    This is C, not C++

    Re: This is C, not C++

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    12 of 16 5/1/2014 6:31 PM

  • Sign In View Thread Permalink

    bilal78699 8-Mar-13 1:17

    Nice Post

    for more info visit

    http://codesmesh.com/linked-list-and-dynamic-memory-allocation-in-c/[^]

    Sign In View Thread Permalink

    Mohamed Saleh 29-Jan-13 0:58

    man you are simple a "genius"

    Sign In View Thread Permalink

    harish gyanani 23-Dec-12 1:12

    i m using dev c++

    i found two mistakes in this program

    first is stdlib.h is not included for malloc function

    second is

    typedef struct node

    {

    int data; // will store information

    node *next; // the reference to the next node

    }node;

    in above line you forget to write node before semicolon

    Sign In View Thread Permalink

    Steven Karapetyan 25-Nov-12 9:39

    Beautiful sorting method

    Sign In View Thread Permalink

    Ha Dinh 25-Nov-12 9:33

    I'm sorry that it's really bad implementation. I don't think you understand pointer. Here

    are several problems:

    1. You declare: node *head = NULL. If you declare as a local variable, you MUST use

    pointer to pointer for all functions that work at front of linked list.

    2. temp1 = (node*)malloc(sizeof(node)); // allocate space for node

    temp1 = head; //transfer the address of head to temp1.

    TOTALLY WRONG!!! If you assign (not transfer) temp1 to point to heads' pointee (temp1

    = head), you don't have to allocate the pointee (memory).

    3. Later, you free(temp1). It will be the same as free(previous head's pointee). This will

    lead to memory leak because of your allocating temp1.

    4. Your "Delete from back" from back doesn't work when linked list is empty.

    5. And too many problems...

    Linked list, in fact, is just a small and simple implementation within a complex project.

    Keeping the code clean and simple is a MUST. The crucial point of linked list is declaring

    node *head as local or global variable.

    Local variable has several problems such as: pointer to pointer, reversing linked list,

    nice post

    My vote of 5

    2 small errors in main linked list

    program

    My vote of 5

    Bad Implementation

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    13 of 16 5/1/2014 6:31 PM

  • complicated...

    Global variable fixes all the problems above. Below is DeleteAtEnd sample, using local

    variable:

    void DeleteAtEnd(NODE* current)

    {

    NODE* previous;

    /* List is empty */

    if (current->next == NULL)

    {

    free(current->next);

    return;

    }

    while ( current->next != NULL)

    {

    previous = current;

    current = current->next;

    }

    previous->next = NULL;

    free(current->next);

    }

    Sign In View Thread Permalink 5.00/5 (1 vote)

    mojgan.creative 26-Mar-13 21:53

    hi(i should say my english is poor!i try to say what i mean...!)

    i think if some one wrote some thing like this article we should support her/him and

    guide and encourage them to write a better article and dont punish them!

    i really try to write english and say what i want

    i hope u understand

    sorry :(

    Sign In View Thread Permalink

    islam saied 20-Nov-12 21:01

    i want to search in linked list to find item hwo can i make function to find item ???

    Sign In View Thread Permalink

    Nurgazy Nazhimidinov 23-Oct-12 23:50

    which sorting algorithm did you use?

    Sign In View Thread Permalink

    faraz9588 28-Sep-12 23:43

    clearly defined

    Sign In View Thread Permalink

    seripigari 19-Sep-12 6:30

    Re: Bad Implementation

    linked list in c

    sorting

    My vote of 5

    My vote of 5

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    14 of 16 5/1/2014 6:31 PM

  • More clear, impossible

    Sign In View Thread Permalink

    booirror 15-Sep-12 6:30

    8)sort nodes

    node *temp1; // create a temporary node

    temp1 = (node*)malloc(sizeof(node)); // allocate space for node

    node *temp2; // create a temporary node

    temp2 = (node*)malloc(sizeof(node)); // allocate space for node

    I think that it needn't allocate memory. in fact, you are not using it at all!

    right or not?(I am chinese,my english is poor.)

    Sign In View Thread Permalink

    sunilengkr 29-Aug-12 20:58

    it is in simple format and very easy to understand due to diagrams...

    Sign In View Thread Permalink

    theahaiku 21-Aug-12 1:12

    Thank you so much.

    Sign In View Thread Permalink

    smash13 15-Aug-12 7:10

    Why do we need to create a Temp node for deleting from the front?

    Why could we not just do this:

    node * temp = head;

    head = head->next;

    free(temp);

    I tested it, appears to work as it should..

    Sign In View Thread Permalink

    hakz.code 31-Jul-12 0:30

    My doubts

    Sign In View Thread Permalink

    zaphoed 22-May-13 3:02

    Hi hakz.code

    Can you please list your doubts and elaborate on them?

    Sign In View Thread Permalink

    hakz.code 31-Jul-12 0:29

    suggestion or question

    My vote of 5

    Linked

    Delete from front: question

    My vote of 3

    Re: My vote of 3

    Dangling memory

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    15 of 16 5/1/2014 6:31 PM

  • Permalink | Advertise | Privacy | Mobile

    Web01 | 2.8.140430.2 | Last Updated 26 Mar 2008

    Article Copyright 2008 by Nasif M.

    Everything else Copyright CodeProject, 1999-2014

    Terms of Use

    Layout: fixed | fluid

    First of all the story regarding Wishes was amazing

    I have a doubt,when you are deleting the nodes,you are extra memory.

    For example:

    in the following code you have allocated temp.Then assigned address of node to temp.Ok

    now when you free the temp you are freeing the head node's memory.But the memory

    you have allocated for temp before assigning head to it is dangling now.

    node *temp; // create a temporary node

    temp = (node*)malloc(sizeof(node)); // allocate space for node

    temp = head; // transfer the address of 'head' to 'temp'

    head = temp>next; // transfer the address of 'temp>next' to 'head'

    free(temp);

    I was brushing up on linked lists.So please correct me if I am wrong!

    Thanks

    Sign In View Thread Permalink

    Last Visit: 31-Dec-99 18:00 Last Update: 1-May-14 9:11 Refresh 1 2 3 Next

    General News Suggestion Question Bug Answer Joke Rant Admin

    How to create Linked list using C/C++ - CodeProject http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-u...

    16 of 16 5/1/2014 6:31 PM