为什么这个链表步也不能实现?
下面这个程序是创造一个小链表,我输入数字进去,但似乎没有进入子函数操作,怎么回事呢?在这个程序中我知道在 主函数的struct student head;与子函数的struct student *current, *prev;前面加上static可以实现输出。但是我不明白为什么我不加这个statci就似乎没有进入子函数,因为一步也没有输出。
我的问题主要是问为什么似乎没有执行子函数?因为我随便输入一个数就终止了。
12
Press any key to continue
#include "console.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
struct student
{
int age;
struct student *next;
};
void Initialize(struct student *head);
void Create(int item, struct student **head,struct student **prev);
void Show(struct student *head);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
struct student *head,*prev;
//static struct student *head;
int num;
head=NULL;
while(scanf("%d", &num))
{
if (0==num){break;}
Create(num, &head,&prev);
}
Show(head);
scanf("%d", &num);
return 0;
}
void Initialize(struct student *head)
{
head=NULL;
}
void Create(int item, struct student **head,struct student **prev)
{
struct student *current;
//static struct student *current, *prev;
current = (struct student *)malloc(sizeof(struct student));
current->age = item;
current->next = NULL;
if((*head) == NULL)
{
(*head) = current;
(*prev) = current;
}
else
{
(*prev)->next = current;
(*prev) = current;
}
}
void Show(struct student *head)
{
struct student *current;
current = head;
while(1)
{
printf("%d ", current->age);
if (current->next==NULL)
{break;}
current = current->next;
}
printf("\n");
}
Initialize(head);
void Initialize(struct student *head)
{
head = NULL;
}
if((*head) == NULL)
{
(*head) = current;
prev = current;
}
else
{
prev->next = current;
prev = current;
}
#include<stdio.h>
#include<tchar.h>
#include<stdlib.h>
// The one and only application object
struct student
{
int age;
struct student *next;
};
void Initialize(struct student *head);
void Create(int item, struct student **head,struct student **prev);
void Destory(struct student **head);
void Show(struct student *head);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
struct student *head,*prev;
//static struct student *head;
int num;
//Initialize(&head);
head=NULL;
while(scanf("%d", &num))
{
if (0==num){break;}
Create(num, &head,&prev);
}
Show(head);
scanf("%d", &num);
Destory(&head);
return 0;
}
void Initialize(struct student **head)
{
*head=NULL;
}
void Destory(struct student **head){
if(!head)return ;
struct student *p=*head, *q;
while(p)
{
q = p;p = p->next ;
free(q);
}
*head =NULL;
};
void Create(int item, struct student **head,struct student **prev)
{
struct student *current;
//static struct student *current, *prev;
current = (struct student *)malloc(sizeof(struct student));
current->age = item;
current->next = NULL;
if((*head) == NULL)
{
(*head) = current;
(*prev) = current;
}
else
{
(*prev)->next = current;
(*prev) = current;
}
}
void Show(struct student *head)
{
struct student *current;
current = head;
while(1)
{
printf("%d ", current->age);
if (current->next==NULL)
{break;}
current = current->next;
}
printf("\n");
}