Skip to content

Instantly share code, notes, and snippets.

View amulyagaur's full-sized avatar
🎯
Focusing

Amulya Gaur amulyagaur

🎯
Focusing
View GitHub Profile
@amulyagaur
amulyagaur / ll.cpp
Created July 16, 2017 17:28
Linked
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
struct node
{
int data;
struct node* next;
};
// 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
};
//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()
}
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;
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;
void print_list(node* head)
{
while(head!=NULL)
{
cout<<head->data<<endl;
head=head->next;
}
return;
}
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
struct node
{
int data;
struct node* next;
};