首页 > 其他 > 详细

头插法实现链表逆置

时间:2020-06-18 22:49:17      阅读:144      评论:0      收藏:0      [点我收藏+]
 /*头插法实现单链表逆置*/
#include<stdio.h>
#include<malloc.h>
typedef struct LNode
{
	char data;
	struct LNode *next;
}LNode,*LinkList;
bool InitList(LinkList &L)
{
	L=(LNode*)malloc(sizeof(LNode));
	if(L==NULL)
		return false;
	L->next=NULL;
	return true;
}
LinkList head_InsertList(LinkList &L,char e)
{
	LNode *s=(LNode*)malloc(sizeof(LNode));
	s->data=e;
	s->next=L->next;
	L->next=s;
	return L;
}
LinkList Translate(LinkList &L)
{
	if(L==NULL)
		return NULL;
	LNode *p=L->next,*q;
	q=(LNode*)malloc(sizeof(LNode));
	q->next=NULL;
	while(p!=NULL)
	{
		head_InsertList(q,p->data);
		p=p->next;
	}
	L=q;
}
void Print(LinkList L)
{
	LNode *p=L->next;
	while(p!=NULL)
	{
		printf("%c	",p->data);
		p=p->next;
	}
}
void main()
{
	LinkList L;
	InitList(L);
	head_InsertList(L,‘a‘);
	head_InsertList(L,‘b‘);
	head_InsertList(L,‘c‘);
	head_InsertList(L,‘d‘);
	head_InsertList(L,‘e‘);
	Print(L);	
	printf("\n********************\n");
	Translate(L);
	Print(L);
	printf("\n********************\n");
}

  

头插法实现链表逆置

原文:https://www.cnblogs.com/-slz-2/p/13160499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!