Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i did the following code in array of structures. how do i modify it to linked list.

C++
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct emp
{
            int id;
            char name[40],password[10];
            int age;
            int bs;
            int nop;
            char pn1[10];
            int ph1;
            char pn2[10];
            int ph2;
            char pn3[10];
            int ph3;

}e[10];

static int id=1000;

void add()
{
            FILE *fp;
            int a,m,i=0;
            char pword[10],another='y';
            fp=fopen("c:\\emp\\empdetails.lst","a+");

            if(fp==NULL)
            {
                        puts("Cannot open the file\n");
                        exit(1);
            }
            do
            {
                        e[i].id=id++;
                        printf("\nThe ID of the employee is: %d",e[i].id);
                        printf("\nEnter name\n");
                        scanf("%s",e[i].name);
                        o:
                        printf("\nEnter the password with minimum 4 characters\n");
                        scanf("%s",e[i].password);                      

                        if(strlen(e[i].password)<4)
                        {
                                    printf("\nPassword strength is too weak\n");
                                    goto o;
                        }
                        else
                        {
                                    do
                                    {
                                                printf("\nRetype the password\n");
                                                scanf("%s",pword);
                                               if(strcmp(e[i].password,pword)==0)

                                                {

                                                            break;

                                                }

                                                else

                                                {

                                                            printf("\nEnter the correct password\n");

                                                            m++;

                                                }

                                    }while(m>=1);

                        }

 

                        printf("\nEnter the age\n");

                        scanf("%d",&e[i].age);

                        printf("\nEnter the salary\n");

                        scanf("%d",&e[i].bs);

                        p:

                        printf("\nEnter the no of projects\n");

                        scanf("%d",&e[i].nop);

                        if(e[i].nop==1)

                        {

                                    printf("\nEnter name of project1\n");

                                    scanf("%s",e[i].pn1);

                                    printf("\nEnter the no of hrs for project1\n");

                                    scanf("%d",&e[i].ph1);

                        }

                        else if(e[i].nop==2)

                        {

                                    printf("\nEnter name of project1\n");

                                    scanf("%s",e[i].pn1);

                                    printf("\nEnter the no of hrs for project1\n");

                                    scanf("%d",&e[i].ph1);

                                    printf("\nEnter name of project2\n");

                                    scanf("%s",e[i].pn2);

                                    printf("\nEnter the no of hrs for project2\n");

                                    scanf("%d",&e[i].ph2);

                        }

                        else if(e[i].nop==3)

                        {

                                    printf("\nEnter name of project1\n");

                                    scanf("%s",e[i].pn1);

                                    printf("\nEnter the no of hrs for project1\n");

                                    scanf("%d",&e[i].ph1);

                                    printf("\nEnter name of project2\n");

                                    scanf("%s",e[i].pn2);

                                    printf("\nEnter the no of hrs for project2\n");

                                    scanf("%d",&e[i].ph2);

                                    printf("\nEnter name of project3\n");

                                    scanf("%s",e[i].pn3);

                                    printf("\nEnter the no of hrs for project3\n");

                                    scanf("%d",&e[i].ph3);

                        }

                        else

                        {

                                    printf("\nEnter the no of projects not more than 3\n");

                                    goto p;

                        }

                        

                        fwrite(&e[i],sizeof(e[i]),1,fp);

                        printf("\nAdd another record \tY/N\n");

                        fflush(stdin);

                        another=getch();

                        i++;

            }while(another=='y');

            fclose(fp);

}

 

void display()

{

            FILE *fp;

            long int recsize;

            int i=0;

            fp=fopen("c:\\emp\\empdetails.lst","rb");

            if (fp==NULL)

            {

                        puts("Cannot open the file\n");

                        getch();

                        exit(0);

            }

            recsize=sizeof(e[i]);

            printf("ID\tName\tPwrd\tAge\tSal\tPrjt1\tHr1\tPrjt2\tHr2\tPrjt3\tHr3\n\n");

            while(fread(&e[i],sizeof(e[i]),1,fp)==1)

            {

                        printf("%d\t%s\t%s\t%d\t%d\t%s\t%d\t%s\t%d\t%s\t%d\n",e[i].id,e[i].name,e[i].password,e[i].age,e[i].bs,e[i].pn1,e[i].ph1,e[i].pn2,e[i].ph2,e[i].pn3,e[i].ph3);

                        i++;

            }

            fclose(fp);

}

 

void del ()

