俊介三

一天更新一点,一天积累一点

导航

单链表

Posted on 2013-03-23 19:26  俊介三在前进  阅读(135)  评论(0)    收藏  举报

单链表就是每个Node都有一个数据项和一个指向下一个Node的指针。优点显而易见:便于删除某个节点、添加某个节点;存储这个结构不需要连续的地址。

其中一个例子是:页帧的管理虚拟内存技术是一种地址空间的映射机制,它允许进程不必完全加载到物理内存也能运行。进程以为自己使用内存连续的地址,实际上内存为每个进程都配备了页表,它把进程以为的地址(连续的虚拟地址)和实际的物理地址(一般都是零零散散的吧)一一对应起来。但这样,当进程引用某个虚拟地址,根据这个页表查找,发现不在物理页帧上,就会产生页错误并为之在物理内存中分配一个页帧,至于什么进程页何时移除物理内存,又是另一个问题,有相应的机制。

单链表的基本操作:

#include <stdio.h>
#include <stdlib.h>

struct List{
    int data;
    List* next;
    List(){}
    List(int d){
        data = d;
        next = NULL;
    }
    List(int d, List* list){
        data =d;
        next = list;
    }
};

//append an element to tail
List* appendToTail(int data, List* head){
    List* tail = new List(data,NULL);
    List* p = head;
    while(p->next!=NULL){
        p = p->next;
    }
    p->next = tail;
    return head;
}

//print the linked list
void print(List* head){
    List* p = head;
    while(p!=NULL){
        if(p->next==NULL){
            printf("%d\n",p->data);
        }else{
            printf("%d -> ",p->data);
        }
        p = p->next;
    }
    return;
}

//release the memory occupied by the linked list
void destroy(List* head){
    List* p = head;
    if(p==NULL) return;
    if(p->next==NULL){
        delete p;
    }else{
        destroy(p->next);
        delete p;
    }
}

//another way to release the linked list, without recursive;
void destroy2(List* head){
    int count = 0;
    List* p =head;
    if(p==NULL) return;
    while(p!=NULL){
        List* temp = p;
        p = p->next;
        delete temp;
        count++;
    }
    printf("delte %d node(s).\n",count);
}

//insert a node after a given node
void insert(int data, List* node){
    List*temp = new List(data);
    if(node->next==NULL){
        node->next = temp;
    }else{
        temp->next = node->next;
        node->next = temp;
    }
}

//insert a node to the head(before the head)
List* insertToHead(int data, List* head){
    List*temp = new List(data, head);
    return temp;
}

//calculate the size of the linked list
int size(List* head){
    if(head==NULL) return 0;
    List* p = head;
    int count =0;
    while(p!=NULL){
        p = p->next;
        count++;
    }
    return count;
}

//delete a node. success it returns true and the like.
bool del(int data, List* head){
    List* p = head;
    if(p->data==data){
        p = head->next;
        delete head;
        return true;
    }else{
        List* pre = p;
        p = p->next;
        while(p!=NULL){
            if(p->data==data){
                pre->next = p->next;
                delete p;
                return true;
            }else{
                pre = p;
                p = p->next;
            }
        }
        return false;
    }
}

int main(){
    List* node = new List(12);
    List* head = appendToTail(23,node);
    head = appendToTail(14,head);
    insert(90,head);
    head = insertToHead(1,head);
    printf("delte a node is %s\n", del(12,head)?"success":"failed");
    print(head);
    printf("The size of this linked list is %d\n",size(head));
    destroy2(head);
    return 0;
}