我正在编写一个程序查询该用户的员工信息,例如名字,姓氏和三位数的员工ID号。结构和链表用于存储数据。遍历链表发生,并显示具有最高ID号的员工的信息。 我试图定义到目前为止具有最大ID的员工的指针,然后在循环中遍历列表,如果当前ID大于前一个,则替换指针。 我脑子里有这个想法和纸上的逻辑,但我很不确定如何将这个过程形成代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct employee
{ char fname [21];
char lname [21];
int id;
struct employee *next;
}DATA;
DATA* insertit (DATA *first, char firstn[], char lastn[], int idnum);
int main (void)
{
DATA *head,
*p;
char firstname[21];
char lastname[21];
int idnumber,
j,
k,
n;
char ch,
nl;
head=NULL;
for (j=0; j<21; j++)
firstname[j]=' ';
printf("Please enter the number of employees to enter information about: ");
scanf("%d", &n);
for(k=0; k<n; k++)
{ printf("Please enter the employee's first name: \n");
scanf("%s", firstname);
printf("Please enter the employee's last name: \n");
scanf("%s", lastname);
printf("Please enter the employee's three digit id number: \n");
scanf("%d", &idnumber);
scanf("%c", &nl);
head=insertit(head, firstname, lastname, idnumber);
for (j=0; j<21; j++)
firstname[j]= ' ';
}
p=head;
while (p!=NULL)
{ printf("\n");
printf("The employee's first name is: %s \n", p->fname);
printf("The employee's last name is: %s \n", p->lname);
printf("The employee's id number is: %d\n", p->id);
p=p->next;
}
printf("Hit any character to continue.");
scanf("%c", &ch);
}
DATA* insertit(DATA *first, char firstn[], char lastn[], int idnum)
/*Function to insert data into a linked list.*/
{
DATA *p,
*q,
*newp;
int found,
len,
i;
found=0;
q=first;
p=first;
while ((p!=NULL) && (!found))
{ if ((p->id < idnum) || (p-> id == idnum))
{ q=p;
p=p->next;
}
else
found=1;
}
newp=(DATA *)malloc(sizeof(DATA));
newp->id=idnum;
strncpy(newp-> fname, firstn, 21);
strncpy(newp-> lname, lastn, 21);
newp->next=p;
if (q!=p)
q->next=newp;
else
first=newp;
return (first);
}
所以,通过我的逻辑,我在想以下几点:
while(z = 0; zsmall) { printf(&#34;身份证号码最高的员工具有以下名字:%s \ n&#34;,p-&gt; fname); printf(&#34;身份证号码最高的员工姓氏如下:%s \ n&#34;,p-&gt; lname); printf(&#34;身份证号码最高的员工具有以下身份证号码:%d \ n&#34;,p-&gt; id); } }
所以,感谢一位用户,我现在有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct employee
{ char fname [21];
char lname [21];
int id;
struct employee *next;
}DATA;
DATA* insertit (DATA *first, char firstn[], char lastn[], int idnum);
void printmax(DATA *head);
int main (void)
{
DATA *head,
*q,
*p;
char firstname[21];
char lastname[21];
int idnumber,
j,
k,
n;
char ch,
nl;
head=NULL;
for (j=0; j<21; j++)
firstname[j]=' ';
printf("Please enter the number of employees that you would like to enter information about: ");
scanf("%d", &n);
for(k=0; k<n; k++)
{ printf("Please enter the employee's first name: \n");
scanf("%s", firstname);
printf("Please enter the employee's last name: \n");
scanf("%s", lastname);
printf("Please enter the employee's three digit id number: \n");
scanf("%d", &idnumber);
scanf("%c", &nl);
head=insertit(head, firstname, lastname, idnumber);
for (j=0; j<21; j++)
firstname[j]= ' ';
}
p=head;
while (p!=NULL)
{ printf("\n");
printf("The employee's first name is: %s \n", p->fname);
printf("The employee's last name is: %s \n", p->lname);
printf("The employee's id number is: %d\n", p->id);
p=p->next;
}
printf("Hit any character to continue."); scanf("%c", &ch);
}
DATA* insertit(DATA *first, char firstn[], char lastn[], int idnum)
/*Function to insert data into a linked list.*/
{
DATA *p,
*q,
*newp;
int found,
len,
i;
found=0;
q=first;
p=first;
while ((p!=NULL) && (!found))
{ if ((p->id < idnum) || (p-> id == idnum))
{ q=p;
p=p->next;
}
else
found=1;
}
newp=(DATA *)malloc(sizeof(DATA));
newp->id=idnum;
strncpy(newp-> fname, firstn, 21);
strncpy(newp-> lname, lastn, 21);
newp->next=p;
if (q!=p)
q->next=newp;
else
first=newp;
return (first);
}
void printmax (DATA *head)
{
DATA *highest = head;
DATA *next = head.next;
while (next != NULL)
{
if (next->highid > highest->highid) {
highest = next;
}
next = next->next;
}
printf("The employee with the highest id number has the first name: %s\n",highest->fname);
printf("The employee with the highest id number has the last name: %s\n",highest->lname);
printf("The employee with the highest id number has the id number: %d\n", highest->id);
}
}
我已经手动跟踪程序,它似乎工作正常,但是当我编译它时,会发生以下错误:
我不熟悉这些错误消息,所以我研究了它们以确定它们的原因,但是,我仍然不确定。因此,如果有人能解释原因并可能解决问题,我将不胜感激。到目前为止,我非常感谢你们的帮助。
答案 0 :(得分:0)
找到最高的员工并打印出来。
void PritMax(const DATA *head) {
const DATA *highest = head;
const DATA *next = head->next;
while (next != NULL) {
if (next->id > highest->id) {
highest = next;
}
next = next->next;
}
printf("The employee with the highest id number has the first name: %s\n",
highest->fname);
printf("The employee with the highest id number has the last name: %s\n",
highest->lname);
printf("The employee with the highest id number has the id number: %d\n",
highest->id);
}