link.h#ifndef _LINK_H_
#define _LINK_H_
#define ASCENDING 0
#define DESCENDING 1
#define UP 0
#define DOWN 1
typedef struct NODE
{
int value;
struct NODE *link;
} Node;
typedef struct
{
unsigned num;
struct NODE *head_add;
} Node_infoInit;
void Create(Node_infoInit *Node_info_c);
void list_print(Node_infoInit *Node_info);
void list_sort(unsigned mode, Node_infoInit *Node_info);
void list_insert(Node_infoInit *Node_info, int new_value);
#endif
link.c:
#include"link.h"
#include<stdio.h>
#include<stdlib.h>
void Create(Node_infoInit *Node_info)
{
unsigned i;
int value_test;
Node *cnew , *cnew_temp , *head;
printf("please define the length of list\n");
scanf("%d" , &Node_info->num);
if(Node_info->num != 0)
{
printf("please input value to elements of list\n");
for(i = 0; i < Node_info->num; )
{
scanf("%d" , &value_test);
if((value_test > 100) || (value_test < -100))
{
printf("the value %d is out of limit , please input again\n", value_test);
}
else
{
cnew_temp = (Node *)malloc(sizeof (Node));
if(i == 0)
{
head = cnew_temp;
cnew = head;
head->value = value_test;
Node_info->head_add = head;
}
else
{
cnew->link = cnew_temp;
cnew = cnew_temp;
cnew->value = value_test;
}
i++;
cnew->link = NULL;
}
}
}
else
{
Node_info->head_add = NULL;
}
}
void list_print(Node_infoInit *Node_info)
{
Node *list;
list = Node_info->head_add;
while(1)
{
printf("%d\n", list->value);
list = list->link;
if(list->link == NULL)
{
printf("%d\n", list->value);
break;
}
}
}
void list_sort(unsigned mode, Node_infoInit *Node_info)
{
Node *list;
int temp;
unsigned i, j;
list = Node_info->head_add;
if(mode == UP)
{
printf("Sort by ascending\n");
for(i = Node_info->num; i > 1; i--)
{
for(j = i; j > 1; j--)
{
if(list->value < list->link->value)
{
temp = list->link->value;
list->link->value = list->value;
list->value = temp;
}
list = list->link;
}
list = Node_info->head_add;
}
}
else
{
printf("Sort by descending\n");
for(i = Node_info->num; i > 1; i--)
{
for(j = i; j > 1; j--)
{
if(list->value > list->link->value)
{
temp = list->link->value;
list->link->value = list->value;
list->value = temp;
}
list = list->link;
}
list = Node_info->head_add;
}
}
}
void list_insert(Node_infoInit *Node_info, int new_value)
{
Node **linkp;
Node *list;
Node *cnew;
linkp = &Node_info->head_add;
while((list = *linkp) != NULL && list->value < new_value)
linkp = &list->link;
cnew = (Node *)malloc(sizeof(Node));
cnew->value = new_value;
cnew->link = list;
*linkp = cnew;
Node_info->num++;
}
两个指向同一个地址的指针,还是指向一个地址的指针和指向这个指针的指针。这两个操作哪个靠谱? |