Thread: with linked lists and structs

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    with linked lists and structs

    let's say i have these
    Code:
    struct stra{struct  stra next;int value;};
    struct strb{struct stra head; struct stra tail;int size;};
    and lets say they gave me an struct stra positionand i want to drive my main list of type struct strb to go to that location how can i do that?
    is that way correct?
    because if i move the x wouldn't change my list position? as they are pointers? im really confused

    for(struct stra x=l->head;x!=position;x=x->next);

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by cable View Post
    Code:
    let's say i have these
    struct stra{struct  stra next;int value;};
    first off that is only a structure, what you want is structure pointer
    unless your doing your entire program without functions
    Last edited by camel-man; 10-10-2011 at 11:44 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you throw in some pointers, it seems OK.
    Code:
    struct stra{struct *stra next;int value;};
    struct strb{struct *stra head; struct stra *tail;int size;};
    
    for(struct stra *x=l->head;x!=position;x=x->next);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    thanks..
    i have one more question about linked lists
    lets say i have a list with head and a (tail at the end) (and lets say its like a circle) the last item which is the tail points at the beginning
    if i have a node_t location(which points at the part of list i want to remove) (node_t is the pointers=next,head,tail) which one is the best algorithm to do it? (you know , i have to bind the previous with the next of the current ) and then free(current)
    i have coded one but is really ugly ,big and used a lot of variables
    Code:
    node_s *pointed_var; ( the pointed variable which points at the part i want to remove)
       list_d list; (the main list)
    list has node_s* tail and *head )
    Last edited by cable; 10-10-2011 at 05:51 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    i have write this code but "free()" seems to not working

    start_node its the node which search should be start for deleting nodes(if you don't find meaning with it, me either but i have to use it
    Code:
    node_t * locate_node(list_t * l, node_t *start_node, int pos){
       int i;
    	node_t *pointer=start_node;
    	  for(i=1;i<pos;i++)
    	     pointer=pointer->next;	
    	return pointer;
    
    }
    void node_delete(list_t *l, node_t* start_node, int pos) {
    	node_t *cur;
    	node_t *pre=NULL;
    		for(cur=l->head;cur!=start_node;pre=cur,cur=cur->next);
    		  if(pre==NULL)
    		    pre=l->tail;
    	node_t *pointer=locate_node(l,start_node,pos);
    		pre->next=pointer->next;
    		free(pointer);	
    		(l->size)--;
    		if(l->d)
    		   print_list(l);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  3. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  4. Replies: 3
    Last Post: 01-22-2002, 12:22 AM
  5. file i/o-structs,linked lists
    By new2c in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2001, 11:31 PM