- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个员工链表。做所有常见的事情,添加、搜索和更新。我正在尝试使删除功能正常工作,但据称删除节点后我无法打印链接列表以查看。这是完整的代码,以及错误消息。感谢您花时间阅读本文。
LinkedList.exe 中 0x0FA4FB53 (msvcr120d.dll) 处的未处理异常:0xC0000005:读取位置 0xFEEEFEEE 的访问冲突。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ID_LENGTH 25
#define NAME_LENGTH 25
#define address_LENGTH 25
#define department_LENGTH 25
#define joinDate_LENGTH 25
#define email_LENGTH 30
int mymenu;
//Create employee Structure
typedef struct employee{
char* ID;
char* name;
char* department;
char* address;
char* joinDate;
double salary;
char* email;
struct employee *next;
}employee;
//Declare Function Prototypes
int login();
int Menu();
void Add(struct employee *head);
void search(struct employee *head, char*);
void update(struct employee *head, char*);
void outputList();
struct employee* searchForEmployee(char* );
employee* new_employee(char*, char*, char*, char*, char*, double, char*);
employee* insert_by_employee(employee*, employee*);
void removeEmployee(char *);
void print_list(employee*); // prints out the LinkedList
employee* head = NULL;
employee* tail = NULL;
employee* temp = NULL;
employee* current = NULL;
//this stores the employee that comes before the employee that is found by the searchforEmployee
struct employee *empBeforeEmptoDelete = NULL;
int main() {
int num = 0;
int MenuChoice = 0;
Menu();
FILE *in;
char* ID = (char*)malloc(sizeof(char*) * ID_LENGTH);
char* name = (char*)malloc(sizeof(char*) * NAME_LENGTH);
char* department = (char*)malloc(sizeof(char*) * department_LENGTH);
char* address = (char*)malloc(sizeof(char*) * address_LENGTH);
char* joinDate = (char*)malloc(sizeof(char*) * joinDate_LENGTH);
double salary = 0;
char* email = (char*)malloc(sizeof(char*) * email_LENGTH);
if ((in = fopen("Employees.txt", "r")) == NULL) //Did the file successfully open?
{
printf("The input file failed to open.\n");
printf("Program cannot continue. Exiting. . .\n");
return 1; //Exit Program
}
while (!feof(in)) //Check for file end
{
//Read first data value to kickstart.
if (fscanf(in, "%s %s %s %s %s %lf %s", ID, name, department, address, joinDate, &salary, email) == EOF) {
break;
}
employee* hold = new_employee(ID, name, department, address, joinDate, salary, email);
head = insert_by_employee(head, hold);
}
//3. ------Print the new List------
//print_list(head);
do
{
switch (MenuChoice = Menu())
{
case 1:
{
Add(head);
break;
}
case 2:
{
char text[10];
printf("Enter the text to search for :");
scanf("%s", text);
//search(head, text);
searchForEmployee(text);
break;
}
case 3:
{
char text[10];
printf("Enter the ID to update for :");
scanf("%s", text);
update(head, text);
break;
}
case 4:
{
outputList();
char text[10];
printf("Enter the ID to remove :");
scanf("%s", text);
removeEmployee(text);
outputList();
break;
}
case 5:
{
print_list(head);
break;
}
case 6:
{
break;
}
case 7:
{
break;
}
case 8:
{
exit(0);
break;
}
default:
{
printf("\nInvalid Selection");
break;
}
}
} while (MenuChoice < 8);
system("Pause");
printf("\n\n\n");
return 1; //Exit Success
}
employee* new_employee(char* id, char* name, char* department, char* address, char* joinDate, double salary, char* email) {
//Create new employee and malloc space
employee* new = (employee*)malloc(sizeof(struct employee));
new->ID = (char*)malloc(sizeof(char) * ID_LENGTH);
new->name = (char*)malloc(sizeof(char) * NAME_LENGTH);
new->department = (char*)malloc(sizeof(char) * department_LENGTH);
new->address = (char*)malloc(sizeof(char) * address_LENGTH);
new->joinDate = (char*)malloc(sizeof(char) * joinDate_LENGTH);
new->email = (char*)malloc(sizeof(char) * email_LENGTH);
//Set data
strcpy(new->ID, id);
strcpy(new->name, name);
strcpy(new->department, department);
strcpy(new->address, address);
strcpy(new->joinDate, joinDate);
new->salary = salary;
strcpy(new->email, email);
//Retun a pointer to the node
return new;
}
//Inserts new node into an alphabetically sorted linked list.
employee* insert_by_employee(employee* head, employee* new)
{
employee* current = NULL;
current = head;
if (current == NULL || strcmp(current->department, new->department) > 0)
{
new->next = current;
return new;
}
else {
while (current->next != NULL && strcmp(current->next->department, new->department) < 0)
{
current = current->next;
}
}
new->next = current->next;
current->next = new;
return head;
}
struct employee* searchForEmployee(char* id){
struct employee *empIterator = head;
char i[] = "Employee ID";
char n[] = "Name";
char a[] = "Address";
char d[] = "Department";
char jd[] = "Join Date";
char s[] = "Salary";
char em[] = "Email";
while (empIterator != NULL){
int isEqual = strcmp(empIterator->ID, id);//if the passed in ID is equla to the ID of the node then isEqual will ring true
if (!isEqual){
printf("Employee Found\n");
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);//header formatting
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", empIterator->ID, empIterator->name, empIterator->address, empIterator->department, empIterator->joinDate, empIterator->salary, empIterator->email);
//printing out the current node which is the desired employee
printf("-----------------------------------------------------------------------------------------------------------------------------------------------------\n");
return empIterator;
}
empBeforeEmptoDelete = empIterator->next;
empIterator = empIterator->next;
}
printf("%s was not found\n\n", id);
return NULL;
}
void print_list(employee* head)
{
employee* current;
current = head;
char i[] = "Employee ID";
char n[] = "Name";
char a[] = "Address";
char d[] = "Department";
char jd[] = "Join Date";
char s[] = "Salary";
char em[] = "Email";
//Header
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);
printf("--------------------------------------------------------------------------------------------------------------------------------------------\n");
while (current != NULL)
{
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", current->ID, current->name, current->address, current->department, current->joinDate, current->salary, current->email);
current = current->next;
}
printf("-------------------------------------------------------------------------------------------------------------------------------------------\n");
return;
}
int login()
{
char username[7];
char password[7];
printf("\nPlease enter your Username: ");
scanf("%s", username);
printf("\nPlease enter your Password: ");
scanf("%s", password);
struct login{
char name[7];
char password[7];
};
struct login details[3];
fflush(stdin);
FILE *file;
file = fopen("login.txt", "r");
if (file == NULL){
printf("Can not open the file\n");
exit(-1);
}
fflush(stdin);
for (int i = 0; i < 3; i++){
fscanf(file, "%s %s\n", details[i].name, details[i].password);
}
for (int i = 0; i < 3; i++){
if ((strcmp(details[i].name, username) == 0) && (strcmp(details[i].password, password) == 0))
{
printf("\nWelcome ");
Menu();
}
fflush(stdin);
}
}
int Menu()
{
int Choice = 0;
do{
printf("1. Add\n");
printf("2. Show\n");
printf("3. Update\n");
printf("4. Delete\n"); // Write a Function for this
printf("5. Departments\n");
printf("6. Employee Report\n");
printf("7. \n");
printf("8. Exit\n\n\n\t\tSELECTION = ");
fflush(stdin);
scanf("%d", &Choice);
fflush(stdin);
} while (Choice < 0 || Choice > 8);
return(Choice);
}
void Add()
{
char* ID = (char*)malloc(sizeof(char*) * ID_LENGTH);
char* name = (char*)malloc(sizeof(char*) * NAME_LENGTH);
char* department = (char*)malloc(sizeof(char*) * department_LENGTH);
char* address = (char*)malloc(sizeof(char*) * address_LENGTH);
char* joinDate = (char*)malloc(sizeof(char*) * joinDate_LENGTH);
double salary = 0;
char* email = (char*)malloc(sizeof(char*) * email_LENGTH);
printf("\nEnter the ID : ");
scanf("%s", ID);
printf("\nEnter the new employee name : ");
scanf("%s", name);
printf("\nEnter their address : ");
scanf("%s", address);
printf("\nEnter their department : ");
scanf("%s", department);
printf("\nEnter their start date : ");
scanf("%s", joinDate);
printf("\nEnter their salary : ");
scanf("%lf", &salary);
printf("\nEnter their email : ");
scanf("%s", email);
employee* hold = new_employee(ID, name, department, address, joinDate, salary, email);
head = insert_by_employee(head, hold);
}
void search(struct employee *head, char *crit)//This function takes in the head pointer and a character array pointer
{
int choice;//menu choice tracker
//arrays to hold the header heads
char i[] = "Employee ID";
char n[] = "Name";
char a[] = "Address";
char d[] = "Department";
char jd[] = "Join Date";
char s[] = "Salary";
char em[] = "Email";
//sub menu to filter by ID and Name searches
printf("\nSearch by ");
printf("\n1 : ID");
printf("\n2 : Name\n");
scanf("%d", &choice);
//if the user wants to search by ID
if (choice == 1){
while (head != NULL)
{
//compare the ID to the criteria. If its the same then execute this if
if ((strcmp(head->ID, crit) == 0))
{
//print that we found the employee
printf("Employee Found\n");
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);//header formatting
printf("----------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", head->ID, head->name, head->address, head->department, head->joinDate, head->salary, head->email);
//printing out the current node which is the desired employee
printf("-----------------------------------------------------------------------------------------------------------------------------------------------------\n");
return;
}
head = head->next;//increments the node until we get to the end of the Linked List
}
printf("Employee not found\n");//Else there is no Employee with an ID of the entered criteria
}
//if the user wants to search by Name
else if (choice == 2){
while (head != NULL)
{
//compare the Name to the criteria. If its the same then execute this if
if ((strcmp(head->name, crit) == 0))
{
//print that we found the employee
printf("Employee Found\n");
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);//header formatting
printf("----------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", head->ID, head->name, head->address, head->department, head->joinDate, head->salary, head->email);
//printing out the current node which is the desired employee
printf("----------------------------------------------------------------------------------------------------------------------------------------------------\n");
return;
}
head = head->next;//increments the node until we get to the end of the Linked List
}
printf("Employee not found\n");//Else there is no Employee with an Name of the entered criteria
}
else
printf("Bad input");//Otherwise the user entered a choice out of the range of our handled input
}
void update(struct employee *head, char *crit)//This function takes in the head pointer and a character array pointer
{
//arrays to hold the header heads
char i[] = "Employee ID";
char n[] = "Name";
char a[] = "Address";
char d[] = "Department";
char jd[] = "Join Date";
char s[] = "Salary";
char em[] = "Email";
char id[25];
char name[25];
char address[25];
char department[25];
char joinDate[25];
double salary;
char email[25];
//if the user wants to search by ID
while (head != NULL)
{
//compare the ID to the criteria. If its the same then execute this if
if ((strcmp(head->ID, crit) == 0))
{
//print that we found the employee
printf("Employee Found\n");
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);//header formatting
printf("----------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", head->ID, head->name, head->address, head->department, head->joinDate, head->salary, head->email);
//printing out the current node which is the desired employee
printf("-----------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("\nEnter the ID : ");
scanf("%s", id);
printf("\nEnter the new employee name : ");
scanf("%s", name);
printf("\nEnter their address : ");
scanf("%s", address);
printf("\nEnter their department : ");
scanf("%s", department);
printf("\nEnter their start date : ");
scanf("%s", joinDate);
printf("\nEnter their salary : ");
scanf("%lf", &salary);
printf("\nEnter their email : ");
scanf("%s", email);
strcpy(head->ID, id);
strcpy(head->name, name);
strcpy(head->department, department);
strcpy(head->address, address);
strcpy(head->joinDate, joinDate);
head->salary = salary;
strcpy(head->email, email);
printf("Employee Found\n");
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);//header formatting
printf("----------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", head->ID, head->name, head->address, head->department, head->joinDate, head->salary, head->email);
//printing out the current node which is the desired employee
printf("-----------------------------------------------------------------------------------------------------------------------------------------------------\n");
return;
}
head = head->next;//increments the node until we get to the end of the Linked List
}
printf("Employee not found\n");//Else there is no Employee with an ID of the entered criteria
}
void outputList(){
char i[] = "Employee ID";
char n[] = "Name";
char a[] = "Address";
char d[] = "Department";
char jd[] = "Join Date";
char s[] = "Salary";
char em[] = "Email";
struct employee * employees = head;
printf("Employees Entered\n\n");
while (employees != NULL){
printf("Employee Found\n");
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);//header formatting
printf("|%15s | %15s | %15s |%15s | %15s | %15.2lf| %25s\n", employees->ID, employees->name, employees->address, employees->department, employees->joinDate, employees->salary, employees->email);
printf("-----------------------------------------------------------------------------------------------------------------------------------------------------\n");
employees = employees->next;
}
}
void removeEmployee(char* empID){
struct employee* empToDelete = NULL;
empToDelete = searchForEmployee(empID);
if (empToDelete != NULL){
printf("%s was deleted\n\n", empID);
if (empToDelete == head){
head = empToDelete->next;
}
else{
empBeforeEmptoDelete->next = empToDelete->next;
}
free(empToDelete);
}
else{
printf("%s was not found", empID);
}
}
最佳答案
在 removeEmployee(char* empID)
函数中:
情况:当要删除的节点不是头节点时;
这里首先要保存delete_node
的previous_node
指针,然后将previous_node->next
设置为delete_node->next
并删除 delete_node
。
你还没有保存previous_node
的指针。
empBeforeEmptoDelete->next = empToDelete->next;
关于c - 删除节点后打印结构链表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29925751/
我知道如何通过iPhone开发创建sqlite数据库、向其中插入数据、删除行等,但我试图以编程方式删除整个数据库本身,但没有得到任何帮助。请有人指导我如何通过代码从设备中删除/删除整个 sqlite
请帮助指导如何在 Teradata 中删除数据库。 当我运行命令DROP DATABASE database_name时,我收到错误消息: *** Failure 3552 Cannot DROP d
Azure 警报规则的删除命令似乎不起作用,尝试了下面的方法,它返回状态为无内容,并且警报未被删除 使用的命令Remove-AzAlertRule -ResourceGroup "RGName"-Na
我在 flex 搜索中为大约50000个视频建立了索引,但是当它达到52000左右时,所有数据都被删除。嗯,这对我来说真的很奇怪,我没有为ES设置任何Heap大小或最小或最大大小的内存大小,因此它们没
我正在处理的问题是表单错误“输入由字母、数字、下划线或连字符组成的有效‘slug’。” 以下是我的表单字段验证: def clean_slug(self): slug = self.c
阅读文档,我希望 $("#wrap2").remove(".error") 从 中删除所有 .error 元素#wrap2。然而看看这个 JSFiddle: http://jsfiddle.net/h
嗨,我第一次尝试发现 laravel 我从 laravel 4.2 开始,我刚刚创建了一个新项目,但我误以为我写了这样的命令行 composer create-project laravel/lara
我已经在网上搜索了很长一段时间,但我找不到如何完全删除 apache 2.4 。 使用: Windows 7 c:\apache24\ 我已经尝试了所有命令,但没有任何效果。 httpd -k shu
可能是一个简单的答案,所以提前道歉(最少的编码经验)。 我正在尝试从任何列中删除具有特定字符串(经济 7)的任何行,并且一直在尝试离开此线程: How to drop rows from pandas
有几种方法可以删除/移除 vector 中的项目。 我有一个指针 vector ,我需要在类的析构函数中删除所有指针。 什么是最有效/最快甚至最安全的方式? // 1º std::for_each(v
我安装了一个 VNC 服务器并在某处阅读了我必须安装 xinetd 的信息。稍后我决定删除 VNC 服务器,所以我也删除了 xinetd。似乎 xinetd 删除了一些与 plesk 相关的文件,如果
我制作了一个从我们的服务器下载视频的应用。问题是: 当我取消下载时,我打电话: myAsyncTask.cancel(true) 我注意到,myAsyncTask 并没有在调用取消时停止...我的 P
是否可以在使用DELETE_MODEL删除模型之前检查模型是否存在我试图避免在尝试删除尚未创建的模型时收到错误消息。基本上我正在寻找对应的: DROP TABLE IF EXISTS 但对于模型。 最
我已经有了这个代码: 但它仍然会生成一个表行条目。 我想做的是,当输入的数量为0时,表行将被删除。请耐心等待,因为我是 php 和 mySQL 编码新手。 最佳答案 您忘记执行查询。应该是 $que
在 SharePoint 中,如果您删除/修改重复日历条目的单次出现,则不会真正删除/修改任何内容 - 相反,会创建一个新条目,告诉 SP 对于特定日期,该事件不存在或具有新参数. 因此,这可以通过删
在 routes.php 中我有以下路由: Route::post('dropzone', ['as' => 'dropzone.upload', 'uses' => 'AdminPhotoContr
在我的应用程序中,我正在尝试删除产品。当我第一次删除产品时,它会成功并且 URL 更改为/remove_category/15。我正在渲染到同一页面。现在,当我尝试删除另一个产品时,网址更改为/rem
这个问题被问了很多次,但给出的答案都是 GNU sed 特定的。 sed -i '' "/${FIND}/,+2d""$FILE" 给出“预期的上下文地址”错误。 有人可以给我一个例子,说明如何使用
在使用 V3 API 时,我找不到任何方法来删除和清理 Google map 。 我已经在 AJAX 站点中运行它,所以我想完全关闭它而无需重新加载页面。 我希望有一个 .unload() 或 .de
是否可以创建一个 Azure SQL 数据库用户来执行以下操作: 针对所有表和 View 进行 SELECT 创建/更改/删除 View 但用户不应该不拥有以下权限: 针对任何表或 View 插入/更
我是一名优秀的程序员,十分优秀!