This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define endl '\n' | |
struct node | |
{ | |
int data; | |
struct node* next; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Linked lists are basically a collection of connected nodes of the following structure. | |
struct node | |
{ | |
int data; // this part holds the data (can be of any datatype) | |
struct node* next; // this part holds pointer to the next node | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//function to insert a new node at the head of the list | |
node* InsertAtHead(node* head,int x) | |
{ | |
node* temp = new node(); //dynamically allocate a new node | |
temp->data =x; //sets the data part to the required value | |
temp->next=head; //sets the link part | |
head =temp; | |
return head; //returns the head pointer to calling function ie.main() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node* InsertAtEnd(node* head,int x) | |
{ | |
node* temp = new node(); | |
temp->data = x; | |
temp->next=NULL; | |
node* temp1 = head; // temporary variable to store the head | |
while(temp1->next!=NULL) | |
temp1 = temp1->next; //iterating to the end of the list | |
temp1->next = temp; | |
return head; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node* InsertAtPos(node* head,int x,int n) | |
{ | |
node* temp = new node(); | |
temp->data = x; | |
temp->next=NULL; | |
//if one has to insert at the front | |
if(n==1) | |
{ | |
temp->next = head; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void print_list(node* head) | |
{ | |
while(head!=NULL) | |
{ | |
cout<<head->data<<endl; | |
head=head->next; | |
} | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define endl '\n' | |
struct node | |
{ | |
int data; | |
struct node* next; | |
}; |