我编写了一个按指针工作的基本链表。它具有根据位置创建,插入,删除和查找节点的功能。 出于某种原因,构建输出显示
1>Done building project "Linked List.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
遗憾的是没有错误,所以我不明白是什么问题。
有人看到我在这里缺少的东西吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
struct node *head = NULL;
void PrintList()
{
struct node *temp = (node*)malloc(sizeof(node));
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
node* New(int data)
{
node *new = (node*)malloc(sizeof(node));
if (new == NULL)
{
printf("Error \n");
exit(0);
}
new->data = data;
new->next = NULL;
return new;
}
void Create(int data)
{
node *temp = head;
while (temp->next != NULL)
temp = temp->next;
node *new = New(data);
temp->next = new;
}
void Insert(int data, int position)
{
struct node *temp1 = (struct node*)malloc(sizeof(struct node*));
temp1->data = data;
temp1->next = NULL;
if (position == 0)
{
temp1->next = head;
head = temp1;
return;
}
struct node *temp2 = (struct node*)malloc(sizeof(struct node*));
for (int i = 0; i < position - 2; i++)
{
temp2 = temp2->next;
temp2->next = temp1;
}
}
void Delete(int position)
{
struct node *temp1 = (struct node*)malloc(sizeof(struct node*));
if (position == 0)
{
head = temp1->next;
free(temp1);
return;
}
for (int i = 0; i < position - 2; i++)
temp1 = temp1->next;
struct node *temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
int Find(int position)
{
struct node *temp = (node*)malloc(sizeof(node));
for (int i = 0; i < position + 1; i++)
temp = temp->next;
return temp->data;
free(temp);
}
void menu()
{
printf("Linked List in C \n\n");
printf("1.Create an element\n");
printf("2.Insert as the kth element\n");
printf("3.Delete the kth element\n");
printf("4.Find the kth element\n");
printf("0.Exit\n");
}
int main()
{
int command;
int data;
int position;
head = NULL;
menu();
while (1)
{
printf("\nEnter a command (0~4):");
scanf("%d", &command);
if (command == 0)
break;
switch (command)
{
case 1:
printf("Enter a number to be created as the last element: \n");
scanf("%d", data);
Create(data);
PrintList();
break;
case 2:
printf("Enter a number to insert into the list: \n");
scanf("%d", data);
printf("Enter the position to insert this element: \n");
scanf("%d", position);
Insert(data, position);
PrintList();
break;
case 3:
printf("Enter the position of the element to be deleted: \n");
scanf("%d", position);
Delete(position);
PrintList();
break;
case 4:
printf("Enter the position of the element you are looking for: \n");
scanf("%d", position);
int X = Find(position);
printf("The element in node %d is %d. \n", position, X);
PrintList();
break;
}
}
//dispose(head);
return 0;
}