链表的用法

久违的链表  mua mua mua !!!
#include<bits/stdc++.h>
using namespace std;

struct node{
	int data;    // 单链表的数据域 
	struct node *Next;  // 单链表的指针域 
};
 
/**
建立1:头插,逆序 
*/

node *creat1(int n){
	node *head;
	head=(node*)malloc(sizeof(node));
	head->Next=NULL;

	for(int i=1;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		int x;
		cin>>x;
		p->data=x;
		p->Next=head->Next;
		head->Next=p;
	}
	return head;
}

/**
建立2:尾插 ,顺序 
*/
node *creat2(int n){
	node *head;
	head=(node *)malloc(sizeof(node));
	head->Next=NULL;
	node *q;
	q=head;
	for(int i=1;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		int x;
		cin>>x;
		p->data=x;
		q->Next=p;
		q=p;
	}
	q->Next=NULL;
	return head;
}
/**
插入  位置:pos  插入val 
*/ 


node *add(node *head,int pos,int val,int n){
	node *pre;
	pre=head;
	for(int i=1;i<pos;i++){
		pre=pre->Next;
	}
	node *p;
	p=(node *)malloc(sizeof(node));
	p->data=val;
	p->Next=pre->Next;
	pre->Next=p;
	return head;
}


/**
删除 值为val的元素 
*/
node *Delete_val(node *head,int val){
	node *p,*pre;
	p=head->Next;
	pre=head;
	while(p->data!=val){
		pre=p;
		p=p->Next;
	} 
	pre->Next=p->Next;
	free(p);
	return head;
}

void print(node *p){
	p=p->Next;
	if(p) cout<<p->data;
	p=p->Next;
	while(p){
		cout<<" "<<p->data;
		p=p->Next;
	}
	cout<<endl;
}

/**
删除  位置 pos  
*/

node *Delete_pos(node *head,int pos){
	node *p,*pre;
	p=head->Next;
	pre=head;
	int i=1;
	while(i<pos){
		i++;
		pre=p;
		p=p->Next;
	}
	pre->Next=p->Next;
	free(p);
	return head;
}

int main (){
	int n;
	cin>>n;
	/**
	node *head ;
	head=creat1(n);
	print(head);
	*/
	node *head;
	head =creat2(n);
	//print(head);
	int m;
	cin>>m;
	for(int i=0;i<m;i++){
		/*int x,val;
		cin>>x>>val;
		x++;
		head=add(head,x,val,n);*/
		char ch[3];
		scanf("%s",ch);
		if(ch[0]=='D'){
			int st,ed;
		    cin>>st>>ed;
		    int k=ed-st+1;
		    while(k--){
			    head=Delete_pos(head,st);
		    }
		}
		else if(ch[0]=='I'){
			int st,sum;
			cin>>st>>sum;
			while(sum--){
				int val;
				cin>>val;
				head=add(head,++st,val,n);
			}
		}
		/**int st,ed;
		cin>>st>>ed;
		int k=ed-st+1;
		while(k--){
			head=Delete_pos(head,st);
		}*/
		
		/*char ch[3];
		scanf("%s",ch);
		if(ch[0]=='D'){
			int pos;
			cin>>pos;
			head=Delete_pos(head,pos);
		} 
		else if(ch[0]=='I'){
			int a,b;
			cin>>a>>b;
			head=add(head,a+1,b,n);
		}*/
		/**
		int pos;
		cin>>pos;
		head=Delete_pos(head,pos);*/
	}
	print(head);
	return 0;
} 




/**
双向链表建立:尾插 顺序 
*/
node *creat2(int n){
	node *head;
	head=(node *)malloc(sizeof(node));
	head->Next=NULL;
	head->pre=NULL;
	node *q;
	q=head;
	for(int i=1;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		int x;
		cin>>x;
		p->data=x;
		p->Next=NULL;
		p->pre=q;
		q->Next=p;
		q=p;
	}
	q->Next=NULL;
	return head;
}
#include<bits/stdc++.h>
using namespace std;

struct node{
	int data;    // 单链表的数据域 
	struct node *Next;  // 单链表的指针域 
};
 

/**
循环链表建立:尾插 顺序 
*/
node *creat2(int n){
	node *head;
	head=(node *)malloc(sizeof(node));
	head->data=1; 
	head->Next=NULL;
	node *q;
	q=head;
	for(int i=2;i<=n;i++){
		node *p;
		p=(node *)malloc(sizeof(node));
		p->data=i;
		p->Next=NULL;
		q->Next=p;
		q=p;
	}
	q->Next=head;
	return head;
}

int Count(node *head ,int n){
	node *p,*q;
	q=head;
	while(q->Next!=head) q=q->Next;
	for(int i=1;;i++){
		p=q->Next;
		if(i%5==0){
			if(p->data==1) return i/5;
			q->Next=p->Next;
		}
		else q=p;
	}
}

int main (){
	int n;
	while(cin>>n){
		if(n==0) break;
	    node *head;
	    head =creat2(n);
	    cout<<Count(head,n)<<endl;
	} 
	
	
	return 0;
} 


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值