码迷,mamicode.com
首页 > Windows程序 > 详细

链表API

时间:2017-12-13 19:51:21      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:tar   let   span   nbsp   main   size   class   nodelist   color   

#include <iostream>
#include <stdio.h>

using namespace std;

#define size 1000

struct node{

    int num;
    node *next;
    node *pre;

};

node NodeListPool[size];
int index = 0;
node nil;
node *NIL;

void init()
{
    NIL = & nil;
    NIL->next = NIL;
    NIL->pre = NIL;
    NIL->num = -100;

}

node *getNewNode()
{
    return &NodeListPool[index++];
}

void insert(node *target, node *newNode)
{
    newNode->next = target->next;
    newNode->pre = target;
    target->next = newNode;
    newNode->next->pre = newNode;

}

node *search(int key)
{
    node *tmp = NIL->next;
    while(tmp != NIL && tmp ->num != key)
    {
        tmp = tmp -> next;
    }

    return tmp;
}

void deleteNode(node *node)
{
    node->pre->next = node->next;
    node->next->pre = node->pre;
}

int main()
{
    init();
    node *tmpNode = NIL;
    for(int i = 0; i < 100 ;i++)
    {
        int tmp = rand()%100 + 1;
        cout << tmp << endl;
        node *newNode = getNewNode();
        newNode ->num = tmp;
        insert(tmpNode,newNode);
        tmpNode = tmpNode->next;
    }

    cout << "-------------------------------------"<< endl;

    tmpNode = NIL;
    while(tmpNode->next != NIL)
    {
        cout << tmpNode->next->num << endl;
        tmpNode = tmpNode->next;
    }
    return 0;
}

 

链表API

标签:tar   let   span   nbsp   main   size   class   nodelist   color   

原文地址:http://www.cnblogs.com/zyqBlog/p/8033834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!