Link list

This is a discussion on Link list within the C Programming forums, part of the General Programming Boards category; Hey, I'm making a very basic top of the pops project as an assignement. Why here am I getting an ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23

    Link list

    Hey,
    I'm making a very basic top of the pops project as an assignement. Why here am I getting an error?

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    struct node {
    	int playcount;
    	char title[30];
    	char artist[30];
    	struct node * next;
    };
    
    typedef struct node node;
    
    node * head,*save,*current;
    node * getnode();
    
    int insert_end(int x);
    int count();
    void display();
    node * search(int kv);
    
    int main()
    {
    	int ch,n;
    	head = NULL;
    	do
    	{
    			printf("\n1.  Display List");
    			printf("\n2.  Insert the value at End");
    			printf("\n3.  Search the Node of Linked List");	
    		
    			printf("\nEnter Your Choice\n");
    			scanf("%d",&ch);
    		switch (ch)
    		{
    		
    		case 1:
    		display();
    		break;
    		
    		case 2:
    		insert_end(n);
    		break;
    		
    		case 3:
    		printf("\nEnter The Key Value To Be Searched\n");
    		scanf("%d",&n);
    		search(n);
    		break;
    		
    		}
    	}
    	while (ch!=0);
    	return 1;
    }
    
    int insert_end()
    {
    
    	node *temp;
    	
    	char a[30],t[30];
    
    		printf("    Enter Song Title (type ! to exit): ");
    		scanf("%s",&t);
    		if (t[0] == '!')	//If = '!' then user wants to exit so return
    		return 0;
    
    		printf("    Enter Song Artist: ");
    		scanf("%s",&a);
    
    	temp = getnode();
    	
    	temp->next = NULL;
    	if (head == NULL)
    	head = temp;
    	else
    	{
    		current = head;
    		while (current != NULL)
    		{
    			save = current;
    			current = current->next;
    		}
    	save->next = temp;
    	}
    	return 1;                   
    }
    
    node * getnode()
    {
    	node * temp;
    	temp = (node *) malloc(sizeof(node));
    	if (temp == NULL)
    	{
    		printf("\nMemory allocation Failure!\n");
    		//exit(1);
    	}
    	else
    	return(temp);
    }
    
    void display()
    {
    	current = head;
    	if (current == NULL)
    	printf("The List Is Empty!\n");
    	while (current != NULL)
    	{
    		printf("%s%s",current->artist,current->title);
    		current = current->next;
    	}
    		getch();
    }
    
    
    node * search(int kv)
    {
    	current = head;
    	if (head == NULL)
    	{
    		printf("\a\nLIST IS EMPTY");
    		getch();
    	}
    	else
    	{
    		while ( (current != NULL) && (current->playcount != kv) )
    		current = current->next;
    		if (current->playcount == kv)
    		{
    			printf("\nSearch Successfull Key value is found\n");
    			getch();
    			return(current);
    		}
    		else
    			printf("\nSearch Unsuccessfull, Element is not found\n");
    			getch();
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int insert_end(int x);
    does not match
    Code:
    int insert_end()
    (Note that there may be more syntax errors, or logic errors; I just looked for the first one I saw.)

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    current = current->next;
    		if (current->playcount == kv)
    asking for crash?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Use getchar(). Also how are you assigning the song title and artist to your nodes?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    Link list
    Hey,
    I'm making a very basic top of the pops project as an assignement. Why here am I getting an error?
    
    Code:
    
    #include<stdio.h>
    #include<conio.h>
    
    struct node {
    	int playcount;
    	char title[30];
    	char artist[30];
    	struct node * next;
    };
    
    typedef struct node node;
    
    node * head,*save,*current;
    node * getnode();
    
    int insert_end(int x);
    int count();
    void display();
    node * search(int kv);
    
    int main()
    {
    	int ch,n;
    	head = NULL;
    	do
    	{
    			printf("\n1.  Display List");
    			printf("\n2.  Insert the value at End");
    			printf("\n3.  Search the Node of Linked List");	
    		
    			printf("\nEnter Your Choice\n");
    			scanf("%d",&ch);
    		switch (ch)
    		{
    		
    		case 1:
    		display();
    		break;
    		
    		case 2:
    		insert_end(n);
    		break;
    		
    		case 3:
    		printf("\nEnter The Key Value To Be Searched\n");
    		scanf("%d",&n);
    		search(n);
    		break;
    		
    		}
    	}
    	while (ch!=0);
    	return 1;
    }
    
    int insert_end()
    {
    
    	node *temp;
    	
    	char a[30],t[30];
    
    		printf("    Enter Song Title (type ! to exit): ");
    		scanf("%s",&t);
    		if (t[0] == '!')	//If = '!' then user wants to exit so return
    		return 0;
    
    		printf("    Enter Song Artist: ");
    		scanf("%s",&a);
    
    	temp = getnode();
    	
    	temp->next = NULL;
    	if (head == NULL)
    	head = temp;
    	else
    	{
    		current = head;
    		while (current != NULL)
    		{
    			save = current;
    			current = current->next;
    		}
    	save->next = temp;
    	}
    	return 1;                   
    }
    
    node * getnode()
    {
    	node * temp;
    	temp = (node *) malloc(sizeof(node));
    	if (temp == NULL)
    	{
    		printf("\nMemory allocation Failure!\n");
    		//exit(1);
    	}
    	else
    	return(temp);
    }
    
    void display()
    {
    	current = head;
    	if (current == NULL)
    	printf("The List Is Empty!\n");
    	while (current != NULL)
    	{
    		printf("%s%s",current->artist,current->title);
    		current = current->next;
    	}
    		getch();
    }
    
    
    node * search(int kv)
    {
    	current = head;
    	if (head == NULL)
    	{
    		printf("\a\nLIST IS EMPTY");
    		getch();
    	}
    	else
    	{
    		while ( (current != NULL) && (current->playcount != kv) )
    		current = current->next;
    		if (current->playcount == kv)
    		{
    			printf("\nSearch Successfull Key value is found\n");
    			getch();
    			return(current);
    		}
    		else
    			printf("\nSearch Unsuccessfull, Element is not found\n");
    			getch();
    	}
                            return NULL; //????
    }
    Further to vart's comment:
    Perhaps you should return NULL when you haven't found something, rather than just exiting the function with no return value at all.

    Your indentation is horrible.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Also, always check return values when using malloc

    Code:
    temp = (node *) malloc(sizeof(node));      /* this can get you in trouble if not checked */

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    Ireland
    Posts
    23
    ok.. I retried code (couldnt use getchar() ) and I have one problem. When i try play a song it crashes. Anyone able to help?
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <ctype.h>
    
    struct node//creates a struct called node which holds artist name,song title,a pointer to next node and a counter
    {
    	char artist[80];
    	char title[80];
    	struct node*link;
    	int counter;
    };
    
    typedef struct node N;//defines struct node as N
    
    
    N*createcd(N*cd,char name[],char song[],int counter);//prototype for create cd function
    N*search(N*head,char key[]);//prototype for search function
    void playsong(N*node);//prototype for playsong function
    N*createlist(N*head);//prototype for create list function
    void displaylist(N*head);//prototype for display list function
    
    
    void main()
    {
    
    	N*head=NULL;//sets the value of head of the list to 0
    	
    	
    	int choice=0;//control variable for loop
    	char songname[80];//name of song wanted in play song function
    	N*position;//gives position of found song
    
    
    	
    	
    	do{
    	
    		printf("\t\tWelcome to CD Player\n\n");//prints out menu
    		
    		printf("\t1.Create a CD\n");
    		printf("\t2.Play a CD\n");
    		printf("\t3.Display List\n");
    		printf("\t0.Press to exit\n\n");
    		printf("Please enter your choice\n");
    		
    		scanf("&#37;d",&choice);
    		
    		if(choice==1)//this is the create cd section, it creates the cd and then displays the link list
    		{
    			
    			head=createlist(head);
    			displaylist(head);
    		}
    		
    				
    		else if(choice==2)//this is the play song section, it prompts the user for the name of the song they want to play, it finds it in the list and then returns it to the play song function
    		{
    			printf("Please enter the name of the song you want to play\n");
    			scanf("%s",songname);
    			
    			position=search(head,songname);
    			
    			playsong(position);
    			
    		}
    		
    		else if(choice==3)
    		{
    			displaylist(head);
    		}
    		
    				
    		else if(choice!=0)
    		{
    			printf("Invalid Choice, Please try Again\n");
    		}
    	
    	}while(!choice==0);
    		
    	
    }
    
    N*createcd(N*cd,char name[],char song[],int count)
    {
    
    
    	cd=malloc(sizeof(N));
    
    	cd->counter=count;//copies no of plays from the input to the node
    	strcpy(cd->artist,name);//copies artist from input to node
    	strcpy(cd->title,song);//copies song title from input top node
    
    
    	cd->link=NULL;//makes the pointer point to the end of the list
    
    	return cd;//returns the edited node
    
    }
    
    
    N*search(N*head,char key[])//finds the song wanted in the link list
    {
    
    	N * current;
    
    	current=head; 
    
    	//if (current->word  < key) move through links until the next link 
    	//matches  or current_word  > key
    
    	while(current!=NULL)
    		{
    			//compare the keyword with the link list 
    			if  (strcmp(key,current->title)==0)
    			{
    				break;
    			}	
    			
    				current = current -> link;
    				
    			
    		}
    
    	if  (current==head)  //empty list
    	{
    		return NULL;
    	}
    
    	else
    	{ 
    	// returns the position of the node 
    		return current;
    	} 
    }
    
    void playsong(N*node)//plays the song
    {
    	
    	node->counter++;//increments counter
    	
    	
    	
    	system("cls");
    	printf("'%s' by %s\n",node->title,node->artist);//displays name of song
    	printf("Played %d times\n",node->counter);//displays number of times song played
    	
    	
    	
    }
    
    N*createlist(N*head)//this links all the created nodes together
    {
    
     	N*last = head;//initially sets end of the list to head
    	char artist[80];
     	char songtitle[80];
     
     	if (head !=NULL)//if head isnt empty
     	{
      		while(last->link!=NULL)
      		{
       			last=last->link;
     		}
     	}
     	
      	printf("Please enter artist name as one word\n");//prompts user to enter artist name
      	scanf("%s",artist);
      
      
      	printf("Please enter song title as one word\n");//prompts user to enter song name
      	scanf("%s",songtitle);
     
      if(head==NULL)//if link list is empty
      {
       	head=createcd(head,artist,songtitle,0);//create a cd
       	last=head;
      }
      
      else//if link list isnt empty
      {
       last->link=createcd(last->link,artist,songtitle,0);
       last=last->link;//sets the end of the list
      }
     return head;
    }
     
    void displaylist(N*head)
    {
    
     N*current;
     
     
     current=head;
    
     if (current==NULL)//if current is 0 the link list is empty
     {
     	printf("Empty list\n\n");
     }
      
     else//otherwise print out the names of all the songs until current =0
     {
      
        while(current!=NULL)
        {
         	printf("%s by %s.Played %d times\n",current->title,current->artist,current->counter);
         	current=current->link;
        }
     }
     
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first thought is that you are returning NULL and then try to access artist and title from NULL.

    Code:
    	current=head; 
    
    	//if (current->word  < key) move through links until the next link 
    	//matches  or current_word  > key
    
    	while(current!=NULL)
    		{
    			//compare the keyword with the link list 
    			if  (strcmp(key,current->title)==0)
    			{
    				break;
    			}	
    			
    				current = current -> link;
    				
    			
    		}
    
    	if  (current==head)  //empty list
    	{
    		return NULL;
    	}
    Is head "magical" in that it can't contain anything? [Sorry, I haven't actually read the whole code, so I can't say. But to me, it looks like it will return NULL when current == head, which would potentially happen if you search for the first CD in the linked list. Or, if head is "magical", you should skip the first element before you start comparing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 09:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 09:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. 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