{

            FILE *fp,*ft;

            int b,h,id,i=0,m=0;

            long int recsize;

            char another='y';

            recsize=sizeof(e[i]);

            do

            {

                        i=0;

                        printf("\nDo u want to delete? Y/N\n");

                        fflush(stdin);

                        another=getch();

                        if(another=='y')

                        {

                                    fp=fopen("c:\\emp\\empdetails.lst","rb+");

                                    if(fp==NULL)

                                    {

                                                printf("\nFile not found\n");

                                    }

                                    fclose(fp);

                                    printf("\nEnter th id of the employee to be deleted\n");

                                    scanf("%d",&id);            

                                    ft=fopen("c:\\emp\\temp.lst","wb");

                                    fp=fopen("c:\\emp\\empdetails.lst","rb+");

                                    rewind(fp);

                                    while(fread(&e[i],recsize,1,fp)==1)

                                    {

                                                if(e[i].id!=id)

                                                {

                                                            fwrite(&e[i],recsize,1,ft);

                                                }

                                                else

                                                {

                                                            m++;

                                                            printf("\nEmployee deleted from FILE\n");

                                                }

                                                i++;

                                                if(m==0)

                                                {

                                                            printf("\nEmployee not found\n");

                                                }

                                    }

                                    fclose(fp);

                                    fclose(ft);

                                    remove("c:\\emp\\empdetails.lst");

                                    rename("c:\\emp\\temp.lst","c:\\emp\\empdetails.lst");

                                    fp=fopen("c:\\emp\\1.lst","rb+");

                                    fclose(fp);

                                    printf("\nDo u want to delete another record? Y/N\n");

                                    fflush(stdin);

                                    another=getch();

                        }

                        i++;

            }while(another=='y');

}

 

void bonus()

{

            FILE *fp;

            char another;

            int id,i=0;

            another='y';

            fp=fopen("c:\\emp\\empdetails.lst","rb+");

            while(another=='y')

            {

                        printf("\nEnter the id of the employee\n");

                        scanf("%d",&id);

                        rewind(fp);

                        while(fread(&e[i],sizeof(e[i]),1,fp)==1)

                        {

                                    if(id==e[i].id)

                                    {

                                                if((e[i].ph1+e[i].ph2+e[i].ph3)>30)

                                                {

                                                printf("Incrementing sal by 10 percent\n");

                                                e[i].bs=e[i].bs+e[i].bs*0.1;

                                                }

                                                else

                                                {

                                                            printf("Incrementing sal by 500\n");

                                                            e[i].bs=e[i].bs+500;

                                                }

                                                fseek(fp,-sizeof(e[i]),SEEK_CUR);

                                                fwrite(&e[i],sizeof(e[i]),1,fp);

                                                break;

                                    }

                        }

                        printf("Modify another record Y/N\n");

                        fflush(stdin);

                        another=getch();

                        i++;

            }

            fclose(fp);

}

 

void adprjct()

{

            FILE *fp;

            char another;

            int id,n,i=0;

            another='y';

            fp=fopen("c:\\emp\\empdetails.lst","rb+");

            while(another=='y')

            {

                        printf("\nEnter the id of the employee\n");

                        scanf("%d",&id);

                        rewind(fp);

                        while(fread(&e[i],sizeof(e[i]),1,fp)==1)

                        {

                                    if(id==e[i].id)

                                    {

                                                a:

                                                printf("Enter the no of projects to be added to the employee\n");

                                                scanf("%d",&n);

                                                if((n==1)&&(e[i].nop<=3))

                                                {

                                                            printf("Enter name of project2\n");

                                    scanf("%s",e[i].pn2);

                                                            printf("Enter the no of hrs for project2\n");

                                            scanf("%d",&e[i].ph2);

                                                            e[i].nop=e[i].nop+1;

                                                }

                                                else if((n==2)&&(e[i].nop<=3))

                                                {

                                                            printf("Enter name of project2\n");

                                                            scanf("%s",e[i].pn2);

                                                            printf("Enter the no of hrs for project2\n");

                                                            scanf("%d",&e[i].ph2);

                                                            printf("Enter name of project3\n");

                                                            scanf("%s",e[i].pn3);

                                                            printf("Enter the no of hrs for project3\n");

                                                            scanf("%d",&e[i].ph3);

                                                            e[i].nop=e[i].nop+2;

                                                }

                                                else

                                                {

                                                            printf("Max no of projects exceeds the limit\n");

                                                            goto a;

                                                }

                                                fseek(fp,-sizeof(e[i]),SEEK_CUR);

                                                fwrite(&e[i],sizeof(e[i]),1,fp);

                                                break;

                                    }

                        }

                        printf("Modify record Y/N\t");

                        fflush(stdin);

                        another=getch();

                        i++;

            }

            fclose(fp);

}

 

int main()

