Thread: Problem with the linked list

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    Problem with the linked list

    hi

    I want some help

    when I run the following code, it runs sucessfully for the first time when I add an account...........but when I try to add another it gives me an error "this program has performed an illegal operation & will be shut down"

    when I ran the program for the first time it executed sucessfully.............it even added more than one record to linked list ............but when I tried to run it again it's giving me the above error


    here's the code



    /*understanding more about linked list*/
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>

    void AddNewAccount(void);
    void listall(void);

    struct account
    {
    int number;
    char name[25];
    float balance;
    struct account *link;
    };

    struct account *first,*current,*new1;
    int anum=0;

    int main()
    {
    char ch;

    first=(struct account *)NULL;
    current=(struct account *)NULL;
    new1=(struct account *)NULL;

    do
    {
    printf("A-add new account\n");
    printf("L-list all the customers\n");
    printf("Q-quit the program\n");
    printf("\nEnter Your Choice : ");
    ch=toupper(getch());

    switch(ch)
    {
    case 'A':
    puts("Add New Account : ");
    AddNewAccount();
    break;

    case 'L':
    listall();
    case 'Q':
    exit(0);

    default:
    break;

    }
    }
    while(ch!='Q');

    getche();
    return 0;
    }

    void AddNewAccount(void)
    {
    char buffer[64];

    new1=(struct account *)malloc(sizeof(struct account));

    /*check to see if this is the fist record,if so initialize all the
    pointers to this, first structure in the database*/

    if(first==(struct account *)NULL)
    first=current=new1;
    else
    {
    /*otherwise find the end of the structure list
    and add on the new structure you just allocated memory for*/

    current=first; //making the first record the current one

    while(current->link!=(struct account *)NULL) //loop through all records
    current=current->link;


    current->link=new1;
    current=new1;

    }

    anum++;

    printf("\n Account number : %5i\n",anum);
    current->number=anum;

    printf("\nEnter customer name : ");
    gets(current->name);

    printf("\nEnter customer balance : ");
    current->balance=atof(gets(buffer));

    current->link=NULL;
    }

    void listall(void)
    {
    if(first==(struct account *)NULL)
    {
    printf("\nThere's no record to be displayed");
    }
    else
    {
    current=first;

    do
    {
    printf("\n%5i: %-15s $%8.2f",current->number,\
    current->name,current->balance);

    }
    while((current=current->link)!=(struct account *)NULL);
    }
    }
    Last edited by a_learner; 12-03-2001 at 01:31 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    hey I got the reason

    anyways thanx for giving it a due considerance

    bye

  3. #3
    Unregistered
    Guest
    did you find out what your error was?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM