Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions Module 6/delete_at_any_pos.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
#include <bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

void insert_at_tail(Node* &head, Node* &tail,int val)
{
Node *newnode = new Node(val);
if(head == NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next = newnode;
tail = newnode;
}

void print_linked_list(Node* head)
{
Node* tmp = head;
while(tmp != NULL)
{
cout << tmp->val << endl;
tmp = tmp->next;
}
}

void delete_at_any_pos(Node* &head, int idx){
Node* tmp= head;
for(int i=1; i<idx; i++){
tmp=tmp->next;
}
Node* deleteNode = tmp->next;
tmp->next= tmp->next->next;
delete deleteNode;

}
int main()
{
int val = 10;
int *ptr = &val;
cout << *ptr << endl;
Node* head = NULL;
Node* tail = NULL;

int val;
while(true)
{
cin >> val;
if(val == -1)
{
break;
}
insert_at_tail(head,tail,val);
}
delete_at_any_pos(head,2);
print_linked_list(head);
return 0;
}
}