前插与后插

#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct lnode
{
	ElemType data;
	lnode* next;
}lnode, *linklist;
//前插法创建单链表
void creatlist_h(linklist &l,int n)
{
	l = new lnode;
	l->next = NULL;
	for (int i = 0; i < n; i++)
	{
		linklist p;
		p = new lnode;
		 cin >> p->data;
		 p->next = l->next; 
		 l->next = p;
	}
}
//后插法创建单链表
void creatlist_r(linklist &l,int n)
{
	l = new lnode;
	linklist r;
	l->next = NULL;
	r = l;
	for (int i = 0; i < n; i++)
	{
		linklist p;
		p = new lnode;
		cin >> p->data;
		p->next = NULL;
		r->next = p;
		r = p;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43838785/article/details/89646374