gpt4 book ai didi

c - 在堆栈中排序/搜索

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:59 24 4
gpt4 key购买 nike

<分区>

我用C语言创建了一个堆栈系统

它接受名字、姓氏和员工编号,程序运行良好。

#include<stdio.h>
#include<conio.h>
#define MAX 20

struct system
{
char first_name[15];
char surname[15];
}employee[20], temp;

int stack[MAX],front=-1,top=-1;
int i;
void push_element();
void pop_element();
void display_stack();
void display_first();

int main()
{
int option;
printf("STACK PROGRAM");
do
{
printf("\n\n 1.Push an element");
printf("\n 2.Pop an element");
printf("\n 3.Display stack");
printf("\n 4.Display first");
printf("\n 5.Display last");
printf("\n 6.Exit");
printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{
case 1: push_element();
break;
case 2: pop_element();
break;
case 3: display_stack();
break;
case 4: display_first();
break;
case 5: display_last();
break;
case 6: return 0;
}

}while(option!=6);
}

void push_element()
{
printf("\n Enter the first name: ");
scanf("%s",employee[i].first_name);

printf("\n Enter the Last name: ");
scanf("%s",employee[i].surname);

int num;
printf("\n Enter the employee number: ");
scanf("%d",&num);
i++;
if(front==0 && top==MAX-1)
printf("\n You have entered more than 20. Please delete a current input to make room. ");
else if(front==-1&&top==-1)
{
front=top=0;
stack[top]=num;

}
else if(top==MAX-1 && front!=0)
{
top=0;
stack[top]=num;
}
else
{
top++;
stack[top]=num;
}
}

void pop_element()
{
top--;
return top;
}

void display_stack()
{
int i;
if(front==-1)
printf("\n No Employees to display");
else
{
printf("\n List of employees:\n\n ");
printf(" Employee number First Name Surname\n\n");
for(i=front;i<=top;i++)
{
printf(" %d \t\t %s \t %s\n", stack[i], employee[i].first_name, employee[i].surname);
}
}
}

void display_first()
{
int i;
if(front==-1)
printf("\n No Employees to display");
else
{
printf("\n The first Employee in the stack is:\n\n ");
printf(" Employee number First Name Surname\n\n");
for(i=front;i<=top;i++)
break;
{
printf(" %d \t\t %s \t %s\n", stack[i], employee[i].first_name, employee[i].surname);
}
}
}

void display_last()
{
int i;
if(front==-1)
printf("\n No Employees to display");
else
{
printf("\n The last Employee in the stack is:\n \n");
printf(" Employee number First Name Surname\n\n");
for(i=top;i<=front;i++)
break;
{
printf(" %d \t\t %s \t %s\n", stack[i], employee[i].first_name, employee[i].surname);
}
}
}

我这辈子都不知道如何对堆栈进行排序。我尝试了其他代码和许多不同的东西,但没有一个接近于找到它但我想按alphabetical 顺序对其进行排序。所以不是按入职时间或员工编号,而是按姓氏的首字母

为此还需要一个搜索功能,并通过员工编号完成。

我在网上看过,在堆栈中使用排序和搜索并不常见,但我需要它。

我不擅长 C,而且我对它还很陌生。任何可能对我有很大帮助的提示或事情将不胜感激。另外,对于任何格式错误,我深表歉意,我对编程和使用软件还很陌生。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com