JamesLiuxy


私信TA

用户名:usernames

访问量:2809

签 名:

等  级
排  名 34128
经  验 406
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 哈尔滨信息工程学院
专  业

  自我简介:

TA的其他文章

解题思路:真是坑人啊!明明是对的就是提交了好几次。

注意事项:注意得有return 0;还有就是想开头说的开始必须得是空表。还有代码不能加注释的我就加了出错了。

参考代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct Line{
     struct Line *prior;
     int data;
     struct Line *next;
}line;

line *initList1(line *head){

   head=(line*)malloc(sizeof(line));
   head->prior=head;
   head->next=head;
//  line *list1=head;
// 
//   //构成环形双向链表 
//   for(int i=1;i<n;i++){
//       line *c=(line*)malloc(sizeof(line));
//       c->next=NULL;
//       c->prior=NULL;
//    
//       list1->next=c;
//       c->prior=list1;
//       list1=list1->next;
//   }
//   
 // list1->next=head;
 // head->prior=list1;
    
	return head;

}

line *insertList1(line *head,int data,int add)
{
	
	line *temp1=(line*)malloc(sizeof(line));
	temp1->data=data;
	temp1->prior=NULL;
	temp1->next=NULL;
	
	if(add==1)
	{   
	    line *t,*t1;
	    t=head;
	    while(t->next!=head){
		     t=t->next;
		}
		
		t->next=temp1;
		temp1->prior=t;
		temp1->next=head;
		head->prior=temp1;
		head=temp1;
	}
	else
	{
	
	line *body=head;
	
	for(int i=1;i<add-1;i++){
		body=body->next;
	}
	
	if(body->next==NULL)
	{
	   body->next=temp1;
	   temp1->prior=body;
	}
	else
	{
		body->next->prior=temp1;
		temp1->next=body->next;
		body->next=temp1;
		temp1->prior=body;
	}
	
	}

     return head;
}


line *deleteList1(line *head,int add){
         
		  line *temp=head;
          line *tail=head;
          
          if(add==1)
		  {
          	        line *t;
          	        t=head;
          	while(temp->next!=tail){
                      temp=temp->next;
			  }
			  
			  temp->next=t->next;
			  t->next->prior=temp;
			  free(t);
          	  head=temp->next;
		  }
		  else
		  {
		  for(int i=1;i<add;i++){
          	   tail=temp;
          	   temp=temp->next;
		  }
		  
		  	tail->next=temp->next;
		  	temp->next->prior=tail;
		  	free(temp);
		  	temp=tail->next;
	     }
	     
		return head;
			  	  
}


void display(line *head)
{
  
   line *temp;
   line *tail;
   temp=head;
   tail=head;
   
   while(temp->next!=tail){
   	  printf("%d ",temp->data);
   	  temp=temp->next;
   }
	printf("\n");
       
}

int main(){
	
	 line *p=NULL;
	 p=initList1(p);	 	
	int n;
	
   while(scanf("%d",&n)!=EOF){
            if(n==1){
            	int m,elem;
            	scanf("%d",&m);
            	scanf("%d",&elem);
            	p=insertList1(p,elem,m);
			} 
			else if(n==2){
			   int m1;
			   scanf("%d",&m1);
	           	p=deleteList1(p,m1);	   	   
			}
			else{
				display(p);
			}
   } 
   
   return 0;
   
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

这里头代码有点多有点乱。
2018-08-31 21:19:03
  • «
  • 1
  • »