{

            char choice;

            int a,i;

            char another;

            another='y';

            while(1)

            {

                        sw:

                        printf("\n\tEmployee Management Menu\n1.Add\n2.Delete\n3.Display\n4.Bonus\n5.Add Project\n6.Exit\n");

                        printf("\nEnter your choice\t");

                        fflush(stdin);

                        scanf("%c",&choice);

                        switch(choice)

                        {

                                    case '1':

                                                                        add();

                                                                        break;

                                    case '2':

                                                                        del ();

                                                                        break;

                                    case '3':

                                                                        display();

                                                                        break;

                                    case '4':

                                                                        bonus();

                                                                        break;

                                    case '5':

                                                                        adprjct();

                                                                        break;

                                    case '6':

                                                                        printf("Quitting...");

                                                                        getch();

                                                                        exit(0);

                                    default:

                                                                        printf("\nInvalid option. Enter correct option\n");

                                                                        break;

                        }

            }

}
Posted
Updated 30-Jan-11 9:59am
v2
Comments
dasblinkenlight 30-Jan-11 14:42pm    
Please edit your question and put "pre" tags around your code. This way the include file names would not be removed as unrecognized tags.
Henry Minute 30-Jan-11 16:01pm    
I have added <pre> tags for you but the formatting is still all to c***. Please sort it out, otherwise you are unlikely to get any help.
Sandeep Mewara 31-Jan-11 1:15am    
Chill Henry! :)
Sandeep Mewara 31-Jan-11 1:14am    
Just a code dump! Be specific please.

I was going to tidy your code up, and add the code blocks, and in fact I started.
But then I realised just how much of a code dump you had done, and gave up.

And dump is the word.

Rules for you: Edit your question and then you might get a useful reply.

1) Don't dump your entire program: just relevant code fragments. No-one is going to look at your code and work out what is going on with that amount of uncommented "code" to look at.
2) Get rid of the spurious newlines. It makes your code look even bigger than it is.
3) Use "code block" to preserve what formatting you started with - it makes it more readable.
4) Beginners should not use goto. They especially should not use goto when asking for help with homework assignments.
5) Try using comments: they help people understand what your code is supposed to do, even if it doesn't.
6) Try using sensible variable names: "bs" "pn1" "ph1" "e" don't really help anyone understand how your code works.
 
Share this answer
 
Comments
Espen Harlinn 30-Jan-11 16:17pm    
He could try looking at GLib, it has, among other useful things, linked lists. http://www.gtk.org/
Sergey Alexandrovich Kryukov 30-Jan-11 21:09pm    
Espen, you press in right direction - my 5.
This is certainly not enough, but the code is so bad so I wouldn't know what to start with.
You did not point out a biggest crime I can see here: switch, case 1, 2, 3... does not makes sense, even remotely. This reason I voted this way is that I would not know how to do better -- a case looks hopeless to me. I would advice to throw out this code and a lot of ideas from the head, start from the very beginning of programming: what to do with types, variables, functions...
Sad.
--SA
From what I can tell, you never really store the array in memory as a whole, hence a linked list would be a terrible data structure choice.

If you are unfamiliar with linked lists, then I suggest you read up[^] on them. They are designed for storing things effectively in memory where it is not possible or practical to store all the items in a single chunk, hence scattering all the elements of the list throughout the memory. Since a file is a continuous chunk, it is exactly the format of an array or vector, and nothing like the format of a linked list.

To get the most effective use from a linked list you need to be using C++ in order to support templates, otherwise you will have extra references to malloc and pointer dereferences or a less generic data structure.

The basic format of a linked list is
typedef struct LinkedList {
	LinkedList *pNext;
	LinkedList *pPrev; //This one is optional if you want to be able to go backwards.
	//The data for this node, this is where templates would come in handy
	//If you want it to be generic, this would be:
	void *pData; //This would be allocated with malloc(sizeof(emp));
	//If you dont want the extra mallocs and dereferences then you would put your struct in here:
	char name[40],password[10];
	int age;
	int bs;
	int nop;
	char pn1[10];
	int ph1;
	char pn2[10];
	int ph2;
	char pn3[10];
	int ph3;

} LinkedList;

pNext points to the memory of the next node in the list, similarly pPrev points to the previous node.
To delete an element you simply change the pNext in the node before and pPrev in the node after the deleted node to cut out the node you are deleting, then free the memory associated with the deleted node.

With that, hopefully you can see the issues in putting this into a file.
 
Share this answer
 
Comments
Harrison H 18-Feb-11 16:12pm    
Both of you got my 5, but note that linked list isn't only for fragmented data in memory.

It's always important to remember what one data structure benefits over another. A std::vector is guaranteed, like you said, to have contiguous memory, but that also means adding into the middle of it takes a high penalty.

Linked lists don't have contiguous memory, but it excels at adding/deleting a reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900