|
| 1 | +#include<iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +/* |
| 5 | +Structure of the linked list node is as*/ |
| 6 | +struct node |
| 7 | +{ |
| 8 | + int data; |
| 9 | + struct node *next; |
| 10 | +}; |
| 11 | + |
| 12 | + |
| 13 | +// function inserts the data in front of the list |
| 14 | +void insertAtBegining(struct node** headRef, int newData) |
| 15 | +{ |
| 16 | + // Code here |
| 17 | + if(*headRef == NULL){ |
| 18 | + (*headRef)->data = newData; |
| 19 | + (*headRef)->next = NULL; |
| 20 | + }else{ |
| 21 | + node *newnode = new node(); |
| 22 | + newnode->data = newData; |
| 23 | + newnode->next = (*headRef); |
| 24 | + (*headRef) = newnode; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// function appends the data at the end of the list |
| 29 | +void insertAtEnd(struct node** headRef, int newData) |
| 30 | +{ |
| 31 | + // Code here |
| 32 | + node *newnode = new node(); |
| 33 | + newnode->data = newData; |
| 34 | + newnode->next = NULL; |
| 35 | + if(*headRef ==NULL){ |
| 36 | + *headRef = newnode; |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + node* curr = *headRef; |
| 41 | + while(curr->next!=NULL){ |
| 42 | + curr = curr->next; |
| 43 | + } |
| 44 | + curr->next = newnode; |
| 45 | + return; |
| 46 | +} |
| 47 | + |
| 48 | +node* deleteNode(node *head,int x) |
| 49 | +{ |
| 50 | + //Your code here |
| 51 | + if(head!=NULL){ |
| 52 | + if(x==0){ |
| 53 | + node *temp = head; |
| 54 | + head = head->next; |
| 55 | + delete temp; |
| 56 | + return head; |
| 57 | + }else{ |
| 58 | + node *temp = head; |
| 59 | + for(int i=0;i<x-1;i++){ |
| 60 | + head = head->next; |
| 61 | + } |
| 62 | + node *todelete = head->next; |
| 63 | + head->next = todelete->next; |
| 64 | + delete todelete; |
| 65 | + return temp; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +bool search(node *head,int x){ |
| 71 | + if(head==NULL) return false; |
| 72 | + if(head->data == x) return true; |
| 73 | + search(head->next,x); |
| 74 | +} |
| 75 | + |
| 76 | +int main(){ |
| 77 | + int t,n,k,num; |
| 78 | + cin>>t; |
| 79 | + while(t--){ |
| 80 | + cin>>n; |
| 81 | + node *head = NULL; |
| 82 | + for(int i=0;i<n;i++){ |
| 83 | + // cin>>num>>k; |
| 84 | + // if(k==0) insertAtBegining(&head,num); |
| 85 | + // else insertAtEnd(&head,num); |
| 86 | + cin>>num; |
| 87 | + insertAtEnd(&head,num); |
| 88 | + } |
| 89 | + //delete node |
| 90 | + /* |
| 91 | + cout<<"enter pos to delete:: \n"; |
| 92 | + int pos; |
| 93 | + cin>>pos; |
| 94 | + deleteNode(head,pos); |
| 95 | + */ |
| 96 | + |
| 97 | + //search for nodevalue |
| 98 | + int val; |
| 99 | + cout<<"enter value to be searched:: \n"; |
| 100 | + cin>>val; |
| 101 | + bool res = search(head,val); |
| 102 | + if(res == true)cout<<"value found\n"; |
| 103 | + else cout<<"value not found\n"; |
| 104 | + |
| 105 | + while(head!=NULL){ |
| 106 | + cout<<head->data<<" "; |
| 107 | + head = head->next; |
| 108 | + } |
| 109 | + cout<<endl; |
| 110 | + delete head; |
| 111 | + } |
| 112 | + return 0; |
| 113 | +} |
0 commit comments