Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Samyak #1055

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Samyak #1055

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions BitManipulation/count_ones/count_ones.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
def count_ones(n: int):
def count_ones(n):
count = 0
while n:
if n & 1:
count += 1
n >>= 1
n=bin(n).split('b')[1]
count=n.count('1')
return count


Expand Down
15 changes: 15 additions & 0 deletions BitManipulation/reverse_bits/reverseBits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def reverse_32_bit(n):
n=bin(n).split('b')[1]
if len(n)<32:
temp=32-len(n)
s=''
for i in range(temp):
s+='0'
n=s+n
n=n[::-1]
return int(n,2)

for _ in range(int(input())):
n=int(input())
result=reverse_32_bit(n)
print(result)
208 changes: 120 additions & 88 deletions Data Structures/Linked List/SinglyLinkedLIst2.cpp
Original file line number Diff line number Diff line change
@@ -1,122 +1,154 @@
#include "iostream"
#include "cstdlib"
#include<bits/stdc++.h>

using namespace std;
struct Node
{

class Node{
public:
int data;
Node* link;
Node *next;
};
Node* A;
void insert()
{
int n,p;
cout<<"Enter the no to be Inserted:";
cin>>n;
cout<<"Enter the position:";
cin>>p;
Node* temp=new Node;
if (p==1)
{
temp->link=A;
temp->data=n;
A=temp;
return;

void PushFront(Node** head_ref, Node** tail, int key){
Node* newnode = new Node();
newnode->next = *head_ref;
newnode->data = key;

if(*tail == NULL){
*tail = newnode;
}
Node* temp2=A;
for(int i=1;i<p-1;i++)
{
temp2=temp2->link;

*head_ref = newnode;
}

void AddAfter(Node* pre_node,Node** tail, int key){
if(pre_node == NULL){
cout<<"Node can't be null\n";
return;
}
temp->link=temp2->link;
temp2->link=temp;
temp->data=n;
Node* newnode = new Node();
newnode->data = key;
newnode->next = pre_node->next;
pre_node->next = newnode;

if(pre_node == *tail)
*tail = newnode;
}
void delet()
{
int p;
cout<<"Enter the position of the element to be deleted:";
cin>>p;
Node* temp=A;
if (p==1)
{
A=temp->link;
temp=NULL;

void TopFront(Node** head_ref){
if(*head_ref == NULL){
cout<<"List is Empty\n";
return;
}
Node* temp2=new Node;
for(int i=1;i<p-1;i++)
{
temp=temp->link;
Node* temp = *head_ref;
cout<<"Top element: "<<temp->data<<endl;
}

void PopFront(Node** head_ref, Node** tail){
if(*head_ref == NULL){
cout<<"Error: Empty list\n";
return;
}
temp2=temp->link;
temp->link=temp2->link;
temp2=NULL;
Node* temp = *head_ref;
cout<<"Deleted Element: "<<temp->data<<endl;
*head_ref = temp->next;

if(*head_ref == NULL)
*tail = NULL;

}
void display()
{
Node* temp=A;
while(temp!=NULL)
{
cout<<"->"<<temp->data;
temp=temp->link;

void PushBack(Node** head_ref, Node** tail, int key){
Node* newnode = new Node();
newnode->data = key;
newnode->next = NULL;

if(*tail == NULL){
*head_ref = newnode;
*tail = newnode;
}
else{
Node* temp = *tail;
temp->next = newnode;
*tail = newnode;
}
}

void printList(Node* node){
if(node == NULL){
cout<<"Error: List is Empty"<<endl;
}
while(node->next != NULL){
cout<<node->data<<" ";
node=node->next;
}
cout<<"\n"<<"---------------------------"<<"\n";
}
void count()
{
int c=0;
Node* temp=A;
while(temp!=NULL)
{
c++;
temp=temp->link;
void TopBack(Node** tail){
if(*tail == NULL){
cout<<"Error: List is Empty\n";
return;
}
cout<<c<<endl;
Node* temp = *tail;
cout<<"Last Element: "<<temp->data<<endl;
}
void rev()
{
Node* temp=A;
Node* temp2=new Node;
Node* prev=temp->link;
while(prev!=NULL)
{
temp2=prev->link;
prev->link=temp;
temp=prev;
prev=temp2;

void PopBack(Node** head_ref, Node** tail){
if(*head_ref == NULL){
cout<<"Error: List is Empty\n";
return;
}
A->link=NULL;
A=temp;
Node* p=*head_ref;
while(p->next->next != NULL);
p->next=NULL;
}
int main()
{
A=NULL;
while(1)
{
int ch;
cout<<"Enter your choice:\n1-Insert\n2-Delete\n3-Display\n4-Reverse\n5-Count\n6-Exit\n->";

// bool find(int key){

// }

int main(){
Node *head = NULL;
Node *tail = NULL;
while(1){
int ch, num;
cout<<"Enter your choice:\n1-Insert at front\n2-Insert at back\n3-Insert After\n4-delete from front\n5-delete from back\n6-First Element\n7-Last Element\n8-PrintList\n9-Exit->";
cin>>ch;
switch(ch)
{
switch(ch){
case 1:
insert();
cout<<"Enter the Element: ";
cin>>num;
PushFront(&head, &tail, num);
break;
case 2:
delet();
cout<<"Enter the Element: ";
cin>>num;
PushBack(&head, &tail, num);
break;
case 3:
display();
cout<<"Enter the Element: ";
cin>>num;
AddAfter(head->next, &tail, num);
break;
case 4:
rev();
PopFront(&head, &tail);
break;
case 5:
count();
PopBack(&head, &tail);
break;
case 6:
TopFront(&head);
break;
case 7:
TopBack(&tail);
break;
case 8:
printList(head);
break;
case 9:
exit(0);
}
}


}