链表反转

链表基本应用

链表创建
链表插入
链表删除
链表修改
链表查询
链表反转
完整代码

截图

image

链表创建

typedef struct Node
{
	int data;
	struct Node *next;

}Node;

Node *CreateNode()
{
	Node *head=malloc(sizeof(Node));
	head->next=NULL;
	head->data=0;

	return head;
}
}

链表插入

Node* AddNode(Node* head, int data)//增
{
	Node* p = malloc(sizeof(Node));
	p->data = data;
	p->next = head->next;
	head->next = p;
	return head;
}

链表删除

void Delete_List(Node **head)//删
{
    Node *p;
    while(*head!=NULL)
    {
        p=*head;
        *head=(*head)->next;
        free(p);
        p=NULL;
    }
}

链表修改

Node *Revise_List(Node *head,int i,int j)//改
{
    Node *p=head;
    if(head->next==NULL)
    {
        printf("链表为空\n");
        return NULL;
    }
    while(head){
        if(head->data==i)
        {
            head->data=j;
        }
    }
    head=p;
    return head;
}

链表查询

int ReadNode(Node *head,int data)//查
{
    while(head)
    {
        if(head->data==data)
        return 0;
        head=head->next;
    }
    return 1;
}

链表反转

Node *RollbackNode(Node *head)//反转
{
		Node* swap = NULL;
	Node* phead = NULL;
	Node* head1 = head;
	head = head->next;
	while (head)
	{
		phead = swap;
		swap = head;
		head = head->next;
		swap->next = phead;
	}
	swap->next = phead;
	phead = swap;

	head1->next = phead;
	phead = head1;
	return phead;
}

完整代码

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
	int data;
	struct Node *next;

}Node;

Node *CreateNode()
{
	Node *head=malloc(sizeof(Node));
	head->next=NULL;
	head->data=0;

	return head;
}

Node* AddNode(Node* head, int data)//增
{
	Node* p = malloc(sizeof(Node));
	p->data = data;
	p->next = head->next;
	head->next = p;
	return head;
}

void Delete_List(Node **head)//删
{
    Node *p;
    while(*head!=NULL)
    {
        p=*head;
        *head=(*head)->next;
        free(p);
        p=NULL;
    }
}
Node *Revise_List(Node *head,int i,int j)//改
{
    Node *p=head;
    if(head->next==NULL)
    {
        printf("链表为空\n");
        return NULL;
    }
    while(head){
        if(head->data==i)
        {
            head->data=j;
        }
    }
    head=p;
    return head;
}

int ReadNode(Node *head,int data)//查
{
    while(head)
    {
        if(head->data==data)
        return 0;
        head=head->next;
    }
    return 1;
}

Node *RollbackNode(Node *head)//反转
{
		Node* swap = NULL;
	Node* phead = NULL;
	Node* head1 = head;
	head = head->next;
	while (head)
	{
		phead = swap;
		swap = head;
		head = head->next;
		swap->next = phead;
	}
	swap->next = phead;
	phead = swap;

	head1->next = phead;
	phead = head1;
	return phead;
}

void showNode(Node* head)
{
	head = head->next;
	while (head)
	{
		printf("%d ", head->data);
		head = head->next;
	}
	printf("\n");
}

int main()
{
	Node *head=CreateNode();
	head=AddNode(head, 1);
	head=AddNode(head, 2);
	head=AddNode(head, 3);
	head=AddNode(head, 4);
	
	showNode(head);
    head=RollbackNode(head);//反转
    
	showNode(head);
	return 0;
}
posted @ 2022-05-31 13:04  xMonody  Views(32)  Comments(0Edit  收藏  举报