#include<stdio.h>
#include<stdlib.h>
node* headPointer(); //创建头节点,并初始化
void createLink(node* head, int* arr); //创建链表(有头节点)
node*findNode(node* head); //查找链表的节点
void addNode(node* head); //添加链表的节点
void travLink(node* head); //遍历链表
typedef struct Node
{
int data; //数据域
struct Node* next; //指针域
}node;
int main()
{
int i = 0;
int arr[] = { 23,34,45,56,67,78,89 };
node* head = headPointer(); //创建头节点,并初始化
createLink(head, arr); //创建链表(有头节点)
//node* tarNode = findNode(head);
addNode(head); //添加新的节点
travLink(head); //遍历链表
free(head);
return 0;
}
//创建头节点,并初始化
node* headPointer()
{
node* head = (node*)malloc(sizeof(node));
if (!head)
{
printf("头节点创建失败");
exit(0);
}
head->next = NULL;
head->data = 0;
printf("头节点创建成功\n");
return head;
}
//创建链表(有头节点)
void createLink(node* head,int*arr)
{
node* move_Pointer = head; //移动指针指向头节点
while (head->data!=7)
{
node* newNode = (node*)malloc(sizeof(node));
if (newNode == NULL)
{
printf("新节点创建失败");
exit(0);
}
newNode->data = arr[head->data]; //为新节点赋值
newNode->next = NULL; //初始化新节点的指针域
// 尾插法----------------------------------------------------
move_Pointer->next = newNode;
move_Pointer = newNode;
//-----------------------------------------------------------
//头插法------------------------------------------------------
//newNode->next = move_Pointer->next;
//move_Pointer->next = newNode;
//-----------------------------------------------------------
head->data += 1;
}
printf("链表创建成功\n");
}
//遍历链表
void travLink(node* head)
{
node* move_Pointer = head->next;
while (move_Pointer!=NULL)
{
printf("%d\n", move_Pointer->data);
move_Pointer = move_Pointer->next;
}
}
//删除节点
//void delNode(node* head)
//{
// node* tarNode = findNode(head);
// node* P;
// if (!tarNode)
// {
// printf("没有找到删除的位置\n");
// exit(0);
// }
//
//}
//查找链表的节点
node* findNode(node* head)
{
int i;
int j = 1;
node* movePointer = head->next;
printf("你要查找第几个节点\n");
scanf_s("%d", &i);
if (i <= head->data)
{
while (movePointer!= NULL)
{
if (i == 0)
return head;
if (i == j)
{
return movePointer;
}
else
{
movePointer = movePointer->next;
j += 1;
}
}
}
else
{
printf("没有你要查找的节点");
exit(0);
}
}
//添加链表的节点
void addNode(node*head)
{
node* tarNode = findNode(head);
int i;
node* P;
if (!tarNode)
{
printf("没有查找到添加位置\n");
exit(0);
}
P = tarNode->next;
node* newNode = (node*)malloc(sizeof(node));
if (!newNode)
{
printf("新添加的节点创建失败\n");
exit(0);
}
printf("你要添加的数据是:");
scanf_s("%d", &i);
newNode->next = NULL;
newNode->data = i;
tarNode->next = newNode;
newNode->next = P;
head->data += 1;
}
请帮忙看看
间接寻址级别不同的报错以前没遇到过;
为什么已经声明了函数还会报重定义的错;
声明函数后又出现了其它报错,如下;