Skip to content
Open
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
96 changes: 96 additions & 0 deletions Data Structure Algo/C++/linked list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

\#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* link;
};
struct node* head;
void Insert_start(int x){
node* temp = new node();
temp->data=x;
temp->link=head;
head=temp;

}
void reverse(){
node* prev=NULL;
node* curr=head;
node* next;
while(curr!=NULL){
next=curr->link;
curr->link=prev;
prev=curr;
curr=next;
//next=next->link;
}
head=prev;
}
void Insert_nth(int x, int n){
node* temp1=head;
node* temp2 = new node();
temp2->data=x;
if(n==1){
temp2->link=head;
head=temp2;
return;
}
for(int i=0;i<n-2;i++){
temp1=temp1->link;
}
temp2->link=temp1->link;
temp1->link=temp2;
}
void Delete(int n){
node* temp1=head;
if(n==1){
head=temp1->link;
return;
}
for(int i=0;i<n-2;i++){
temp1=temp1->link;
}
node* temp2=temp1->link;
temp1->link=temp2->link;
free(temp2);
}
void print(){
node* temp=head;
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->link;
}
printf("\n");
}
void rec_print_start(node* p){
//node* temp=head;
if(p==NULL) return;
cout<<p->data<<"\t";
rec_print_start(p->link);
}
void rec_print_end(node* p){
//node* temp=head;
if(p==NULL) return;
rec_print_end(p->link);
cout<<p->data<<"\t";
}
int main(){
int i,n,x,y;
head=NULL;
printf("How many nos? ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter no: ");
scanf("%d%d",&x,&y);
Insert_nth(x,y);
print();
}
Delete(3);
print();
reverse();
print();
rec_print_end(head);
cout<<"\n";
rec_print_start(head);
cout<<"\n";
}
14 changes: 14 additions & 0 deletions Data Structure Algo/C++/patterns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<bits/stdc++.h>
using namespace std;
bool prime(int n){
for(int i=2;i<n/2;i++){
if(n%i==0) return false;
}
return true;
}
int main(){
int n;
cin>>n;
if(prime(n)) cout<<"prime";
else cout<<"non prime";
}
48 changes: 48 additions & 0 deletions Data Structure Algo/C++/queue using arrray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<bits/stdc++.h>
using namespace std;
int a[51];
int front =-1;
int rear =-1;
void enqueue(int x){
if(rear==51){
cout<<"underflow";
return;
}
if(front==-1) front++;
a[++rear]=x;
}
void dequeue(){
if(front==-1 && rear==-1){
cout<<"underflow";
return;
}
if(front==rear){
front=-1;
rear=-1;
return;
}
front++;
}
bool isEmpty(){
if(rear==-1 && front==-1) return true;
return false;
}
void print(){
for(int i=front;i<=rear;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
int main(){
int n,x;
cout<<"Enter n: ";
cin>>n;
while(n--){
cout<<"Enter x: ";
cin>>x;
enqueue(x);
}
print();
dequeue();
print();
}
50 changes: 50 additions & 0 deletions Data Structure Algo/C++/queue using linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;

};
node* rear=NULL;
node* front=NULL;
void enqueue(int x){
node* temp=new node();
temp->data=x;
temp->next=NULL;
if(rear=NULL){
rear=temp;
return;
}
rear->next=temp;
rear=temp;
return;
}
void dequeue(){
if(front=NULL){
cout<<"underflow";
return;
}
node* temp=front;
front=temp->next;
free(temp);
}
void print(){
node* temp=front;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main(){
int n,x;
cout<<"Enter n: ";
cin>>n;
while(n--){
cout<<"Enter x: ";
cin>>x;
enqueue(x);
}
print();
dequeue();
print();
}
27 changes: 27 additions & 0 deletions Data Structure Algo/C++/reverse linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
struct node* head;
void print(){
node* temp=head;
while(temp->next!=NULL){
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<"\n";
}
int main(){
node* prev=NULL;
node* curr=head;
node*next;
while(curr->next!=NULL){
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
next=next->next;
}
}
43 changes: 43 additions & 0 deletions Data Structure Algo/C++/stack using array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<bits/stdc++.h>
using namespace std;
int p=51;
int Stack[50];
int top=-1;
void push(int x){
if(top==50){
cout<<"stack iis full";
return;
}
Stack[++top]=x;
}
void pop(){
if(top==-1){
cout<<"stack is empty";
return;
}
top--;
}
void print(){
for(int i=0;i<=top;i++){
cout<<Stack[i]<<" ";
}
}
void Top(){
cout<<Stack[top];
}
bool isEmpty(){
if(top==-1) return true;
return false;
}
int main(){
int n,x;
cout<<"Enter how many numbers to enter: ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter no to push in stack: ";
cin>>x;
push(x);
}
pop();
print();
}
46 changes: 46 additions & 0 deletions Data Structure Algo/C++/stack using linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
node* top=NULL;
void push(int x){
node* temp=new node();
temp->data=x;
temp->next=NULL;
if(top==NULL){
top=temp;
return;
}
temp->next=top;
top=temp;
}
void pop(){
node* temp=top;
if(top==NULL){
cout<<"underflow";
return;
}
top=temp->next;
free(temp);
}
void print(){
node* temp=top;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main(){
int n,x;
cout<<"Enter n: ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter x: ";
cin>>x;
push(x);
}
pop();
print();
}