C++ 链表

编程实现一个单链表建立、测长、打印、插入、删除。

#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct student{
 int data;
 struct student *next;
}node;
int x,cycle = 1;
node* create(){//create a list
    node *head,*p,*s;
    head = new node;//(node*)malloc(sizeof(node));
    head->data = -1;
    p = head;
    return (head);
}
int length(node *head){//calculate the length of a list
    int n =-1;
    node *p;
    p = head;
    while(p!=NULL){
        p = p->next;
        n++;
    }
    return n;
}
void print(node* head){//print list
    node* p;
    int n;
    n = length(head);
    cout<<"
NOW, these "<<n<< " records are :
";
    p = head;
    if(head != NULL)
        while(p!=NULL){
            cout<<p->data<<" ";
            p = p->next;
        }
}
bool delnode(node* head, int index){//reture true if delete successfully ;return false if the index isn't in the list
    node* p = head;
    node* p_front = p;
    p = p->next;
    int count = 0;
    while(p!=NULL){
        if(count == index){
            p_front->next = p->next;
            delete p;
            return true;
        }
        count++;
        p_front = p;
        p = p->next;
    }
    return false;
}
bool addnode(node* head,int index,int value){//sucess:true;failure:false
    node* p = head;//if there is a guard in the head of the list ,the cases is rare and sample.my list has a guard whose data is -1;
    node* p_front = p;
    p = p->next;
    int count = 0;
    node* node_add = NULL;
    if(p == NULL){//the case of inserting into a empty list
        node_add = new node;
        node_add->data = value;
        node_add->next = NULL;
        p_front->next = node_add;
    }
    while(p!=NULL){//inserting into normal list: insert into the middle(head),tail 
        if(count == index){
            node_add = new node;
            node_add->data = value;
            node_add->next = p_front->next;
            p_front->next = node_add;
            return true;
        }
        count++;
        p_front = p;
        p = p->next;
    }
    if(count == index){//the possible cases is one more than the number of its element,so I must take a extra attention of it(inserting int to the tail) 
        node_add = new node;
        node_add->data = value;
        node_add->next = p;
        p_front->next = node_add;
        return true;
    }
    return false;
}
void dellist(node *head){//recycle the menery
    node* p;
    while(head!=NULL){
        p = head;
        head = head->next;
        cout<<"
recycle:"<<p->data;
        delete p;//free(p);
    }
}
int main(){
    node* list = create();
    node *p = list;
    node *s=NULL;
    int num[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
    int count_num =0;
    while(cycle){
        cout<<"
please input the data:";
        x = num[count_num++];//cin>>x;
        if(x!=0){
            s = new node;//(node*)malloc(sizeof(node));
            s->data = x;
            s->next = NULL;
            cout<<"
"<<s->data;
            p->next = s;
            p = s;
        }
        else{
            cycle = 0;
        }
    }
    print(list);
    int a = length(list);
    cout<<"
length:"<<a;
    //delete a node according to its index
    delnode(list,10);
    delnode(list,0);
    delnode(list,12);
    delnode(list,20);
    print(list);
    //add a node according to its index
    addnode(list,10,11);
    addnode(list,13,15);
    print(list);
    dellist(list);
    getchar();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/yuanzhenliu/p/5308411.html