diff --git a/Data Structure Algo/C++/linked list.cpp b/Data Structure Algo/C++/linked list.cpp new file mode 100644 index 00000000..e4b572f8 --- /dev/null +++ b/Data Structure Algo/C++/linked list.cpp @@ -0,0 +1,96 @@ + +\#include +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;ilink; + } + 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;ilink; + } + 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<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<data<<"\t"; +} +int main(){ + int i,n,x,y; + head=NULL; + printf("How many nos? "); + scanf("%d",&n); + for(i=0;i +using namespace std; +bool prime(int n){ + for(int i=2;i>n; + if(prime(n)) cout<<"prime"; + else cout<<"non prime"; +} diff --git a/Data Structure Algo/C++/queue using arrray.cpp b/Data Structure Algo/C++/queue using arrray.cpp new file mode 100644 index 00000000..b3ca9d5e --- /dev/null +++ b/Data Structure Algo/C++/queue using arrray.cpp @@ -0,0 +1,48 @@ +#include +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<>n; + while(n--){ + cout<<"Enter x: "; + cin>>x; + enqueue(x); + } + print(); + dequeue(); + print(); +} diff --git a/Data Structure Algo/C++/queue using linkedlist.cpp b/Data Structure Algo/C++/queue using linkedlist.cpp new file mode 100644 index 00000000..c4913e97 --- /dev/null +++ b/Data Structure Algo/C++/queue using linkedlist.cpp @@ -0,0 +1,50 @@ +#include +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<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(); +} diff --git a/Data Structure Algo/C++/reverse linkedlist.cpp b/Data Structure Algo/C++/reverse linkedlist.cpp new file mode 100644 index 00000000..4d32eb47 --- /dev/null +++ b/Data Structure Algo/C++/reverse linkedlist.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +struct node{ + int data; + node* next; +}; +struct node* head; +void print(){ + node* temp=head; + while(temp->next!=NULL){ + cout<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; + } +} diff --git a/Data Structure Algo/C++/stack using array.cpp b/Data Structure Algo/C++/stack using array.cpp new file mode 100644 index 00000000..ba47ccec --- /dev/null +++ b/Data Structure Algo/C++/stack using array.cpp @@ -0,0 +1,43 @@ +#include +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<>n; + for(int i=0;i>x; + push(x); + } + pop(); + print(); +} diff --git a/Data Structure Algo/C++/stack using linkedlist.cpp b/Data Structure Algo/C++/stack using linkedlist.cpp new file mode 100644 index 00000000..4c0ba68d --- /dev/null +++ b/Data Structure Algo/C++/stack using linkedlist.cpp @@ -0,0 +1,46 @@ +#include +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<data<<" "; + temp=temp->next; + } +} +int main(){ + int n,x; + cout<<"Enter n: "; + cin>>n; + for(int i=0;i>x; + push(x); + } + pop(); + print(); +}