Closed Thread
Results 1 to 4 of 4

Thread: Doubly linked list

  1. #1
    Apprentice123 is offline Programming Expert
    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0

    Doubly linked list

    I'm trying to make doubly linked list in c, only not quite understand the theory. The code is as follows:

    Code:
    typedef struct list
    {
        int num;
        struct list *pre;
        struct list *next;
    } LIST;
    
    void insert_later(LIST *L, int n)
    {
        LIST *aux;
        
        aux = (LIST *) malloc(sizeof(LIST));
        aux->num = n;
        aux->next = L->next;
        aux->pre= *L;
        L->next = *aux;
    }
    I know that is not correct, what are my mistakes in the theory of lists and in the language?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    julmuri's Avatar
    julmuri is offline Programmer
    Join Date
    Dec 2008
    Location
    127.0.0.1
    Posts
    139
    Rep Power
    12

    Re: Doubly linked list

    This might be better way to do it.

    Code:
    struct List
    {
        int      data;
        List*    head;
        List*    tail;
    
    }; // struct List
    
    
    int append( List* list, int data )
    {
        //
        // is valid list
        if ( 0 == list )
            return -1;
    
        //
        // allocate new node
        List* tail = (List*)malloc( sizeof(List) );
        if ( 0 == tail )
            return -1;
    
        //
        // forward to last valid node
        for ( ; 0 != list->tail; list = list->tail )
        { /* ... */ }
    
        //
        // set tail to newly allocated node
        list->tail = tail;
    
        // set head to old list tail and
        // tail to 0 so we can tell it is last node
        tail->data = data;
        tail->head = list;
        tail->tail = 0;
    
        return 0;
    }
    
    
    int main( int argc, char* argv[] )
    {
        List list = {0};
    
        for ( int it = 1; it < 5; ++it )
            append( &list, it );
    
        for ( List* it = &list; 0 != it->tail; it = it->tail )
            printf( "List::data=%d\n", it->data );
    
        //
        // clean up
        return 0;
    }
    Hope this helps.
    Code:
    std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

  4. #3
    Apprentice123 is offline Programming Expert
    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0

    Re: Doubly linked list

    I would like to create a list structure.

    The first function I tried was to insert value at the end of the list. How do I insert the end of the list?

  5. #4
    Apprentice123 is offline Programming Expert
    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0

    Re: Doubly linked list

    I find this code in internet:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int info;
        struct node *right;
        struct node *left;
    };
    
    typedef struct node *nodeptr;
    
    nodeptr getnode()
    {
        nodeptr p;
        p = (struct node*) malloc(sizeof(struct node));
        return p;
    }
    
    nodeptr initialize(nodeptr S)
    {
        S = NULL;
        return S;
    }
    
    int empty(nodeptr S)
    {
        if(S==NULL)
            return 1;
        else
            return 0;
    }
    
    void insertright(nodeptr p, int num)
    {
        nodeptr q;
        nodeptr temp;
        q = getnode();
        temp = getnode();
        if(p==NULL)
        {
            q->info = num;
            q = p->right;
            q->left = p;
        }
        else 
        {
            temp = p->right;
            p->right = q;
            q->left = p;
            q->right = temp;
            temp->left = q;
        }
    }
    
    void insertleft(nodeptr p, int num)
    {
        nodeptr q;
        nodeptr temp;
        q = getnode();
        temp = getnode();
        if(p==NULL)
        {
            p->info = num;
            q->left = p;
            p->right = q;
        }
        else 
        {
            p->info = num;
            temp = q->right;
            q->right = p;
            q = p->left;
            p->right = temp;
            temp->left = p;
        }
    }
    
    void removenode(nodeptr p)
    {
        nodeptr q;
        q = p;
        p = p->right;
        p->left = NULL;
        free(q);
    }
    
    void display(nodeptr S)
    {
        nodeptr p1;
        p1 = S;
        printf("\n LINKED LIST \n");
        while(p1!=NULL)
        {
            printf("&#37;d \n",p1->info);
            p1 = p1->right;
        }
    }
    
    int main()
    {
        nodeptr mainnode;
        initialize(mainnode);
        insertright(mainnode,10);
        insertright(mainnode,20);
        insertright(mainnode,30);
        insertleft(mainnode,40);
        insertleft(mainnode,50);
        display(mainnode);
        return 0;
    }
    But to execute the program show the message of the windows that the memory can not be read

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 06-28-2010, 08:45 PM
  2. help with Linked List
    By M0SS_99 in forum C and C++
    Replies: 3
    Last Post: 01-08-2010, 03:18 PM
  3. Simple Doubly Linked List in C
    By tate in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-07-2010, 10:37 PM
  4. Linked list
    By BINNY88 in forum C and C++
    Replies: 5
    Last Post: 10-03-2008, 06:04 AM
  5. Linked list
    By borny86 in forum Pascal and Delphi
    Replies: 9
    Last Post: 12-06-2007, 09:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts