从结构数组转换为链表 [英] converting from array of structures to linked list

查看:120
本文介绍了从结构数组转换为链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在结构数组中执行了以下代码.如何将其修改为链表.

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

结构 emp
{
             int  id;
            字符名称[ 40 ],密码[ 10 ];
             int 年龄;
             int  bs;
             int  nop;
            字符 pn1 [ 10 ];
             int  ph1;
            字符 pn2 [ 10 ];
             int  ph2;
            字符 pn3 [ 10 ];
             int  ph3;

} e [ 10 ];

静态  int  id =  1000 ;

无效 add()
{
            文件* fp;
             int  a,m,i =  0 ;
            字符 pword [ 10 ],另一个= '  y';
            fp = fopen("   a +");

            如果(fp == NULL)
            {
                        puts(" );
                        exit( 1 );
            }
            
            {
                        e [i] .id = id ++;
                        printf(" ,e [i] .id) ;
                        printf(" );
                        scanf(" ,e [i] .name);
                        o:
                        printf(" );
                        scanf(" ,e [i] .password);

                        如果(strlen(e [i] .password)< 4)
                        {
                                    printf(" );
                                    转到 o;
                        }
                        其他
                        {
                                    
                                    {
                                                printf(" );
                                                scanf(" ,pword);
                                               如果(strcmp(e [i] .password,pword)== < 0 )

                                                {

                                                             break ;

                                                }

                                                其他

                                                {

                                                            printf(" );

                                                            m ++;

                                                }

                                    } 同时(m> =  1 );

                        }

 

                        printf(" );

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

                        printf(" );

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

                        p:

                        printf(" );

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

                        如果(e [i] .nop ==  1 )

                        {

                                    printf(" );

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

                                    printf(" );

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

                        }

                        其他 如果(e [i] .nop ==  2 )

                        {

                                    printf(" );

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

                                    printf(" );

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

                                    printf(" );

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

                                    printf(" );

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

                        }

                        其他 如果(e [i] .nop ==  3 )

                        {

                                    printf(" );

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

                                    printf(" );

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

                                    printf(" );

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

                                    printf(" );

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

                                    printf(" );

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

                                    printf(" );

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

                        }

                        其他

                        {

                                    printf(" );

                                    转到 p;

                        }

                        

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

                        printf(" );

                        fflush(stdin);

                        another = getch();

                        i ++;

            } 同时(另一个== '  y');

            fclose(fp);

}

 

无效 display()

{

            文件* fp;

              int 调整大小;

             int  i =  0 ;

            fp = fopen("   rb");

            如果(fp == NULL)

            {

                        puts(" );

                        getch();

                        exit( 0 );

            }

            recsize =  sizeof (e [i]);

            printf(" );

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

            {

                        printf(" ,e [i] .id,e [i] .name,e [i].密码,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);

}

 

无效 del()

{

            文件* fp,* ft;

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

              int 调整大小;

            字符 another = '  y';

            recsize =  sizeof (e [i]);

            

            {

                        i =  0 ;

                        printf(" );

                        fflush(stdin);

                        another = getch();

                        如果(另一个== '  y' )

                        {

                                    fp = fopen("   rb +");

                                    如果(fp == NULL)

                                    {

                                                printf(" );

                                    }

                                    fclose(fp);

                                    printf(" );

                                    scanf(" ,& id);

                                    ft = fopen(" "   wb");

                                    fp = fopen("   rb +");

                                    倒带(fp);

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

                                    {

                                                如果(e [i] .id!= id)

                                                {

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

                                                }

                                                其他

                                                {

                                                            m ++;

                                                            printf(" );

                                                }

                                                i ++;

                                                如果(m ==  0 )

                                                {

                                                            printf(" );

                                                }

                                    }

                                    fclose(fp);

                                    fclose(ft);

                                    remove(" );

                                    重命名("   c:\\ emp \\ empdetails.lst");

                                    fp = fopen("   rb +");

                                    fclose(fp);

                                    printf(" );

                                    fflush(stdin);

                                    another = getch();

                        }

                        i ++;

            } 同时(另一个== '  y');

}

 

无效 bonus()

{

            文件* fp;

            字符另一个;

             int  id,i =  0 ;

            another = '  y';

            fp = fopen("   rb +");

            同时(另一个== '  y' )

            {

                        printf(" );

                        scanf(" ,& id);

                        倒带(fp);

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

                        {

                                    如果(id == e [i] .id)

                                    {

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

                                                {

                                                printf(" );

                                                e [i] .bs = e [i] .bs + e [i] .bs *  0 . 1 ;

                                                }

                                                其他

                                                {

                                                            printf(" );

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

                                                }

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

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

                                                 break ;

                                    }

                        }

                        printf(" );

                        fflush(stdin);

                        another = getch();

                        i ++;

            }

            fclose(fp);

}

 

无效 adprjct()

{

            文件* fp;

            字符另一个;

             int  id,n,i =  0 ;

            another = '  y';

            fp = fopen("   rb +");

            同时(另一个== '  y' )

            {

                        printf(" );

                        scanf(" ,& id);

                        倒带(fp);

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

                        {

                                    如果(id == e [i] .id)

                                    {

                                                A:

                                                printf(" );

                                                scanf(" ,& n);

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

                                                {

                                                            printf(" );

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

                                                            printf(" );

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

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

                                                }

                                                其他 如果((n ==  2  )&&(e [i] .nop< =  3 ))

                                                {

                                                            printf(" );

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

                                                            printf(" );

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

                                                            printf(" );

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

                                                            printf(" );

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

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

                                                }

                                                其他

                                                {

                                                            printf(" );

                                                            转到

                                                }

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

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

                                                 break ;

                                    }

                        }

                        printf(" );

                        fflush(stdin);

                        another = getch();

                        i ++;

            }

            fclose(fp);

}

 

 int  main()

{

            字符选择;

             int  a,i;

            字符另一个;

            another = '  y';

            同时( 1 )

            {

                        sw:

                        printf(" );

                        printf(" );

                        fflush(stdin);

                        scanf(" ,& choice);

                        开关(选择)

                        {

                                    案例 '  1':

                                                                        添加();

                                                                         break ;

                                    案例 '  2':

                                                                        del();

                                                                         break ;

                                    案例 '  3':

                                                                        展示();

                                                                         break ;

                                    案例 '  4':

                                                                        奖金();

                                                                         break ;

                                    案例 '  5':

                                                                        adprjct();

                                                                         break ;

                                    案例 '  6':

                                                                        printf(" );

                                                                        getch();

                                                                        exit( 0 );

                                    默认:

                                                                        printf(" );

                                                                         break ;

                        }

            }

} 

解决方案

我要整理您的代码,并添加代码块,实际上我已经开始了.
但是后来我意识到您做了多少代码转储,然后放弃了.

转储就是这个词.

适用规则:编辑问题,然后可能会得到有用的答复.

1)不要转储整个程序:只是相关的代码片段.没有人会看一下您的代码,并弄清楚要看的大量未注释的代码"是怎么回事.
2)摆脱虚假的换行符.它使您的代码看起来比实际的还要大.
3)使用代码块"保留您开始时使用的格式-使其更具可读性.
4)初学者不应使用goto.在寻求作业分配帮助时,他们尤其不应使用goto .
5)尝试使用注释:它们可以帮助人们理解您的代码应该做的事情,即使事实并非如此.
6)尝试使用明智的变量名:"bs","pn1","ph1","e"并不能真正帮助任何人理解您的代码的工作方式.


据我所知,您永远不会真正将数组作为整体存储在内存中,因此,链表将是一个糟糕的数据结构选择.

如果您不熟悉链表,那么建议您阅读 [#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; } } }

解决方案

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.


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.


这篇关于从结构数组转换为链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