C单链表的建立,打印,反转
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
} Node;
Node* CreateList()
{
int length;
printf("请输入链表长度:\n");
scanf("%d",&length);
Node* head=(int*)malloc(sizeof(int));
Node* last;
head->data=length;
head->next=NULL;
last=head;
printf("请输入链表各元素:\n");
int i=0;
while(i<length)
{
Node* q=(int*)malloc(sizeof(int));
scanf("%d",&q->data);
q->next=NULL;
last->next=q;
last=q;
i++;
}
return head;
}
void PrintList(Node* head)
{
Node* p=head->next;
printf("打印链表:\n");
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
void ReverseList(Node* head)
{
Node* p=head->next;
head->next=NULL;
while(p!=NULL)
{
Node* q=(int*)malloc(sizeof(int));
q->data=p->data;
q->next=head->next;
head->next=q;
p=p->next;
}
}