C++链表基础操作大全

目录:

  (1)建立有序链表,已0结束:输入一组数,输入为0,停止,按着从小到大的顺序输出

(2)倒序输出(链表):随机输入一组数据,当输入为0时结束,然后逆序输出

(3)在有序链表中加一个数,是链表成为创新的有序链表:

  (4) 输入N个数,再输入一个数,查找该数据最早的位置


*

(1)建立有序链表,已0结束:输入一组数,输入为0,停止,按着从小到大的顺序输出

样例输入:

3 6 5 2 9 4 0

样例输出:

2 3 4 5 6 9

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
void insert(int n);
int main()
{
int n;
cin>>n;
while(n!=0)
{
insert(n);
cin>>n;
}
while(head)
{
cout<<head->data<<" ";
head=head->next;
}cout<<endl;
}
void insert(int n)
{
node *s,*q,*p;
s=new node;
s->next=NULL;
s->data=n;
if(head==NULL)
{
head=s;return;     
}
if(head->data>s->data)
{
s->next=head;
head=s;
return;
}
for(q=head,p=head->next;p;q=p,p=p->next)
{
if(p->data>s->data)
{
s->next=p;
q->next=s;
return;
}
}
q->next=s;

return;

*********************************************************************************************************

(2)倒序输出(链表):随机输入一组数据,当输入为0时结束,然后逆序输出



#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};


void show(node *head)
{

while(head)
{
cout<<head->data<<" ";
head=head->next;
}cout<<endl;
}
int main()
{
node *s,*head;
head=NULL;
int n;
while(cin>>n)
{
if(n==0)
break;
s=new node;
s->data=n;s->next=NULL;
if(head==NULL)
head=s;

else 

{

s->next=head;head=s;
    }

}

show(head);

}

****************************************************************************************************************

(3)在有序链表中加一个数,是链表成为创新的有序链表:

先输入一组数的个数n,再输入这n个数据,然后再输入一个数。输出有序链表。

样例输入:

5

1 3 4 6 8

5

样例输出:

1 3 4 5 6 8

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};node *head=NULL;
void insert(int k)
{
node *t=head;
while(t->next&&t->next->data<k)
t=t->next;
node *s=new node;
s->data=k;
s->next=t->next;t->next=s;
}
void show(node *head)
{

while(head)
{
cout<<head->data<<" ";
head=head->next;
}cout<<endl;
}
int main()
{
 int n,m;node *s,*p;
 cin>>n;
 for(int i=0;i<n;i++)
 {
 cin>>m;
 s=new node;s->next=NULL;
 s->data=m;
 if(head==NULL)
 {
 head=s;p=s;
 }
 else {
       p->next=s;p=p->next;
      }
 }p->next=NULL;
 int l;cin>>l;
 insert(l);
 show(head);
}

*************************************************************************************************************

(4)输入N个数,再输入一个数,查找该数据最早的位置

样例输入:

5

2 5 6 4 3

6

样例输出:

3

#include<iostream>
using namespace std;
int j=1;
struct node
{
int data;
node *next;
};
node *head=NULL;
void insert(int k)
{
node *p,*s;p=new node;p->next=NULL;
if(head->data==k)
{
cout<<1<<endl;return;
}
else
{
p=head;
      while(p->next)
 {
 j++;
 if(p->next->data==k)
 {
cout<<j<<endl; break;
 }p=p->next;
 }
 
}

if(p->next==NULL)
 cout<<0<<endl;
}


int main()
{
int n,m,i=1;cin>>n;node *p,*s;
for(i;i<=n;i++)
{
cin>>m;
s=new node;
s->next=NULL;
s->data=m;
if(head==NULL)
{
head=s;p=s;
}
else {
p->next=s;p=p->next;
}
}p->next=NULL;
int k;cin>>k;
insert(k);
}





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值