C++链表

C++链表,明天面试考前练习,主要熟悉一下环境,求面霸笼罩,,,

#include <iostream>
using namespace std;
typedef struct Node{
    int data;
    Node *next;
} Node;
int main()
{
    Node* head,*last,*temp;
    int num,i;
    cin>>num;
    for(i=0;i<=num;i++)
    {
        temp=new Node();
        temp->data=i;
        if(i==0)
            head=temp;
        else
            last->next=temp;
        temp->next=NULL;
        last=temp;
    }
    Node *p=head;
    while(p!=NULL)
    {
        cout<<p->data<<endl;
        p=p->next;
    }
    cout<<"hello success!"<<endl;
    return 0;
}

 输出三角形:

#include <iostream>
using namespace std;
int main()
{
    int num=9,i,j,k;
    for(i=1;i<=num;i++)
    {
        for(k=num-i;k>=0;k--)
            cout<<" ";
        for(j=0;j<i;j++)
        cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}

posted @ 2014-03-05 21:06  再见,少年  Views(188)  Comments(0Edit  收藏  举报