C言語による一方向チェーンテーブルの反転
1631 ワード
C言語による一方向チェーンテーブルの反転
大宝さん
時間:2015-12-22
試験環境:VC++6.0 gcc version 4.6.3(Ubuntu/Linaro 4.6.3-1 ubuntu 5)
大宝さん
時間:2015-12-22
試験環境:VC++6.0 gcc version 4.6.3(Ubuntu/Linaro 4.6.3-1 ubuntu 5)
/*
:
:
:2015-12-22
*/
#include
#include
struct list
{
struct list *p;
int n;
};
typedef struct list List;
/*
List
*/
List *create_list()
{
int i = 0;
List *head = NULL;
List *pnote = NULL;
pnote = (List *)malloc(sizeof(List));
head = pnote;
for(i=0; i<4; i++)
{
pnote->n = i + 1;
pnote->p = (List *)malloc(sizeof(List));
pnote = pnote->p;
}
pnote->n = i + 1;
pnote->p = NULL;
return head;
}
/*
*/
void free_list(List **head)
{
List *pnote = *head;
while((*head)->p != NULL)
{
pnote = (*head)->p;
free(*head);
*head = pnote;
}
*head = NULL;
}
/*
*/
void print_list(List *head)
{
if(head == NULL)
{
printf("head is NULL!
");
return ;
}
do
{
printf("%d ", head->n);
head = head->p;
}
while(head != NULL);
printf("
");
}
/*
*/
List *list_reverse(List *head)
{
List *new_head = NULL;
List *pnote = NULL;
if(head == NULL)
{
printf("head is NULL!
");
return head;
}
new_head = head->p;
head->p = NULL;
do
{
pnote = new_head;
new_head = pnote->p;
pnote->p = head;
head = pnote;
}
while(new_head != NULL);
return pnote;
}
int main()
{
List *head = create_list();
print_list(head);
head = list_reverse(head);
print_list(head);
free_list(&head);
print_list(head);
printf("*** Debug test ***
");
return 0;
}