why it keeps the change??

This is a discussion on why it keeps the change?? within the C Programming forums, part of the General Programming Boards category; in the end i have this line Code: head = newhead; head is not double astrix why it saves the ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why it keeps the change??

    in the end i have this line
    Code:
    head = newhead;
    head is not double astrix
    why it saves the change.
    it only saves changes for *head members
    ??

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    typedef struct node{
    	int value;
    	struct node *next;
    }Node;
    
    void what1(Node** p, int num){
    	Node *elt;
    	elt = (Node*)malloc (sizeof(Node));
    	elt -> next = *p;
    	elt ->value = num;
    	*p = elt;
    }
    
    Node* what2(Node *head){
    	Node *temp, *prev, *next, *prevmax, *newhead;	
    	while(head->next){
    		prevmax = prev = next = NULL;
    		for(temp=head; temp; temp=temp->next){
    			printf("%d ", temp->value);
    			next = temp -> next;
    			if((!prev || prev->value < temp->value) && 
    (!next || temp->value > next->value)){
    				if(! prevmax )
    					newhead = temp;
    				else
    					prevmax->next= temp;
    				prevmax = temp;
    			}
    			if(prev && prev!=prevmax)
    				free(prev);
    			prev = temp;
    		}
    		prevmax -> next = NULL;
    		head = newhead;
    		putchar('\n');
    	}
    	return head;
    }
    void main (){ 
    	int i, a[]={ 2, 4, 1};
    	Node* list=NULL;
    	for (i = 0; i < 3; i++)
    		what1(&list, a[i]);
    	list = what2(list);
    	printf("final value: %d ", list->value);
    	free(list);
    }
    Last edited by transgalactic2; 04-13-2009 at 01:49 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's a circular loop. I'll leave the rest to you, simply because it would be interesting to see you actually do something for yourself here for a change.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type casting?
    By greatonesv in forum C Programming
    Replies: 12
    Last Post: 10-22-2008, 08:21 PM
  2. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 08:20 AM
  3. Change Value in an array
    By beginner999 in forum C Programming
    Replies: 3
    Last Post: 01-18-2003, 06:16 AM
  4. Replies: 2
    Last Post: 11-08-2002, 02:22 AM
  5. Ask about how to change from int to unsigned char
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2002, 01:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21