非循环双链表创建打印头插法和尾插法

编译环境vs2015

//头插法

#include 
#include 

typedef struct listinfo *NODE;
typedef struct listinfo node;
typedef struct listinfo
{
	int data;
	node *pnext;
	node *piror;
};

/*初始化链表*/
NODE listInit(NODE phead)
{
	phead = (NODE)malloc(sizeof(node));
	phead->data = 0;
	phead->piror = NULL;
	phead->pnext = NULL;
	return phead;
}

//创建双链表并添加数据,后续节点为NULL,所以只需要处理前驱节点,将新节点和前驱节点连接起来就可以了。
NODE insertlist(NODE phead)
{
	int i, insernum, idata;
	NODE pnew,pend;
	pend = phead->pnext;
	printf("双链表创建节点数据\n");
	printf("录入节点数:");
	scanf("%d", &insernum);
	for (i = 0; i < insernum; i++)
	{
		printf("数据:");
		scanf("%d", &idata);
		pnew = (NODE)malloc(sizeof(node));
		
		pnew->data = idata;
		pnew->pnext = NULL;
		pnew->piror = phead->pnext;//1.先把数据插入首节点
		
		if (phead->pnext != NULL)//后继节点不为NULL时,新节点和后续节点接上
		{
			pnew->pnext = pend->pnext;//2.
			pend->pnext->piror = pnew;//3.
			pend = pnew;//链接节点
		}
		
		phead->pnext = pnew;//4.首节点接上
		pnew->piror = phead;//5.新节点前驱节点接上首节点
		pend = phead;//链接节点
	}
	return  phead;
}
/*打印链表*/
void nodeprit(NODE phead)
{
	NODE temp;
	temp = phead->pnext;
	/*顺序遍历*/
	printf("后继节点遍历\n");
	while (temp != NULL)
	{
		printf("%d ", temp->data);
		if (temp->pnext == NULL)
		{
			break;
		}
		temp = temp->pnext;
	}
	/*前驱节点遍历*/
	printf

你可能感兴趣的:(C语言,双链表,非循环双链表,头插法,尾插法,C语言)