c和c++单链表

c++版
#include<iostream>
#include<malloc.h>
using namespace std;
struct node{
    int data;
    node *next;
    node(){
        next=NULL;
    }
};
node *head;
void clist(){
    head=(node *)malloc(sizeof(node));
    head->next=NULL;
}
void create(int i){
    node *p,*q;
    q=(node *)malloc(sizeof(node));
    p=head;
    while(i--){
        cin>>q->data;
        p->next=q;
        p=q;
        q=(node *)malloc(sizeof(node));
    }
}
void display(){
    node *p;
    p=head->next;
    while(p){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}
void clist1(){
    node *p;
    while(head){
        p=head->next;
        free(head);
        head=p;
    }
}
int getline(){
    int length=0;
    node *p=head->next;
    while(p){
        length++;
        p=p->next;
    }
    return length;
}
int find(int e){
    node *p=head->next;
    while(p){
        if(p->data==e) return 1;
        p=p->next;
    }
    return 0;
}
node *getnode(int i){
    if(i<0||i>getline()){
        cout<<"no"<<endl;
        throw i; 
    }
    node *p=head;
    while(p&&i){
        p=p->next;
        i--;
    }
    return p;
}
void insert(int i,int e){
    node *p;
    node *q;
    q=(node *)malloc(sizeof(node));
    q->data=e;
    if(i==1){
        q->next=head->next;
        head->next=q;
    }
    else{
        p=getnode(i-1);
        if(i==getline())
        p->next=q;
        else{
            q->next=p->next;
            p->next=q;
        }
    }
}
void Delete(int e){
    if(!find(e))
    {
        cout<<"没这个数"<<endl;
        return;
    }
    node *p=head;
    node *q=head->next;
    while(q){
        if(q->data==e)
        break;
        
        p=p->next;
        q=q->next;
    }
    p->next=q->next;
    return;
}
bool isempty(){
    return head->next==NULL;
}
void reverse(){
    if(isempty()){
        cout<<"为空"<<endl;
    }
    node *p,*q;
    int len=getline();
    int i=1;
    int j=len;
    while(i<j){
        p=getnode(i);
        q=getnode(j);
        int temp=p->data;
        p->data=q->data;
        q->data=temp;
        ++i;
        --j;
    }
}
int main()
{
    clist();
    int g;
    cout<<"输入你链表里有几个数";
    cin>>g;
    create(g);
    display();
    cout<<"长度为"<<getline()<<endl;
    cout<<"输入你要查找的数"<<endl;
    int t;
    cin>>t;
    if(find(t)) cout<<""<<endl;
    else cout<<"没有"<<endl;
    cout<<"输入你要插入的位置和数"<<endl;
    int a,b;
    cin>>a>>b;
    insert(a,b);
    display();
    cout<<"输入你要删除的数"<<endl;
    cin>>t;
    Delete(t);
    display(); 
    cout<<"翻转后的情况为"<<endl;
    reverse();
    display(); 
    return 0;
}

#include<iostream> #include<cstring> using namespace std; class cnode{ public : int data; cnode *next; cnode() { next=NULL; } }; class clist{ private: cnode *head; public: clist(); void create(); void display(); ~clist(); int getlin() const; bool isempty()const; bool find(const int e) const; cnode *getnode(int i) const; void insert(int i,const int e); void Delete(const int e); void reverse(); }; clist::clist(){ head=new cnode(); head->next=NULL; } void clist::create() { cnode *p,*q; p=head; q=new cnode(); cout<<"请输入值ctrl+z停止:"<<endl; while(cin>>q->data) { p->next=q; p=q; q=new cnode(); } } void clist::display() { cnode *p; p=head->next; while(p) { cout<<p->data<<" "; p=p->next; } cout<<endl; } clist::~clist() { cnode *p; while(head) { p=head->next; delete head; head=p; } } int clist::getlin() const { int length=0; cnode* p=head->next; while(p) { length++; p=p->next; } return length; } bool clist::isempty()const { return (head->next==NULL); } bool clist::find(const int e)const { cnode* p=head->next; while(p) { if(p->data==e) return true; p=p->next; } return false; } cnode* clist::getnode(int i)const { if(i<0||i>getlin()) { throw i; } cnode* p=head; while(p&&i) { p=p->next; i--; } return p; } void clist::insert(int i,const int e) { cnode* p; cnode *node=new cnode(); node->data=e; if(i==1) { node->next=head->next; head->next=node; } else{ p=getnode(i-1); if(i==getlin()) p->next=node; else{ node->next=p->next; p->next=node; } } } void clist::Delete(const int e) { if(!find(e)) { cout<<"不包含啊"<<endl; return ; } cnode* p=head; cnode *q=head->next; while(q) { if(q->data==e) { break; } p=p->next; q=q->next; } p->next=q->next; return; } void clist::reverse(){ if(isempty()) { cout<<"kongle"<<endl; } cnode *p,*q; int len=getlin(); int i=1; int j=len; while(i<j) { p=getnode(i); q=getnode(j); int temp=p->data; p->data=q->data; q->data=temp; ++i; --j; } } int main() { clist* link=new clist(); link->create(); link->display(); cout<<link->getlin()<<endl; cout<<link->isempty()<<endl; cout<<link->find(3)<<endl; link->insert(1,888); link->insert(3,999); link->Delete(6); link->display(); link->reverse(); link->display(); system("pause"); return 0; }
原文地址:https://www.cnblogs.com/wpbing/p/9191429.html