要考二级,有链表的题目,学了一天觉得要记录一下,以我的记性,不记下来过几天肯定忘了
#include <stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}node;
node * createlist()
{ int i;
node *head,*p,*s;
head = (node*)malloc(sizeof(node));
p=head ;
for(i=0;i<3;i++){
s=(node *)malloc(sizeof(node));
s->data=5-i;
p->next=s;
p=s;
}
p->next=NULL;
return head;
}
void outlist(node *head){
node *p=head->next;
while(p!=NULL)
{
printf("->%d",p->data);
p=p->next;
}
}
int main() {
node *head=createlist();
outlist(head);
return 0;
}
用ppt弄了个动画 按我理解的意思大概就长这样吧。