Thread: xor linked list

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    thanks...that was an eye opener it took me some time but i figured out that i forgot to set head and some of the others to what was made!!!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    okay i got the insert function working, i got the delete function working correctly, but i am having problems with the list function...here is what my program looks like with just dumby variables as the data and not rndm()!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_NUMBERS 100
    
    typedef struct _node{
            int data;
            unsigned long link;
    } node;//end node
    
    node *head, *tail;
    
    void  insert(int insertNum){
    
            node *nekw, *tmp, *next, *prv, *cur;
            nekw = malloc(sizeof(node));
            cur = head;
            prv = NULL;
            nekw->data = insertNum;
    
            if(cur == NULL){
                    head = nekw;
                    cur = head;
                    next = tail;
            }//end if statement.
    
            else if(cur->data > nekw->data){
                    tmp = cur;
                    cur = nekw;
                    cur->link = tmp->link;
                    next = (node*)(cur->link ^ (unsigned long)prv);
                    if(next == NULL){
                            next = tmp;
                            tail = next;
                    }//end if statement
                    else{
                            tmp->link = (unsigned long)cur ^ (unsigned long)next;
                            cur->link = (unsigned long)prv ^ (unsigned long)tmp;
                            nekw = (node *)(next->link ^ (unsigned long) cur);
                            next->link = (unsigned long)nekw ^ (unsigned long)tmp;
                            cur = tmp;
                    }//end else statement
            }//end else if statement
            else{
                    while((cur != NULL) && (cur->data < nekw->data)){
                            next = (node *)(cur->link ^ (unsigned long)prv);
                            prv = cur;
                            cur = next;
                    }//end while loop
                    if(next == NULL){
                            cur = nekw;
                            tail = cur;
                    }//end if statement
                    else{
                            nekw->link = (unsigned long) cur ^(unsigned long) next;
                            cur->link = (unsigned long) nekw ^ (unsigned long) prv;
                            tmp = (node *)(next->link ^ (unsigned long)cur);
                            next->link = (unsigned long) nekw ^ (unsigned long) tmp;
                            cur = nekw;
                    }//end else
            }//end else statement
    }//end insert.
    
    void delete(int deleteNum){
            node *cur, *prv, *next;
            cur = head;
            prv = NULL;
            while(cur != NULL && (cur->data != deleteNum)){
                    prv =(node *)(cur->link ^ (unsigned long)next);
                    prv = cur;
                    cur = next;
            }//end while loop
            if(cur != NULL && (cur->data == deleteNum)){
                    prv->link = (unsigned long) prv ^ (unsigned long) cur;
                    next->link = (unsigned long) cur^ (unsigned long) next;
                    free(cur);
            }//end if statement.
    }//end delete.
    
     void list(){ //int option
            int cnt;
            node *cur, *prv, *next, *tmp;
            tmp = malloc(sizeof(node));
            cur = head; //here is where i have a breakpoint for gdb!!!
            prv = NULL;
            printf("This is the list in acending order: \n");
            while(cnt < MAX_NUMBERS){
                       printf("&#37;d", cur->data);
                       cnt++;
                       next = (node *)(cur->link ^ (unsigned long)prv);
                       prv = cur;
                       cur = next;
                       printf("  %d", cur->data);//this is where it will error because cur = NULL
                       if(cnt % 10 == 0){
                                 printf("\n");
                       }//end if statement.
            }//end while loop
    
    }//end list.
    
    int main(){
            head = NULL;
            tail = head;
            static int cnt, numDelete;
    
            for(cnt=1; cnt < MAX_NUMBERS; cnt++){
                    insert(cnt);
            }//end for loop.
    
            list();
    
            numDelete = 99;
            delete(numDelete);
    
            list();
    
            return 0;
    }//end main.
    Last edited by adramalech; 10-08-2008 at 09:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM