linked list question

This is a discussion on linked list question within the C Programming forums, part of the General Programming Boards category; First off let me say this is not Hw im trying to remember how linked lists work etc for my ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    19

    linked list question

    First off let me say this is not Hw im trying to remember how linked lists work etc for my upcoming fall class: However what im trying to do is traverse a linear linked list thats sorted and when i find the node im looking for to delete it. Its been a while since ive messed with data structures so im trying to refresh myself. Please see the code i have below and tell me why only 6,5,4,3,2,1 prints out. If you could correct it in code I would greatly appreciate it.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct list_el {
    int val;
    struct list_el * next;
    };
    
    typedef struct list_el item;
    
    int main() {
    item * curr, * head, * temp;
    int i;
    
    head = NULL;
    
    for(i=1;i<=10;i++) {
    curr = malloc(sizeof(item));
    curr->val = i;
    curr->next = head;
    head = curr;
    }
    
    curr = head;
    int zz,number=10;
    
    for(zz=1;zz<=10;zz++)
    {
    
    
    if(curr->val==5)
    { 
                    
      temp->next = curr->next;
    //printf("You have found the number 5 so im gonna delete it\n");
    
    }
    
    
    else if(curr->val!=5)
    {
         
    printf("current possition = %d\n", number);
    number--;
    
    temp = curr;
    curr = curr->next;
    }
    
    
    }
    
    curr=temp;
    
    
    while(curr) {
    printf("%d\n", curr->val);
    curr = curr->next ;
    }
    
    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Does your code compile ????????????? It does not.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    yes it compiles.

    i just re-copied it

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct list_el {
    int val;
    struct list_el * next;
    };
    
    typedef struct list_el item;
    
    int main() {
    item * curr, * head, * temp;
    int i;
    
    head = NULL;
    
    for(i=1;i<=10;i++) {
    curr = malloc(sizeof(item));
    curr->val = i;
    curr->next = head;
    head = curr;
    }
    
    curr = head;
    int zz,number=10;
    
    for(zz=1;zz<=10;zz++)
    {
    
    
    if(curr->val==5)
    { 
                    head->next = temp;
      temp->next = curr->next;
    //printf("You have found the number 5 so im gonna delete it\n");
    
    }
    
    
    else if(curr->val!=5)
    {
         
    printf("current possition = %d\n", number);
    number--;
    
    temp = curr;
    curr = curr->next;
    }
    
    
    }
    
    curr=temp;
    
    
    while(curr) {
    printf("%d\n", curr->val);
    curr = curr->next ;
    }
    
    system("PAUSE");
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    you can print out this code

    Code:
    for(i=1;i<=10;i++) {
    curr = malloc(sizeof(item));
    curr->val = i;
    curr->next = head;
    head = curr;
    }
    u got wrong , u reverse the list !

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    this code is ok
    try it...

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct list_el {
    int val;
    struct list_el * next;
    };
    
    typedef struct list_el item;
    
    int main() {
    item * curr, * head, * temp;
    int i;
    
    head = NULL;
    
    for(i=1;i<=10 ;i++) {
        
    curr = malloc(sizeof(item));
    
    // this is sure for newest Node
    curr->val = i;
    curr->next = NULL;
    
    if(i==1){
    head = curr; // head only sets 1 time
    temp = curr;
    }
    
    // moving...
    
    temp->next = curr;
    temp = curr;
    
     
    
    }
    
    
    
    while(head) {
        printf("%d\n", head->val);
        head = head->next ;
    }
    
    
    
    
        system("PAUSE");
        return 0;
    }

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    My version. It's easier for me to think forward than backwards, so I redid it a bit.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct list_el {
    	int val;
    	struct list_el * next;
    };
    
    typedef struct list_el item;
    
    int main() {
    	item * curr, * head, * prev ;
    	int i;
    	int to_delete = 5 ; 
    
    	head = NULL;
    	curr = NULL ; 
    	prev = NULL ; 
    	
    	for ( i=0 ; i<10 ; i++) {
    		prev = curr ; 
    		curr = malloc(sizeof(item));
    		curr->val  = i;
    		curr->next = 0 ; 
    		if (i==0) head = curr ;  		
    		else prev->next = curr ; 
    	}
    	
    	curr = head ; // reset to top of linked list 
    	prev = NULL ; 
    	while (curr) {
    		if (curr->val == to_delete) { 
    			if (prev) prev->next = curr->next ; // fix forward chain 
    			else head = curr->next ;
    			free(curr) ; 
    			curr = NULL ; 
    		}
    		else { 
    			prev = curr ; 
    			curr = curr->next ; 
    		}
    	}
    		
    	/*	ignore this - just an exercise in obfuscation.... 
    	while (curr) {
    		curr->val == to_delete ? ( { prev != NULL ? (prev->next = curr->next) : 
    		( head = curr->next ); 	free(curr) ; curr = NULL ; } ) : 
    		( { prev = curr ; curr = curr->next ; }) ;
    	}
    	*/ 	
    
    	curr = head ;  // reset to top of list again 
    	while(curr) {
    		printf("%d\n", curr->val);
    		curr = curr->next ;
    	}
    	return 0;
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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