链表
作者:
xxw从头再来
,
2024-03-12 20:31:24
,
所有人可见
,
阅读 40
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct node{
int dat;
struct node *next;
}nod;
int main()
{
nod *head = NULL,*p,*q,*t;
int n,a;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a;
p = new(nod); //分配内存
p->dat = a;
p->next = NULL;
if(head ==NULL)
head = p;
else
q->next = p;
q = p;
}
cin>>a;
t = head;
while(t!=NULL)
{
if(t->next->dat >a)
{
p = new(nod);
p->dat = a;
p->next = t->next;
t->next = p;
break;
}
t = t->next;
}
t = head;
while(t!=NULL)
{
cout<<t->dat<<" ";
t=t->next;
}
return 0;
}