Thread: function linked lists help

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    function linked lists help

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    
    
    #define NEXT 50
    #define PREV 49
    
    using namespace std;
    
    typedef struct node
    {
        int a;
        node *next;
        node *prev;
    }NODE;
    
    
    //Function prototypes
    void display(node *head);
    void addnode(node *current, node *head);
    
    int main()
    {
        NODE *current = NULL;
        NODE *head = NULL;
    
    
        for (int i = 0; i < 1; i++)
    		addnode(current,head);
    
        display (head);
        system("PAUSE");
        return 0;
    }
    
    void addnode(node *current, node *head)
    {
    	node *newnode = new node;
    		cout << "input a value: ";
    		cin >> newnode->a;
    			if (head == NULL)
    				{
    					head = newnode;
    					current = newnode;
    					current->next = NULL;
    					current->prev = NULL;
    				}// if statement
    			else
    				{		
    					newnode->prev = current;
    					current->next = newnode;
    					current = newnode;
    					current->next = NULL;
    				}// else statement
    }
    
    void display(node *head)
    {
       int ch;
       node *show = head;
       cout << show->a << endl;
       while(1)
       {
            ch =getch();
    
    		if (ch == PREV)
    		{
    			if (show->prev != NULL)
    			{
    				show = show->prev;
    				cout << show->a << endl;
    			}
    		}
    		if (ch == NEXT)
    		{
    			if (show->next != NULL)
    			{
    				show = show->next;
    				cout << show->a << endl;
    			}
    		}
       }
    }
    ok, i know for a fact that the display function works correctly. and i know the coding for the actual adding of the linked lists also works, but it only works when i keep it in main. i just tried to add it to another function (the adding part) and it keeps erroring out as soon as i try to display. like i said, if i keep the code for adding the linked list in main, everything works perfectly. so i know its a problem with the code to add. i heard you're suppose to use pointers to pointers, but im not sure how or why you use them.
    please advise
    thanks

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    figured it out !

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Right, to change what head and current actually point to you need to pass their address:

    Code:
    void addnode(node **current, node **head)
    {
    	node *newnode = new node;
    		cout << "input a value: ";
    		cin >> newnode->a;
    			if (*head == NULL)
    				{
    					*head = newnode;
    					*current = newnode;
    					(*current)->next = NULL;
    					(*current)->prev = NULL;
    				}// if statement
    			else
    				{		
    					newnode->prev = *current;
    					(*current)->next = newnode;
    					*current = newnode;
    					(*current)->next = NULL;
    				}// else statement
    }
    in main the syntax would be:

    Code:
    int main()
    {
        NODE *current = NULL;
        NODE *head = NULL;
    
    
        for (int i = 0; i < 1; i++)
    		addnode(&current, &head);
    
        display (head);
        system("PAUSE");
        return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    exactly how i did it

    i may just be a programmer after all!
    hah

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Food for thought...




    Code:
    typedef struct node
    {
        int data;
        node *next;
        node *prev;
    }NODE;
    
    
    
    
    node * newnode(int data)
    {
     node * nd = new node;
       if(nd != NULL)
      {
       nd->prev = 0;
       nd->next = 0;
       nd->data = data;
      }
     return nd;
    }
    
    
    
    node * headnode(node * index)
    {
     while(index && index->prev) index = index->prev;
     return index;
    }
    
    
    
    
    node * tailnode(node * index)
    {
     while(index && index->next) index = index->next;
     return index;
    }
    
    
    
    
    bool insertnode(node * insert, node * prev, node * next)
    {
     if(insert == NULL) return false;
    
     // repoint old neghbors
    
     if(insert->next != NULL) insert->next->prev = insert->prev;
     if(insert->prev != NULL) insert->prev->next = insert->next;
    
     insert->next = next;
     insert->prev = prev;
    
     // repoint new neghbors
    
     if(insert->next != NULL) insert->next->prev = insert;
     if(insert->prev != NULL) insert->prev->next = insert;
    
     return true;
    }
    
    
    
    
    bool pushback(node ** head, int data)
    {
     node * nd = newnode(data);
    
     if(*head == NULL) *head = nd;
    
     else insertnode(nd, tailnode(*head), 0);
    
     return (nd != NULL);
    }
    
    
    
    
    
    bool pushfront(node ** head, int data)
    {
     node * nd = newnode(data);
    
     insertnode(nd, 0, *head);
    
     *head = nd;  // nd is new head
    
     return (nd != NULL);
    }
    
    
    
    
    
    
    void displaylist(node * head)
    {
      while(head)
     {
      cout << head->data << endl;
      head = head->next;
     }
    }
    
    
    
    
    int main()
    {
     NODE * head = 0;
    
     for(int i = 0; i < 10; i++)
      pushback(&head, i);
    
     for(int i = 0; i < 10; i++)
      pushfront(&head, i);
    
     displaylist(head);
    
     cin.get();
    
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM