⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xianxingbiaonizhi

📁 这是数据结构中实现线性表的逆置算法
💻
字号:

#include "stdio.h"
#include "malloc.h"

struct linknode			//定义结构体
{
    int data;
    struct linknode * next;
};

struct linknode *revercelink(struct linknode *head)
{	//逆置链表 
	struct linknode *p, *q, *r;
	r=p=head;
	q=p->next;
	p->next=NULL;
	while(q!=NULL)
	{
		p=q;
		q=q->next;
		p->next=r;
		r=p;
	}
	return p;
}

struct linknode *createlink()
{   //创建链表 
    struct linknode *p, *q;
    int i=0, a;
    printf("请输入表(以0结束):\n");
   	scanf("%d", &a);					//接收输入的数字 
	p=(struct linknode *)malloc(sizeof(struct linknode)); 	//开辟链表空间 
   	if(a==0)
   	{
		p=(struct linknode *)malloc(sizeof(struct linknode)); 	//开辟链表空间 
	   	p->data=a;
	   	p->next=NULL;
  	}
    while(a!=0)
    {
		p=(struct linknode *)malloc(sizeof(struct linknode)); 	//开辟链表空间 
        if(i==0)
		{	p->next=NULL;	}
			else
			{	p->next=q;	}
        p->data=a;		//向链表存数据 
        q=p;
        i++;
    	scanf("%d", &a);		//接收输入的数字 
	}
	p=revercelink(p);
    return p;
}

struct linknode *output(struct linknode *head)
{	//输出链表 
	struct linknode *p;
	p=head;
	while(p!=NULL)
	{
		printf("%d\n", p->data);
		p=p->next;
	}
	return head;
}

void start()
{	//选择操作 
	struct linknode *head, *p;
	head=createlink();
	printf("逆置前的表:\n"); 
	head=output(head);
	head=revercelink(head); 
	printf("逆置后的表:\n"); 
	head=output(head);
}

main()
{	
	start();	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -