Linked List_0

 avatar
kaziamir
c_cpp
a year ago
1.9 kB
14
Indexable
Never
#include<bits/stdc++.h>
#include<stdbool.h>
using namespace std;

#define resetP p = start;

typedef struct node{
    int data;
    node *next;
}node;

int main(){
    node *node1, *node2, *node3;

    node *p;
    node *start;

    node1 = (node *)malloc(sizeof(node));
    node2 = (node *)malloc(sizeof(node));
    node3 = (node *)malloc(sizeof(node));

    node1->next = node2;
    node2->next = node3;
    node3->next = NULL;
    start = node1;
    p = start;

    node1->data = 1;
    node2->data = 2;
    node3->data = 3;

    /*****Before Insertion*****/
    /*
    resetP;
    while(p != NULL){
        cout<<p->data<<endl;
        p = p->next;

    }
    */


    /*****New Node at Beginning*****/

    node *nodeFirst;
    nodeFirst = (node *)malloc(sizeof(node));

    start = nodeFirst;
    nodeFirst->next = node1;
    nodeFirst->data = 0;
    resetP;

    /*****New Node at Ending******/

    node *nodeLast;
    nodeLast = (node *)malloc(sizeof(node));

    nodeLast->next = NULL;
    nodeLast->data = 4;

    //cout<<p<<endl;

    while(p->next != NULL){
        p = p->next;
    }
    p->next = nodeLast;

    /*****New Node in Middle*****/

    resetP;
    node *nodeMid;
    nodeMid = (node *)malloc(sizeof(node));
    nodeMid->next = NULL;
    nodeMid->data = 9;

    int k = 4;
    node *x;
    for(int i=2;i<k;i++){
        if(p->next != NULL){
            p = p->next;

        }
    }
    nodeMid->next = p->next;
    p->next = nodeMid;

    /*
    resetP;
    while(p != NULL){
        cout<<p->data<<endl;
        p = p->next;

    }
    */

    resetP;
    int to_find = 2;
    bool flag=false;
    while(p != NULL){
        if(p->data == to_find){
            flag=true;
            break;
        }
        p = p->next;
    }
    //printf("%s\n",flag);
    cout<<flag<<endl;

    flag == true ? cout<<"Founded"<<endl : cout<<"Not Found"<<endl;

    resetP;

    return 0;
}