DS single Link list--class implementation

Source: Internet
Author: User

Using C + + language and class to achieve a single linked list, including the head node

Attributes include: Data data field, next pointer field

Actions include: Insert, delete, find

Note: The single linked list is not an array, so the position starts with 1, the first node, and the header node does not place the data.

input

N

Line 1th First enter N to indicate n data, then enter n data
Line 2nd Enter the location and new data to insert
Line 3rd Enter the location and new data to insert
Line 4th Enter the location you want to delete
Line 5th Enter the location you want to delete
Line 6th Enter the location you want to find
Line 7th Enter the position to find output

N

The data is separated by a space,

Row 1th output data from a single linked list after creation

For each successful operation (insert or delete), the output of the executed single linked list data

Every time a successful lookup is performed, the output finds the data

If the operation fails (including INSERT, delete, lookup, etc.), output string error, do not need to output single list sample input 6 3 777 1 888 1 0 5 Sample output 11 22 33 4 4 777-888-a-777-a-777-A-list error error 44

The code is as follows:


#include <iostream> using namespace std;
#define OK 0 #define ERROR-1;
    Class ListNode {public:int data;
    ListNode * NEXT;
ListNode () {next=null;}};
    Class Linklist {Public:listnode * head;
    int Len;
    Linklist ();
    ~linklist ();
    ListNode *ll_index (int i);
    int ll_get (int i);
    int ll_insert (int i,int item);
    int Ll_del (int i);
void Ll_display ();
};
    Linklist::linklist () {head=new listnode ();
len=0;
    } linklist::~linklist () {listnode*p,*q;
    P=head;
        while (p!=null) {q=p;
        p=p->next;
    Delete q;
    } len=0;
Head=null;
    int linklist::ll_insert (int i,int item) {ListNode *p;
    if (Ll_index (i) ==null) return-1;
    else P=ll_index (i);
    ListNode *s;
    S=new ListNode ();
    s->data=item;
    s->next=p->next;
    p->next=s;
    len++;
     
return 0;
    } listnode *linklist::ll_index (int i) {int j=0;
    ListNode *p;
    P=head; while (p&&j< i-1) {p=p->next;
    ++j; } if (!p| |
    J&GT;I-1) return NULL;
else return p;
    int Linklist::ll_del (int i) {ListNode *p,*q;
    if (Ll_index (i) ==null) return-1;
        else {p=ll_index (i);
        q=p->next;
        p->next=q->next;
        Delete q;
        len--;
    return OK; } int Linklist::ll_get (int i) {if (i>len| |
    i<=0) return-1;
        else {ListNode *p;
        P=head;
        int j=0;
            while (p->next&&j<i) {p=p->next;
        ++j;
        } int num=p->data;
    return num;
    } void Linklist::ll_display () {ListNode *p;
    p=head->next;
        while (p) {cout<<p->data<< "";
    p=p->next;
} cout<<endl;
    int main () {int n,i,m;
    Linklist list;
    cin>>n;
        for (i=1;i<=n;i++) {cin>>m; List. Ll_insert (I,m); } list.
    Ll_display ();
    cin>>n>>m; if (list.
    Ll_insert (n,m) ==-1) cout<< "error" <<endl; Else list.
    Ll_display ();
    cin>>n>>m; if (list.
    Ll_insert (n,m) ==-1) cout<< "error" <<endl; else {list.
    Ll_display ();
    } cin>>n; if (list.
    Ll_del (n) ==-1) cout<< "error" <<endl; else {list.
    Ll_display ();
    } cin>>n; if (list.
    Ll_del (n) ==-1) cout<< "error" <<endl; else {list.
    Ll_display ();
    } cin>>n; if (list.
    Ll_get (n) ==-1) cout<< "error" <<endl; else {cout<<list.
    Ll_get (n) <<endl;
    } cin>>n; if (list.
    Ll_get (n) ==-1) cout<< "error" <<endl; else {cout<<list.
    Ll_get (n) <<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.