├── All-Operations.java └── Insert at begin and end and reverse the string /All-Operations.java: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //single linked list 4 | //define node 5 | class Node 6 | { 7 | public: 8 | int key;//data 9 | Node*next;//address 10 | }; 11 | //creating node 12 | Node*newnode(int data) 13 | { 14 | Node*n=new Node();//object creation 15 | n-> key=data;//object asign to key 16 | n->next=NULL;//address field 17 | return n; 18 | } 19 | //insertion of data 20 | Node*insert_begin(int data,Node*head) 21 | { 22 | Node*m=newnode(data); 23 | if(head==NULL) 24 | { 25 | head =m; 26 | } 27 | else 28 | { 29 | m->next=head; 30 | head=m; 31 | } 32 | return head; 33 | } 34 | //insertion at end 35 | Node*insert_end(int data,Node*head) 36 | { 37 | Node*m=newnode(data); 38 | if(head==NULL) 39 | { 40 | head =m; 41 | } 42 | else 43 | { 44 | Node*temp=head; 45 | while(temp->next!=NULL) 46 | { 47 | temp=temp->next; 48 | } 49 | temp->next=m; 50 | } 51 | return head; 52 | } 53 | //insertion at middle 54 | Node*mid_insert(int data,Node*head,int pos) 55 | { 56 | Node*m=newnode(data); 57 | if(head==NULL) 58 | { 59 | head=m; 60 | } 61 | else 62 | { 63 | int count=1; 64 | Node*temp=head; 65 | while(temp->next!=NULL) 66 | { 67 | count++; 68 | if(count==pos) 69 | { 70 | break; 71 | } 72 | temp=temp->next; 73 | } 74 | if(count==pos) 75 | { 76 | m->next=temp->next; 77 | temp->next=m; 78 | } 79 | else if(pos==1) 80 | { 81 | head=insert_begin(data,head); 82 | } 83 | else 84 | { 85 | cout<<"invalid"<key<<"->"; 98 | temp=temp->next; 99 | } 100 | cout<<"NULL"; 101 | cout<next; 114 | } 115 | return head; 116 | } 117 | //deletion at end 118 | Node*del_end(Node*head) 119 | { 120 | if(head==NULL) 121 | { 122 | cout<<"list is empty"; 123 | } 124 | if(head->next==NULL) 125 | { 126 | head=del_begin(head); 127 | } 128 | else 129 | { 130 | Node*temp=head; 131 | while(temp->next->next!=NULL) 132 | { 133 | temp=temp->next; 134 | } 135 | temp->next=NULL; 136 | } 137 | return head; 138 | } 139 | //deletion at middle 140 | Node*del_mid(Node*head,int pos) 141 | { 142 | if(head==NULL) 143 | { 144 | cout<<" Not delete"; 145 | } 146 | else if(pos==1) 147 | { 148 | head=del_begin(head); 149 | } 150 | else 151 | { 152 | int count=1; 153 | Node*temp=head; 154 | while(temp!=NULL) 155 | { 156 | count++; 157 | Node*temp1=temp->next; 158 | if(count==pos) 159 | { 160 | temp->next=temp1->next; 161 | temp1=NULL; 162 | } 163 | temp=temp->next; 164 | } 165 | return head; 166 | } 167 | } 168 | //Searching the element in linked list; 169 | Node*search_element(int data,Node*head) 170 | { 171 | Node*temp=head; 172 | int count=0; 173 | while(temp->next!=NULL) 174 | { 175 | if(temp->key==data) 176 | { 177 | count++; 178 | cout<<"yes"<next; 181 | } 182 | if(count==0){ 183 | cout<<"no"<next==NULL) 196 | { 197 | cout<<"List contain one value"<next; 204 | while(temp2!=NULL) 205 | { 206 | temp1->next = temp; 207 | temp = temp1; 208 | temp1 = temp2; 209 | temp2 = temp1->next; 210 | } 211 | temp1->next = temp; 212 | head = temp1; 213 | } 214 | return head; 215 | } 216 | //main function 217 | int main() 218 | { 219 | Node*head=NULL; 220 | head=insert_begin(30,head); 221 | head=insert_begin(79,head); 222 | head=insert_end(3,head); 223 | head=insert_begin(380,head); 224 | head=insert_end(392,head); 225 | head=del_end(head); 226 | head=del_begin(head); 227 | head=mid_insert(5,head,1); 228 | head=insert_end(55,head); 229 | head=del_mid(head,2); 230 | //head=search_element(30,head); 231 | head=display(head); 232 | head=reserve(head); 233 | head=display(head); 234 | } 235 | -------------------------------------------------------------------------------- /Insert at begin and end and reverse the string: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | class Node { 6 | public: 7 | int key; 8 | Node* next; 9 | }; 10 | 11 | Node* newnode(int data) { 12 | Node* n = new Node(); 13 | n->key = data; 14 | n->next = NULL; 15 | return n; 16 | } 17 | 18 | Node* insert_begin(int data, Node* head) { 19 | Node* nn = newnode(data); 20 | nn->next = head; 21 | head = nn; 22 | return head; 23 | } 24 | 25 | Node* insert_end(int data, Node* head) { 26 | Node* nn = newnode(data); 27 | if (head == NULL) { 28 | head = nn; 29 | } else { 30 | Node* temp = head; 31 | while (temp->next != NULL) { 32 | temp = temp->next; 33 | } 34 | temp->next = nn; 35 | } 36 | return head; 37 | } 38 | 39 | Node* insert_mid(int data, Node* head, int pos) { 40 | Node* nn = newnode(data); 41 | if (head == NULL) { 42 | head = nn; 43 | } else { 44 | int count = 1; 45 | Node* temp = head; 46 | while (temp->next != NULL) { 47 | count++; 48 | if (count == pos) { 49 | break; 50 | } 51 | temp = temp->next; 52 | } 53 | if (count == pos) { 54 | nn->next = temp->next; 55 | temp->next = nn; 56 | } else { 57 | cout << "Invalid position"; 58 | } 59 | } 60 | return head; 61 | } 62 | 63 | void display(Node* head) { 64 | Node* temp = head; 65 | while (temp != NULL) { 66 | cout << temp->key << "->"; 67 | temp = temp->next; 68 | } 69 | cout << "NULL" << endl; 70 | } 71 | 72 | Node* reverse(Node* head) { 73 | Node *temp1 = NULL, *temp2 = head, *temp3; 74 | if (head == NULL) { 75 | cout << "List empty"; 76 | } else if (head->next == NULL) { 77 | cout << "List contains one value"; 78 | } else { 79 | temp3 = head->next; 80 | while (temp3 != NULL) { 81 | temp2->next = temp1; 82 | temp1 = temp2; 83 | temp2 = temp3; 84 | temp3 = temp3->next; 85 | } 86 | temp2->next = temp1; 87 | head = temp2; 88 | } 89 | return head; 90 | } 91 | 92 | int main() { 93 | Node* head = NULL; 94 | head = insert_begin(30, head); 95 | head = insert_begin(20, head); 96 | head = insert_begin(10, head); 97 | head = insert_begin(5, head); 98 | head = insert_end(40, head); 99 | head = insert_mid(50, head, 3); 100 | display(head); 101 | head = reverse(head); 102 | display(head); 103 | } 104 | --------------------------------------------------------------------------------