#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define LIST_NUM (5) //定义创建链表长度
struct LNode {
int data;
struct LNode *next;
}; //链表结构体
/*********************创建链表**********************/
struct LNode *Create(int num)
{
int data;
int i;
struct LNode *head, *new, *temp;
head = NULL;
for(i = 0; i < num; i++) {
new = (struct LNode*)malloc(sizeof(struct LNode));
printf("%s: input intger:", __func__);
scanf("%d", &data);
new->data = data;
if(head == NULL) {
head = new;
temp = new;
} else {
temp->next = new;
temp = new;
}
}
temp->next = NULL;
return head;
}
/**********************打印链表信息*******************/
void Print(struct LNode *head)
{
struct LNode *current;
current = head;
printf("%s: output LNode: ", __func__);
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
putchar('\n');
}
/**********************判断链表是否为空****************/
int Isempty(struct LNode *head)
{
if(head->next == NULL) {
return 1;
} else {
return 0;
}
}
/*********************往链表中增加节点******************/
struct LNode *Insert(struct LNode *head, int pos, int data)
{
int i;
struct LNode *current, *new, *temp;
new = (struct LNode*)malloc(sizeof(struct LNode));
if(new == NULL) {
printf("%s: malloc error\n", __func__);
}
new->data = data;
current = head;
if( (pos > 0) && (pos < Getlen(head)) ) {
for(i = 0; i < pos; i++) {
current = current->next;
}
temp = current->next;
current->next = new;
new->next = temp;
}
if(pos == 0) {
new->next = current;
head = new;
}
return head;
}
/*********************删除链表中的某个节点**************/
struct LNode *Delte(struct LNode *head, int pos)
{
int i;
struct LNode *current, *temp;
current = head;
if( (pos > 0) && (pos < Getlen(head)) ) {
for(i = 0; i < pos; i++) {
temp = current;
current = current->next;
}
temp->next = current->next;
free(current);
}
if(pos == 0) {
head = current->next;
free(current);
}
return head;
}
/********************使用递归逆置链表******************/
struct LNode *Conver(struct LNode *head)
{
struct LNode *current, *temp;
if(head->next == NULL) {
printf("%s: end\n", __func__);
return head;
}
current = Conver(head->next);
temp = head->next;
temp->next = head;
head->next = NULL;
printf("%s: temp and current\n", __func__);
return current;
}
/*****************获取链表长度************************/
int Getlen(struct LNode *head)
{
int len = 0;
struct LNode *current;
current = head;
while(current->next != NULL) {
len++;
current = current->next;
}
if(len) {
len++;
}
return len;
}
int main(int argc, char **argv)
{
struct LNode *head, *current;
head->next = NULL;
if(Isempty(head)) {
printf("%s: head is empty\n", __func__);
printf("%s: list len is %d\n", __func__, Getlen(head));
} else {
printf("%s: head is exist\n", __func__);
printf("%s: list len is %d\n", __func__, Getlen(head));
}
head = Create(LIST_NUM);
if(Isempty(head)) {
printf("%s: head is empty\n", __func__);
printf("%s: list len is %d\n", __func__, Getlen(head));
} else {
printf("%s: head is exist\n", __func__);
printf("%s: list len is %d\n", __func__, Getlen(head));
}
current = head;
Print(current);
current = Insert(head, 0, 10); //插入数据为10的数据作为链表头
Print(current);
current = Delte(current, 5); //删除第五个数据(0开始)
Print(current);
current = Conver(current); //逆置
Print(current);
return 0;
}
链表的递归倒置,程序
最新推荐文章于 2021-05-24 09:03:36 发布