数据结构&双向循环链表的基本操作(c/c++)

双向循环链表的基本操作:
创建,打印,反转,删除,添加。

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std; 
typedef struct mylist{
	int data;
	mylist *pre;
	mylist *next;
	
}list;

list *head;
list *tail;

list* createlist()
{
	char c;                 //use char to quit,assign list by convert char to int,
	head=(list *)malloc(sizeof(list));	
	tail=head;
	list* temp=head;
	cout<<"please input value of head"<<endl;; 
	cin>>c;
	head->data=(int)c-48; 		//set head,1 in assci  is 49
	while(1)
	{
		printf("plaese input a (int)value\n"); 
		printf("you can  input 'q' to stop create\n");
		cin>>c;
		if('q'==c)      //exit and  connect head to tail
		{	
			head->pre=tail;
		    tail->next=head; 
		    return head;
		}	    
		else
		{
				list *p; 
				p=(list *)malloc(sizeof(list));
				p->data=(int)c-48;
				temp->next=p;
				p->pre=temp;
	            temp=p;
				tail=p;
				p=NULL;			
				free(p);
		}
		    }
		
	
}
	
list* insertlist(int position,int value){
	int count=1;
	if(position<=0||head==NULL)              //list can  not be empty and the position must be bigger than 0
	{  
		printf("position<=0||head==NULL");
		return NULL;
	}
	list* p=(list*)malloc(sizeof(list));
	p->data=value;
	if(position==1)                 //insert head
	{    
		p->next=head;
		head->pre=p;
		p->pre=tail;
		tail->next=p; 
		head=p; 
		return head;
	}
	
	list *temp=head;
    while(temp->next!=tail)             // insert middle
    {  
    	count++;
    	temp=temp->next;
    	if(count==position)
    	{
    		p->pre=temp->pre;
    		p->next=temp;
    		temp->pre->next=p;
    		temp->pre=p;
			return head;
			}
		}
	//insert tail
	p->pre=tail;
	p->next=head;
	head->pre=p;
	tail->next=p;
	tail=p;
	return head;
	
}

int printlist(list *listhead){
	if(!listhead)                       //if list is empty
	{          
		cout<<"list is empty\n";
		return 0;
	}
	list* temp=listhead;
	while(temp!=tail)
	{
		printf("%d ",temp->data);
		temp=temp->next;
	}
	cout<<tail->data;
    cout<<endl;
	return 0;
} 

list* deletelist(int position)
{   int count=1;
   //list can  not be empty and the position must be bigger than 0
	if(position<=0||head==NULL)         
	{           
		printf("position<=0||head==NULL");
		return NULL;
	}
	list* temp=NULL;
	if(position==1)      //head
	{     
		temp=head->next;
		tail->next=temp;
		temp->pre=tail;
		free(head);
		head=temp;
		return head;
	}
	
	temp=head;
    while(temp->next!=tail)              // middle
    {   
    	count++;
    	temp=temp->next;
    	if(count==position){
    		temp->next->pre=temp->pre;
    		temp->pre->next=temp->next;
    		free(temp);
			return head;}
		}
	//tail
	temp=tail->pre;
	tail->pre->next=head;
	head->pre=tail->pre;
	free(tail);
	tail=temp;
	return head;
}

list* turnlist(list *listhead)
{
	if(!listhead)
	{
		cout<<"list is empty\n";
		return NULL;
	}
	list *current=head;
	list *temp=NULL;
	while(temp!=head)
	{ 
	  temp=current->next;
	  current->next=current->pre;
	  current->pre=temp;
	  current=temp;
	}
	temp=head;
	head=tail;
	tail=temp;
}
int main()
{
	int c;
	int posi,value;
	while(1){
		printf("1.create\n");
		printf("2.print\n");
		printf("3.insert\n");
		printf("4.delete\n");
		printf("5.turn\n");
		printf("6.exit\n");
		scanf("%d",&c);
	switch(c)
	{
		case 1:
			createlist();
			break;
		case 2:
			printlist(head);
			break;
		case 3:
			cout<<"please input position and value"<<endl;
			cin>>posi;
			cin>>value;
			insertlist(posi,value);
			break;
		case 4:
			cin>>posi;
			deletelist(posi);
			break;
		case 5:
			turnlist(head);
			break;
		case 6:
			return 0;
		default:
			printf("there's not this option\n");
			break;	
	}
	}
	}

results of execution

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值