update
This commit is contained in:
parent
2992f4f408
commit
4f46de8d00
3330 changed files with 394553 additions and 76939 deletions
47
.config/Code/User/History/47873956/GmJO.cpp
Normal file
47
.config/Code/User/History/47873956/GmJO.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
template <class T>
|
||||
class Node{
|
||||
public:
|
||||
T data;
|
||||
Node<T>* next;
|
||||
Node(T data, Node<T>* next) : data(data), next(next){}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class LinkedList{
|
||||
Node<T>* head;
|
||||
public:
|
||||
LinkedList(T data){
|
||||
head = new Node<T>(data, nullptr);
|
||||
}
|
||||
|
||||
bool insertAtPosition(int position, T data){
|
||||
Node<T>* newNode = new Node(data, head);
|
||||
for(int i=0;i<position-1;i++){
|
||||
newNode = newNode->next;
|
||||
}
|
||||
Node<T>* temp = newNode->next;
|
||||
newNode = newNode->next;
|
||||
temp->next = newNode;
|
||||
return true;
|
||||
}
|
||||
|
||||
void printList(){
|
||||
Node<T>* temp = head;
|
||||
while(temp != nullptr){
|
||||
cout<<temp->data;
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
LinkedList<int> list(1);
|
||||
list.insertAtPosition(0, 1);
|
||||
list.printList();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue