链表

#include<stdio.h>
#include<stdlib.h>
struct node
{
  int data;
  struct node *next;
  
};
int main()
{
  struct node *p,*q,*t,*head;
  int i,n,a;
  head=NULL;
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    scanf("%d",&a);
    p=(struct node *)malloc(sizeof(struct node));
    p->data=a;
    p->next=NULL;
    if(head==NULL)
      head=p;
    else
      q->next=p;
    q=p;
    
  }
  t=head;
  while(t!=NULL){
    printf("%d ",t->data);
    t=t->next;
  }
  //链表的增添;
  int newass;
  scanf("%d",&newass);
  t=head;
  while(t!=NULL){
    if(t->next->data>newass)//链表需要在两个节点中间插入 
    {
      p=(struct node *)malloc(sizeof(struct node));
      p->data=newass;//存入新的节点 
      p->next=t->next;//先连尾巴 
      t->next=p;//再连头 
      break;
      
    }
    t=t->next;
  }

  t=head;
  while(t!=NULL){
    printf("%d ",t->data);
    t=t->next;
  }
} 
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>

using namespace std;
typedef struct node
{
  int date;
   node *next;
}node; 
int main()
{
  int a;
  node *p,*q,*head,*t,*bef;
  head=new node;  p=new node;  q=new node;  t=new node; bef=new node;
  head=p;
  while(scanf("%d",&a) && a!=0){
    q->date=a;
    q->next=NULL;
    p->next=q;
    p=q;
    q=new node;
    
  }
  t=head->next;
  while(t!=NULL){
    printf("%d ",t->date);
    t=t->next;
  }
} 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页