#include "stdio.h"
#include "stdlib.h"
typedef struct linkList{
int data;
struct linkList* next;
}*linkList, listNode;
linkList reserveList(linkList list);
linkList autoCreateList();
linkList autoCreateList(){
int data[4] = {1, 2, 3, 4};
int i;
linkList list = (linkList)malloc(sizeof(listNode));
linkList temp;
listNode* newNode;
list->data = data[0];
list->next = NULL;
temp = list;
for(i=1; i<4; i++){
newNode = (listNode*)malloc(sizeof(listNode));
newNode->data = data[i];
temp->next = newNode;
temp = newNode;
}
temp->next = NULL;
return list;
}
linkList reserveList(linkList list){
listNode *next, *prev;
next = NULL;
prev = NULL;
if(list==NULL){
return NULL;
}
while(list!=NULL){
next = list->next;
list->next = prev;
prev = list;
list = next;
}
return prev;
}
int main(){
linkList list;
list = autoCreateList();
list = reserveList(list);
while(list!=NULL){
printf("%d ", list->data);
list = list->next;
}
return 0;
}
【算法入门】反转单链表(C语言实现)
最新推荐文章于 2024-08-27 12:51:30 发布