码迷,mamicode.com
首页 > 其他好文 > 详细

list

时间:2017-12-09 22:24:34      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:name   tno   include   ++   div   for   creat   std   insert   

#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct ListNode{
	int data;
	ListNode* pre;
	ListNode* nxt;
}List;
int length;

List* CreateList(int len)
{
	List* ret=(List*)malloc(sizeof(List));
	List* tail=ret;
	ret->pre=ret,ret->nxt=ret,ret->data=1;
	for(int i=2;i<=len;i++)
	{
		List* newnode=(List*)malloc(sizeof(List));
		newnode->data=i,
		tail->nxt=newnode,newnode->pre=tail,newnode->nxt=ret,tail=newnode;
	}
	ret->pre=tail;
	length=len;
	return ret;
}
void Delete(List* pos)
{
	pos->pre->nxt=pos->pre;
	pos->nxt->pre=pos->nxt;
	free(pos);
	length--;
}
List* JumpToItem(List* list,int n)
{
	if(n==0) return list;
	while(n--)
		list=list->nxt;
	return list;
}
void Print(List* list)
{
	printf("%d ",list->data);
	for(int i=2;i<=length;i++)
	{
		list=list->nxt;
		printf("%d ",list->data);
	}
	printf("\n");
}
void InsertAft(List* pos,int data)
{
	List* newnode=(List*)malloc(sizeof(newnode));
	newnode->data=data,
	newnode->pre=pos,newnode->nxt=pos->nxt,pos->nxt=newnode;
	length++;
}
void InsertBef(List* pos,int data)
{
	pos=pos->pre;
	InsertAft(pos,data);
}
int main()
{
	List* list=CreateList(15);
	Print(list);
	Delete(list);
	Print(JumpToItem(list,1));
	InsertAft(list,233);
	Print(JumpToItem(list,1));
	InsertBef(JumpToItem(list,2),2333);
	Print(JumpToItem(list,1));
	return 0;
}

  

list

标签:name   tno   include   ++   div   for   creat   std   insert   

原文地址:http://www.cnblogs.com/TheRoadToAu/p/8012755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!