0% found this document useful (0 votes)
17 views

Link List

The document discusses various topics related to data structures and algorithms in C++ including object oriented programming, classes and structures, single linked lists, doubly linked lists, and hash maps. It provides code examples for creating and traversing single linked lists, adding and deleting nodes, finding the middle node, and checking for loops. Doubly linked lists are also covered with examples for basic operations like adding and deleting nodes.

Uploaded by

mrrinku1604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Link List

The document discusses various topics related to data structures and algorithms in C++ including object oriented programming, classes and structures, single linked lists, doubly linked lists, and hash maps. It provides code examples for creating and traversing single linked lists, adding and deleting nodes, finding the middle node, and checking for loops. Doubly linked lists are also covered with examples for basic operations like adding and deleting nodes.

Uploaded by

mrrinku1604
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Object Oriented :-

Bank :-
10 employees.
Name(String), mobilenumber(Int), address(Staring), family members

Class Employees {
String Name,
Int mobile,
Staring address
}

Public and private


Public :- open to other worlds.
Private :- close for other worlds.

#include <bits/stdc++.h>
using namespace std;

class employee{
public:
string name;
int mobile;
};

int main() {
employee abc;
abc.name = "Qwer";
cout<<abc.name;
return 0;
}

Diff b/w class and struct :-


1. By default class variables are private and public for struct.
2.

SLL(Single Link List):-


Insert , delete , search :- O(N)

Single link list :-


Question :-
Create a SLL. which nodes are int.

20 NULL

#include <bits/stdc++.h>
using namespace std;

struct singlyLinkList {
int value;
singlyLinkList* next = NULL;

singlyLinkList(int number){
value = number;
}
};

int main() {
singlyLinkList* head = NULL;
head = new singlyLinkList(20);
singlyLinkList* second = new singlyLinkList(30);
head->next = second;
singlyLinkList* third = new singlyLinkList(50);
second->next = third;
head->next = third;
while(head!=NULL){
cout<<head->value<<" ";
head = head->next;
}
//create anode;
return 0;
}

Question :- Create a SLL.

#include <bits/stdc++.h>
using namespace std;

struct singlyLinkList {
int value;
singlyLinkList* next = NULL;

singlyLinkList(int number){
value = number;
}
};

singlyLinkList* addNode(int value, singlyLinkList* head){


singlyLinkList* temp= head;
singlyLinkList* newNode = new singlyLinkList(value);
if(head == NULL){
head = newNode;
}
else{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
return head;
}

int main() {
singlyLinkList* head = NULL;
for(int i=0;i<10;i++)
{
head = addNode(i, head);
}
while(head!=NULL){
cout<<head->value<<" ";
head = head->next;
}
//create anode;
return 0;
}

Question :- Add a new node at the end of SLL.


Question :- Add a new node at the first position of SLL.
Question :- Add a new node in between of SLL.

#include <bits/stdc++.h>
using namespace std;

struct singlyLinkList {
int value;
singlyLinkList* next = NULL;

singlyLinkList(int number){
value = number;
}
};

singlyLinkList* addNode(int value, singlyLinkList* head){


singlyLinkList* temp= head;
singlyLinkList* newNode = new singlyLinkList(value);
if(head == NULL){
head = newNode;
}
else{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
return head;
}

int main() {
singlyLinkList* head = NULL;
for(int i=0;i<10;i++)
{
head = addNode(i, head);
}
// add 10 end of list 0 1 2 3 4 5 6 7 8 9 10
// add 10 at first position 10 0 1 2 3 4 5 6 7 8 9
// add 10 at 4 th position 0 1 2 10 3 4 5 6 7 8 9
//create anode;
return 0;
}

https://www.codechef.com/AUG21/?itm_medium=navmenu&itm_campaign=AUG21

Question :- Delete the node from END, between, start.


Question :- check if two link lists are identical(same length and same elements).

1->2->3->4->5->6
1->3->4->7

1->2->3
1->2->3

(a+b)%mod = (a%mod+b%mod)%mod

Question :- Count the number of nodes in a SLL.


3->6->8->10->12
Answer :- 5

Question :- Given a SLL. find the middle node of the link list.
3->6->8->10->12
Answer :- 8
3->6->8->10->12->13
Answer :- 8,10
Question :- Given a SLL. check if the list creates a loop.
Node{
Int value,
Node * next,
Node * another
}

DPP:-
https://leetcode.com/problems/linked-list-cycle/
https://leetcode.com/problems/intersection-of-two-linked-lists/
https://leetcode.com/problems/coin-change/

struct singlyLinkList {
int value;
singlyLinkList* next = NULL;
singlyLinkList* nextOne = NULL;

singlyLinkList(int number){
value = number;
}
};

https://leetcode.com/problems/lru-cache/
https://practice.geeksforgeeks.org/explore/?category%5B%5D=Linked%20List&page=1&
category%5B%5D=Linked%20List

Question :- Search an element in an infinite sorted stream(Array).


1,2,3,7,9,10,12,56,78,90,9000,9000000000,88888888888899....................................

Size of array = infinite


Search Element :- 9000000

0 to 1
2 to 2
3 to 4
5 to 8
9 to 16
17 to 32
33 to 64

Doubly Linked List :-


Add Node
Delete Node
Delete in Between
Remove a Node.

struct DoublyLinkList {
int value;
DoublyLinkList * next = NULL;
DoublyLinkList *pre=NULL;

singlyLinkList(int number){
value = number;
}
};

#include <iostream>
using namespace std;
struct doublylinklist{
int value;
doublylinklist*next=NULL;
doublylinklist*pervious=NULL;

doublylinklist(int number){
value=number;
}
};

int main() {
// your code goes here
doublylinklist* head = NULL;
head = new doublylinklist(5);
doublylinklist* second = new doublylinklist(7);
head->next=second;
doublylinklist* third=new doublylinklist(0);
doublylinklist* four=new doublylinklist(8);
doublylinklist* five=new doublylinklist(6);
second->next=third;
third->next=four;
four->next=five;
// reverse connectiong
five->pervious=four;
four->pervious=third;
third->pervious=second;
second->pervious=head;
doublylinklist* temp = head, *lastNode;
while(temp!=NULL)
{
cout<<temp->value<<" ";
lastNode = temp;
temp=temp->next;
}
cout<<endl;
//rever travel
while(lastNode !=NULL){
cout<<lastNode->value<<" ";
lastNode = lastNode->pervious;
}
return 0;
}

HashMap:-
Map<int, int>

You might also like