马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<winuser.inl>
#include<stdio.h>
#include <cstdlib>
#include<string.h>
//将一个整数插进一个有序的数组
void getInput(struct Array* number);
void printfInfo(struct Array** head);
void releaseMemory(struct Array** head);
void tail_add(struct Array** head);
void find_position(struct Array** head, int insert_number);
struct Array
{
int value;
struct Array *next;
};
void getInput(struct Array* number)
{
printf("enter number:");
scanf_s("%d", &number->value);
}
void printfInfo(struct Array** head)
{
struct Array* number;
number = *head;
printf("the array is:\n");
while (number != NULL)
{
printf("%d",number->value);
number = number->next;
}
printf("\n");
}
void releaseMemory(struct Array** head)
{
struct Array* number, * temp;
number = *head;
while (number->next != NULL)
{
temp = number;
number = number->next;
free(temp);
}
}
void find_position(struct Array** head,int insert_number)
{
struct Array* previous,*current,*new_node;
//直接给previous赋值null,current赋值头节点,后面就不需要再用中间变量来存储之后,再进行交换地址了
//current此时就是一个空的结构体,头指针给他之后,他指向了链表的头节点
current = *head;
previous = NULL;
//找到合适于插入值的位置的前后节点的位置
while (current->next != NULL && current->value < insert_number)
{
previous = current;
current = current->next;
}
//生成一个新的节点
new_node = (struct Array*)(malloc(sizeof(struct Array)));
if (new_node == NULL)
{
printf("memory failed");
exit(1);
}
//将新的节点变成链表里面的一员,插入到前面找到的合适的位置那
if(current->next == NULL) //空链表,直接将头指针指向他
{
*head = new_node;
}
else
{
previous->next = new_node;
new_node->next = current;
new_node->value = insert_number;
}
}
void tail_add(struct Array** head)
{
struct Array* number, * temp;
number = (struct Array*)(malloc(sizeof(struct Array)));
if (number == NULL)
{
printf("memory failed");
exit(1);
}
getInput(number);
if (*head != NULL)
{
temp = *head;
//找尾节点
while (temp->next != NULL)
{
temp = temp->next;
};
//插入数据
temp->next = number;
number->next = NULL;
}
else
{
*head = number;
number->next = NULL;
}
}
int main()
{
struct Array* numberarray = NULL; ////初始化一个指针,头指针,相当于定义了一个空的单链表
//struct Student* stu = NULL;
int num;
printf("total num of stu:");
scanf_s("%d", &num);
printf("\n");
printf("enter info:\n");
for (int i = 0; i < num; i++)
{
tail_add(&numberarray);
}
printf("finish entering!\n");
//printf("begining to print\n");
//printfInfo(&namelist);
int insert_number;
char sign[10];
do
{
printf("enter the number you want to insert:");
scanf_s("%d", &insert_number);
find_position(&numberarray, insert_number);
printfInfo(&numberarray);
printf("enter any letter continue,#表示结束\n");
scanf_s("%s", sign,10);
} while (sign[0] != '#');
releaseMemory(&numberarray);
return 0;
}
void find_position(struct Array** head,int insert_number)
{
struct Array* previous,*current,*new_node;
previous = * head;
current = (previous == NULL) ? NULL : previous->next;
new_node = (struct Array*)(malloc(sizeof(struct Array)));
if (new_node == NULL)
{
printf("memory failed");
exit(1);
}
else
{
new_node->value = insert_number;
new_node->next = NULL;
}
if(previous == NULL || insert_number <= previous->value)
{
*head = new_node;
(*head)->next = previous;
}
else
{
while (current != NULL && current->value < insert_number)
{
previous = current;
current = current->next;
}
previous->next = new_node;
new_node->next = current;
}
}
|