Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include<iostream>
- struct Node{
- int data;
- struct Node *next;
- };
- Node* head = NULL;
- int Size = 0;
- void Traversal(){
- struct Node* temp = head;
- cout<<"\tThe Linked List is : \t";
- while(temp != NULL){
- cout<<temp->data<<" |-> ";
- temp = temp->next;
- }
- cout<<"\tSize - "<<Size<<endl;
- cout<<"\n------------------------------------------------------------------------\n";
- }
- void InsertBeg(int x){
- cout<<"Inserting "<<x<<" at the beginning"<<endl;
- struct Node* node = new Node;
- node->data = x;
- node->next = head;
- head = node;
- Size++;
- Traversal();
- }
- void InsertEnd(int x){
- cout<<"Inserting "<<x<<" at the end"<<endl;
- Node* node = new Node;
- node->data = x;
- node->next = NULL;
- if (head == NULL){
- head = node;
- }
- else{
- Node *temp = head;
- while(temp->next != NULL){
- temp = temp->next;
- }
- temp->next = node;
- }
- Size++;
- Traversal();
- }
- void InsertPos(int x, int pos){
- if (pos > Size){
- cout<<"Invalid Position"<<endl;
- return;
- }
- if (pos == 0){
- InsertBeg(x);
- return;
- }
- cout<<"Inserting "<<x<<" at position "<<pos<<endl;
- Node* node = new Node;
- node->data = x;
- node->next = NULL;
- Node* temp = head;
- while(pos--){
- if (pos == 0){
- node->next = temp->next;
- temp->next = node;
- }
- else{
- temp = temp->next;
- }
- }
- Size++;
- Traversal();
- }
- void MultiInsertM(int i){
- Node* temp = head;
- for(int j=1;j<=Size;j++){
- if(j%i == 0){
- InsertPos(i, j-1);
- temp = temp->next;
- }
- }
- }
- int P[] = {2, 3, 5, 7, 11, 13, 17, 19};
- void MultiInsertP(){
- Node* temp = head;
- int k = 0;
- for(int j=1;j<=Size;j++){
- if(j == P[k]){
- InsertPos(P[k], j-1);
- temp = temp->next;
- k++;
- }
- }
- }
- int main(){
- InsertEnd(23);
- InsertEnd(15);
- InsertEnd(35);
- InsertEnd(49);
- MultiInsertM(2);
- // MultiInsertP();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement