c_cpp 使用Struct实现链接列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 使用Struct实现链接列表相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
using namespace std;

struct node{
    int data;
    node* next;
};                  //Semi colon after struct defn

node* head=NULL;    //head is defined global to avoid always returning head in functions
                    //if class is used its not necessary as ll class will contain head pointer

node* getNewnode(int x){        //works as a constructor
    node* newnode=(node*)malloc(sizeof(node));   //in structs malloc is used to allocate memory unlike new keyword in class
    newnode->data=x;
    newnode->next=NULL;
    return newnode;
}
bool isEmpty(){     //checks if the ll is empty
    if(head==NULL){
        return true;
    }
    return false;
}
node* getLastNode(){    //returns address of the last node of ll
    if(head==NULL){
        return head;
    }else{
        node* temp=head;
        while(temp->next!=NULL){
            temp=temp->next;
        }
        return temp;
    }
}
void addAtEnd(int x){       //adds an element at the end of ll
    node* n1=getNewnode(x);
    if(head==NULL){
        head=n1;
    }else{
        node* last=getLastNode();
        last->next=n1;
    }
}
void addAtFront(int x){     //adds an element at the front of ll
    node* n1=getNewnode(x);
    if(head==NULL){
        head=n1;
    }else{
        n1->next=head;
        head=n1;
    }
    //delete(n1); // to delete n1 from memory [DON'T DELETE when ADDing a node]
}
void printList(){       //prints entire ll
    if(head==NULL){
        cout<<"List Empty!"<<endl;
    }else{
        node* temp=head;
        while(temp!=NULL){
            cout<<temp->data<<" ";
            temp=temp->next;
        }
    }
    cout<<endl;
}
void inserter(int x,int n){     //inserts x at nth node's position indexing from 1
    if(n==1){
        addAtFront(x);
    }else{
        node* n1=getNewnode(x);
        node* temp=head;
        int pos=2;
        while(pos<n){
            temp=temp->next;
            pos++;
        }
        n1->next=temp->next;
        temp->next=n1;
        //delete(n1); // to delete n1 from memory [DON'T DELETE when ADDing a node]
    }
}
node* searcher(int x){      //searches for element x and returns its node address
    node* temp=head;
    int pos=1;
    while(temp!=NULL){
        if(temp->data==x){
            break;
        }else{
            pos++;
            temp=temp->next;
        }
    }
    return temp;
}
bool delAtEnd(){        //deletes last node and tells if deletion is successfully performed
    if(head==NULL){
        return false;
    }else{
        node* temp=head;
        while(temp->next->next!=NULL){
            temp=temp->next;
        }
        node* temp1=temp->next;
        temp->next=NULL;
        delete(temp1); // to delete the last node from memory
        return true;
    }
}
bool delAtFront(){      //deletes head node and tells if deletion is successfully performed
    if(head==NULL){
        return false;
    }else{
        node* temp=head;
        head=temp->next;
        delete(temp); // to delete the previous head node from memory
        return true;
    }
}
bool deleter(int n){        //deletes the nth node indexing from 1 and tells if deletion is successfully performed
    if(n==1){
        return delAtFront();
    }else{
        int pos=1;
        node* temp=head;
        while(pos<n-1){
            temp=temp->next;
            pos++;
        }
        node* temp1=temp->next;
        temp->next=temp->next->next;
        delete(temp1); // to delete the previous node from memory
        return true;
    }
}
int main(){
    head= NULL;
    addAtEnd(2); //functions are called directly as they have no class
    addAtEnd(2);
    addAtEnd(2);
    addAtEnd(2);
    addAtFront(3);
    printList();
    inserter(5,1);
    inserter(5,3);
    printList();
    struct node* n1=searcher(5); //struct keyword is used always to use the defined structure
    cout<<n1->data<<endl;
    if(delAtEnd()==true){
        printList();
    }else{
        cout<<"List Empty!"<<endl;
    }
    if(delAtFront()==true){
        printList();
    }else{
        cout<<"List Empty!"<<endl;
    }
    if(deleter(2)==true){
        printList();
    }else{
        cout<<"List Empty!"<<endl;
    }

    return 0;
}

以上是关于c_cpp 使用Struct实现链接列表的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用类和链接列表实现堆栈

c_cpp 链接列表实现与类

c_cpp 链接列表的线性队列实现

c_cpp 队列 - C ++中的链接列表实现

c_cpp 使用类的链接列表

c_cpp 使用第二级指针删除链接列表节点