linklist.cpp

来自「单链表倒序」· C++ 代码 · 共 64 行

CPP
64
字号
#include <stdio.h>
#include <stdlib.h>
struct celltype
{
	int element;
	celltype *next;
};
typedef celltype* position;
typedef celltype* DLIST;
void HEADandTAIL(position &l)
{
	l=(position)malloc(sizeof(struct celltype));
	l->element=0;
	l->next=l;
}
	
void INSERT (int x, position &p)
{
	position TMP;
	TMP=(position)malloc(sizeof(struct celltype));
	TMP->element=x;
	TMP->next=p->next;
	p->next=TMP;
}
int main()
{
	DLIST l, pst;
	int t;
	int bs[4]={5,6,7,8};
	HEADandTAIL(l);
	pst=l;
	printf("本程序链表的长度为6, 已知\n");
	for(t=0; t<4; t++)
	{
		INSERT(bs[t], pst);
		pst=pst->next;
		l->element++;
	}
	pst=l;
	for(t=0; t<l->element; t++)
	{
		printf("%d ", pst->next->element);
	    pst=pst->next;
    }
	printf("\n这是原顺序\n以下是反向后的顺序\n");
	l->next->next->next->next->next=l->next->next->next;
    l->next->next->next->next=l->next->next;
    l->next->next->next=l->next;
    l->next->next=l;
    l->next=pst;	
	pst=l;
	for(t=0; t<l->element; t++)
	{
		printf("%d ", pst->next->element);
	    pst=pst->next;
    }
	printf("\n");
	return 0;
}




⌨️ 快捷键说明

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