链表(啊哈!算法)

指针的作用用来存储地址

int a;

int *p; 定义一个存储整型变量的地址的指针

p=&a;  保存a的地址;

pritnf("%d," *p);   输出p存储地址的值

*的作用{1.表乘法 2.申明一个指针,在定义指针变量时使用 3.间接运算符,取得指针所指向的内存中的值}

#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
    int date;
    struct node *next;
};
int main(){
    int n,date,number;
    struct node *head,*q,*p,*t;
    head=NULL;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>date;
        p=(struct node *)malloc(sizeof(struct node));
        p->date=date;
        p->next=NULL;
        if(head == NULL){
            head=p;
        }else{
            q->next=p;
        }
        q=p;
    } 
    t=head;
    while(t!=NULL){
        cout<<t->date<<' ';
        t=t->next;
    }
    //Insert a number 
    cin>>number;
    t=head;
    while(t!=NULL){
        if(t->next->date>number){
            p=(struct node*)malloc(sizeof(struct node));
            p->date=number;
            p->next=t->next;
            t->next=p;
            break;
        }
        t=t->next;
    }
    cout<<endl;
    t=head;
    while(t !=NULL){
        cout<<t->date<<" ";
        t=t->next;
    } 
    return 0;
}

 

posted on 2020-03-28 11:21  二进制dd  阅读(236)  评论(0编辑  收藏  举报

导航