Linked List Need Help Urgent

This is a discussion on Linked List Need Help Urgent within the C Programming forums, part of the General Programming Boards category; Need urgent HELP for the coding below. Wonder why all records added cannot be displayed, after entering the record when ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    Question Linked List Need Help Urgent

    Need urgent HELP for the coding below. Wonder why all records added cannot be displayed, after entering the record when trying to list them out, the list still appeared to be EMPTY.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    
    
    struct album
    {
    	int rec;
    	int AlbumNo;
    	char AlbumName[50];
        char ArtistName[50];
        int Year;
    	int TotalTrack;
    	char TrackName[50][50], TrackDuration[50][50];
        int trackTimeM[60];
    	int trackTimeS[60];
        struct album *next;
    };
    
    typedef struct album node;
    
    void AddAlbum();
    void AddAlbum2(node *p);
    void ListAlbum();
    void ListAlbum2(node *p);
    void ModifyAlbum();
    void ModifyAlbum2(node *p);
    void RemoveAlbum();
    int Compare(node *p,int);
    
    void main()
    {
    	int exit=1,option=0;
    
    	node *head;
    	head = NULL;
    	  do
    		{
     
    		printf("\n===================================\n");
    		printf("\nWelcome to the Music Storage System\n");
    		printf("\n===================================\n\n\n");
    		printf("1-- Add New Album Record\n");
    		printf("2-- Display All Album Records\n");
    		printf("3-- Modify Album Records\n");
    		printf("4-- Remove Existing Album Records\n");
    		printf("5-- Exit\n\n");
    		printf("Please select your option\n\n");
    
    			scanf("%d",&option);
    			switch(option)
    				{
    				  case 1 :AddAlbum();
    				  case 2 :ListAlbum();
    				  case 3 :ModifyAlbum();
    				  case 4 :RemoveAlbum();
    				  case 5 :
    					  {
    						 exit=0;
    						 break;								  				 
    					  }
    				  default :
    					  {
    						printf("\n\aPlease enter option between 1 - 5.\n");
    						break;
    						main();
    					  }
    				}
    
    		}while(exit==1);
    
    	getch();
    }
    
    
    void AddAlbum()
    {
    	int artist;
    	node *head;
    	head = NULL;
    					do 
    						{
    							printf("\nEnter the Type of Album:\n");
    							printf("1-- Single Artist\n");
    							printf("2-- Multiple Artists\n\n");
    							scanf("\n%d",&artist); fflush(stdin);
    							switch (artist)
    									{
    								case 1: { 
    
    							  if(head==NULL)
    								{
    									head=(node *)malloc(sizeof (node));
    									AddAlbum2(head);
    									head->next=NULL;
    								}
    							  else
    								{
    									node *temp;
    									temp=head;
    									while(temp->next!=NULL)
    										{
    										   temp=temp->next;
    										}
    									temp->next=(node *)malloc(sizeof (node));
    									AddAlbum2(temp->next);
    									temp->next->next=NULL;
    
    								}
    									break;	}
    									
    								case 2: printf("\n\aThis system only supports albums created for single artists.\n");AddAlbum();
    									}
    						}while (artist !=2);		
    
    
    		}
    
    void AddAlbum2(node *p)
    {
    	static int  rec = 0;
    
    	p->AlbumNo = ++ rec;
    	
    
    
    	printf("\n\nEnter album's name : \t");
    	gets(p->AlbumName);fflush(stdin);
    
    	printf("Enter artist's name : \t");
    	gets(p->ArtistName);fflush(stdin);
    	
    	printf("Enter album's released year : \t");
    	scanf("%d",&p->Year);fflush(stdin);
    	
    
    	while (p->Year <1900 || p->Year > 2004) 
    		{
    	printf("Enter album's released year : ");
    	scanf("%d",&p->Year);fflush(stdin);
    		}
    
    	while ( p->TotalTrack < 1 || p->TotalTrack > 20)
    		{
    	printf("Enter total number of tracks in the album : \t");
    	scanf("%d",&p->TotalTrack);fflush(stdin);
    		}	
    		
    	for ( int x=0; x<p->TotalTrack; x++ ) 
    		{
    			printf("Enter track title : \t");
    			gets(p->TrackName[x]);fflush(stdin);
    
    			printf("Enter track duration (mm:ss): \t");
    			gets(p->TrackDuration[x]);fflush(stdin);
    
    	    }
    		
    	main();
    }
    
    void ListAlbum()
    {
    
    	int list=0;
    	node *head,*temp;
    	head = NULL;
    
    					
    					    list = 0;
    						 temp=head;
    						 
    						 while(temp != NULL)
    							{
    							   list = 1;
    							   ListAlbum2(temp);
    							   temp=temp->next;
    							}
    
    				         if (list == 0)
    							{printf("\n\aThere is no record existing record.\n");}
    
    						 main();
    					  
    
    }
    
    void ListAlbum2(node *p)
    {
    	int x;
    	printf("\nRecord Number: \t%d ",p->AlbumNo);
    	printf("\nAlbum Name: \t%s ",p->AlbumName);
    	printf("\nArtist Name: \t%s ",p->ArtistName);
    	printf("\nTotal %d tracks recorded in year %d.",p->TotalTrack, p->Year);
    
    	for (x=0;x<p->TotalTrack;x++)
    		{
    			printf( "\nTrack %d : %s \t\tDuration: %s\n\n", x+1, &p->TrackName[x], p->TrackDuration[x]);
    		}
    
    }
    
    
    void ModifyAlbum()
    {
    	int num,modify=0;
    
    	node *head,*temp;
    	head = NULL;
    						 modify = 0;
    						 temp=head;
    						 printf("\nPlease enter the record number: ");
    						 scanf("%d",&num);fflush(stdin);
    						 
    						 while(temp != NULL)
    							{
    							  if(Compare(temp,num))
    									{
    									    modify = 1;
    										ModifyAlbum2(temp);
    									}
    							  temp=temp->next;
    							}
    
    						 if (modify == 0)
    							{printf("\n\aThere is no record existing record.\n");}
    						 main();
    						    		 
    					  }
    
    
    void ModifyAlbum2(node *p)
    {
    	int x,add,edit,option;
    
    	printf("\n1-- Edit album's name");
    	printf("\n2-- Edit artist's name");
    	printf("\n3-- Edit album released year");
    	printf("\n4-- Add additional track/s to album");
    
    
    	printf("\n\nPlease select your option : ");
    	scanf("%s",&option);fflush(stdin);
    
    	switch (option)
    	{
    		case 1 :
    			{	
    				printf("\nEnter album's name : \t");
    				gets(p->AlbumName);fflush(stdin);
    				printf("\nAlbum's name changed.");
    				break;
    			}
    				
    		case 2 :
    			{
    				printf("\nEnter artist's name : \t");
    				gets(p->ArtistName);fflush(stdin);
    				printf("\nArtist's name changed");
    				break;
    			}
    
    		case 3 :
    			{	
    				while (p->Year <1900 || p->Year > 2004) 
    				{
    				printf("Enter album's released year : \t");
    				scanf("%d",&p->Year);fflush(stdin);
    				}
    				printf("\nAlbum's released year changed.");
    				break;
    			}
    		
    		case 4 :
    			{
    
    				while ( (p->TotalTrack + add) < 18)
    					{
    						printf("Enter the total number of ADDITIONAL track/s : \t");
    						scanf("%d",&add);fflush(stdin);
    					}	
    				
    				p->TotalTrack = p->TotalTrack + add;
    
    				for ( x=1 ; x<=add; x++ ) 
    					{
    					
    						printf("\nEnter track title : \t");
    						gets(p->TrackName[p->TotalTrack+x]);fflush(stdin);
    						printf("Enter track duration (mm:ss): \t\n");
    						gets(p->TrackDuration[p->TotalTrack+x]);fflush(stdin);
    							
    					}
    				printf("\nThe addtional tracks is/are successfully added.");
    				break;
    			}
    
    		default : 
    			{
    				printf("\n\aPlease enter option between 1 - 5.\n");
    				break;
    			}
    
    
    	}
    }
    
    void RemoveAlbum()
    					  {
    
    	int num,remove=0;
    
    	node *head,*temp,*current,*previous;
    	head = NULL;
    
    						 remove = 0;
    						 temp=head;
    							printf("\nPlease enter the record number : ");
    							scanf("%d",&num);fflush(stdin);
    
    							if(Compare(head,num))
    								{
    									remove=1;
    									temp=head;
    									head=head->next;
    									free(temp);
    								}
    							else
    								{
    									previous=head;
    									current = head->next;
    									
    									while (current !=NULL && current->AlbumNo != num)
    										{
    											previous = current;
    											current = current->next;
    										}
    
    									if (current != NULL)
    										{
    											remove=1;
    											temp = current;
    											previous->next = current->next;
    											free(temp);
    										}
    								}
    
    							
    							 if (remove == 0)
    								{printf("\n\aThere is no existing record.");}
    							 else
    								{printf("\n\aRecord removed.");}
    												
    					        main();	
    						    								 			
    					  }
    
    
    int Compare(node *p,int got)
    {
    	if(p->AlbumNo==got)
    		{return 1;}
    	else
    		{return 0;}
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Your problem maybe here...

    Code:
    void ListAlbum()
    {
             int list=0;
             node *head,*temp;
    
             head = NULL;  //you set head to NULL
             list = 0;  // list is already been initialised
             temp=head;    // you then set temp to NULL
    
             // temp equals NULL so the condition is met,
             // loop is never processed
             while(temp != NULL)
             {
                         list = 1;
                         ListAlbum2(temp);
                          temp=temp->next;
              }
    
               // list equals 0 so message is shown
               if (list == 0)
               {
                         printf("\n\aThere is no record existing record.\n");
                }
    
               // donot call main BAD, just end function normally 
               // nothing required
               main();
    }
    Last edited by bigtamscot; 08-17-2004 at 12:06 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Please don't say "urgent". While your problem may be urgent to you, it's not to us; only your fault if you have a project due tomorrow.

    Try to keep your code as standard as possible, so those trying to aid you can compile your code. Writing in compliance with standards is also good coding form. In here, this inculdes removing the <conio.h> and getch(). maybe fix those tab stops too

    Don't use gets(), it's unsafe http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When using scanf(), check for failure, more about this here
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    fflush(stdin) is also nonstandard
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Always check to make sure malloc () didn't fail.
    void main() is incorrect
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    hello, internet!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Did I mention formatting in another thread?
    Probably.
    This is just as bad.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    The OP's code - reformatted (thanks to indent -kr -ts4 -nut hello.c)
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    
    
    struct album {
        int rec;
        int AlbumNo;
        char AlbumName[50];
        char ArtistName[50];
        int Year;
        int TotalTrack;
        char TrackName[50][50], TrackDuration[50][50];
        int trackTimeM[60];
        int trackTimeS[60];
        struct album *next;
    };
    
    typedef struct album node;
    
    void AddAlbum();
    void AddAlbum2(node * p);
    void ListAlbum();
    void ListAlbum2(node * p);
    void ModifyAlbum();
    void ModifyAlbum2(node * p);
    void RemoveAlbum();
    int Compare(node * p, int);
    
    void main()
    {
        int exit = 1, option = 0;
    
        node *head;
        head = NULL;
        do {
    
            printf("\n===================================\n");
            printf("\nWelcome to the Music Storage System\n");
            printf("\n===================================\n\n\n");
            printf("1-- Add New Album Record\n");
            printf("2-- Display All Album Records\n");
            printf("3-- Modify Album Records\n");
            printf("4-- Remove Existing Album Records\n");
            printf("5-- Exit\n\n");
            printf("Please select your option\n\n");
    
            scanf("%d", &option);
            switch (option) {
            case 1:
                AddAlbum();
            case 2:
                ListAlbum();
            case 3:
                ModifyAlbum();
            case 4:
                RemoveAlbum();
            case 5:
                {
                    exit = 0;
                    break;
                }
            default:
                {
                    printf("\n\aPlease enter option between 1 - 5.\n");
                    break;
                    main();
                }
            }
    
        } while (exit == 1);
    
        getch();
    }
    
    
    void AddAlbum()
    {
        int artist;
        node *head;
        head = NULL;
        do {
            printf("\nEnter the Type of Album:\n");
            printf("1-- Single Artist\n");
            printf("2-- Multiple Artists\n\n");
            scanf("\n%d", &artist);
            fflush(stdin);
            switch (artist) {
            case 1:{
    
                    if (head == NULL) {
                        head = (node *) malloc(sizeof(node));
                        AddAlbum2(head);
                        head->next = NULL;
                    } else {
                        node *temp;
                        temp = head;
                        while (temp->next != NULL) {
                            temp = temp->next;
                        }
                        temp->next = (node *) malloc(sizeof(node));
                        AddAlbum2(temp->next);
                        temp->next->next = NULL;
    
                    }
                    break;
                }
    
            case 2:
                printf
                    ("\n\aThis system only supports albums created for single artists.\n");
                AddAlbum();
            }
        } while (artist != 2);
    
    
    }
    
    void AddAlbum2(node * p)
    {
        static int rec = 0;
    
        p->AlbumNo = ++rec;
    
    
    
        printf("\n\nEnter album's name : \t");
        gets(p->AlbumName);
        fflush(stdin);
    
        printf("Enter artist's name : \t");
        gets(p->ArtistName);
        fflush(stdin);
    
        printf("Enter album's released year : \t");
        scanf("%d", &p->Year);
        fflush(stdin);
    
    
        while (p->Year < 1900 || p->Year > 2004) {
            printf("Enter album's released year : ");
            scanf("%d", &p->Year);
            fflush(stdin);
        }
    
        while (p->TotalTrack < 1 || p->TotalTrack > 20) {
            printf("Enter total number of tracks in the album : \t");
            scanf("%d", &p->TotalTrack);
            fflush(stdin);
        }
    
        for (int x = 0; x < p->TotalTrack; x++) {
            printf("Enter track title : \t");
            gets(p->TrackName[x]);
            fflush(stdin);
    
            printf("Enter track duration (mm:ss): \t");
            gets(p->TrackDuration[x]);
            fflush(stdin);
    
        }
    
        main();
    }
    
    void ListAlbum()
    {
    
        int list = 0;
        node *head, *temp;
        head = NULL;
    
    
        list = 0;
        temp = head;
    
        while (temp != NULL) {
            list = 1;
            ListAlbum2(temp);
            temp = temp->next;
        }
    
        if (list == 0) {
            printf("\n\aThere is no record existing record.\n");
        }
    
        main();
    
    
    }
    
    void ListAlbum2(node * p)
    {
        int x;
        printf("\nRecord Number: \t%d ", p->AlbumNo);
        printf("\nAlbum Name: \t%s ", p->AlbumName);
        printf("\nArtist Name: \t%s ", p->ArtistName);
        printf("\nTotal %d tracks recorded in year %d.", p->TotalTrack,
               p->Year);
    
        for (x = 0; x < p->TotalTrack; x++) {
            printf("\nTrack %d : %s \t\tDuration: %s\n\n", x + 1,
                   &p->TrackName[x], p->TrackDuration[x]);
        }
    
    }
    
    
    void ModifyAlbum()
    {
        int num, modify = 0;
    
        node *head, *temp;
        head = NULL;
        modify = 0;
        temp = head;
        printf("\nPlease enter the record number: ");
        scanf("%d", &num);
        fflush(stdin);
    
        while (temp != NULL) {
            if (Compare(temp, num)) {
                modify = 1;
                ModifyAlbum2(temp);
            }
            temp = temp->next;
        }
    
        if (modify == 0) {
            printf("\n\aThere is no record existing record.\n");
        }
        main();
    
    }
    
    
    void ModifyAlbum2(node * p)
    {
        int x, add, edit, option;
    
        printf("\n1-- Edit album's name");
        printf("\n2-- Edit artist's name");
        printf("\n3-- Edit album released year");
        printf("\n4-- Add additional track/s to album");
    
    
        printf("\n\nPlease select your option : ");
        scanf("%s", &option);
        fflush(stdin);
    
        switch (option) {
        case 1:
            {
                printf("\nEnter album's name : \t");
                gets(p->AlbumName);
                fflush(stdin);
                printf("\nAlbum's name changed.");
                break;
            }
    
        case 2:
            {
                printf("\nEnter artist's name : \t");
                gets(p->ArtistName);
                fflush(stdin);
                printf("\nArtist's name changed");
                break;
            }
    
        case 3:
            {
                while (p->Year < 1900 || p->Year > 2004) {
                    printf("Enter album's released year : \t");
                    scanf("%d", &p->Year);
                    fflush(stdin);
                }
                printf("\nAlbum's released year changed.");
                break;
            }
    
        case 4:
            {
    
                while ((p->TotalTrack + add) < 18) {
                    printf
                        ("Enter the total number of ADDITIONAL track/s : \t");
                    scanf("%d", &add);
                    fflush(stdin);
                }
    
                p->TotalTrack = p->TotalTrack + add;
    
                for (x = 1; x <= add; x++) {
    
                    printf("\nEnter track title : \t");
                    gets(p->TrackName[p->TotalTrack + x]);
                    fflush(stdin);
                    printf("Enter track duration (mm:ss): \t\n");
                    gets(p->TrackDuration[p->TotalTrack + x]);
                    fflush(stdin);
    
                }
                printf("\nThe addtional tracks is/are successfully added.");
                break;
            }
    
        default:
            {
                printf("\n\aPlease enter option between 1 - 5.\n");
                break;
            }
    
    
        }
    }
    
    void RemoveAlbum()
    {
    
        int num, remove = 0;
    
        node *head, *temp, *current, *previous;
        head = NULL;
    
        remove = 0;
        temp = head;
        printf("\nPlease enter the record number : ");
        scanf("%d", &num);
        fflush(stdin);
    
        if (Compare(head, num)) {
            remove = 1;
            temp = head;
            head = head->next;
            free(temp);
        } else {
            previous = head;
            current = head->next;
    
            while (current != NULL && current->AlbumNo != num) {
                previous = current;
                current = current->next;
            }
    
            if (current != NULL) {
                remove = 1;
                temp = current;
                previous->next = current->next;
                free(temp);
            }
        }
    
    
        if (remove == 0) {
            printf("\n\aThere is no existing record.");
        } else {
            printf("\n\aRecord removed.");
        }
    
        main();
    
    }
    
    
    int Compare(node * p, int got)
    {
        if (p->AlbumNo == got) {
            return 1;
        } else {
            return 0;
        }
    
    }
    TIP for beginners.
    DO NOT use TAB characters for formatting code. Every single editor / printer / web browser / whatever will treat them differently, so its a guarantee that it will look a mess somewhere.
    So please figure out how to make your editor "use spaces for tabs".
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Maybe they could solve the problem on the server end by filtering what is bewtween code tags and automatigically format for the post? If not at least it would be a cool project for some body to work on.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need urgent help in linked list
    By Cassandra in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 07:47 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  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