Thread: linked list problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    linked list problem

    im making this linked (code above) and i need that when the information (country) is the same as the previous information the linked list update only the count and the information is not stored example

    if there is this input
    mmm 1
    mmm 2
    mmm 3
    output will need to be mmm3

    what shoul i do to make this happen

    Code:
    void insertitem(ListNode **startPtr,booking_type* bookings){
    	int ans=0;
    	
    	
    	ListNode *prevNode =NULL ,*curNode=*startPtr;
    		
    	ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));
    	
    	strcpy(newNode->data, bookings->country); //newNode->data =bookings->country;
    	newNode->nextPtr = NULL;
    	newNode->count = 1;	
    	while ((curNode!=NULL)&&(strcmp(curNode->data,bookings->country)<=0) ) // future : use strcmp 
    	{
    		
    			
    		
    		
    	
       if(strcmp(curNode->data,bookings->country)==0){
    	   
    		newNode->count++;
    		
    	
    	}
    			
    			
    			prevNode=curNode;
    			curNode = prevNode->nextPtr;
    			
    	}
    	
    
    
    	
    	 if(prevNode == NULL)
    		*startPtr=newNode;
    
    
    	 else
    		prevNode->nextPtr = newNode;
    
    
    	newNode->nextPtr = curNode;
    	}

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why are you making a ton of threads for the same problem ?! Laserlight has provided you with explanations on another thread, and every time you can't follow what you are being told you barf another topic, on the exact same problem. STOP IT!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  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
    Indeed - closed.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list problem.
    By csharp100 in forum C Programming
    Replies: 16
    Last Post: 07-23-2010, 05:01 AM
  2. Problem with linked list
    By tallgeese84 in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:51 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked List Problem
    By Daniel in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2003, 06:20 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM