Skip to main content

Posts

C++ Doubly Linked List Traversal

#include<iostream> using namespace std; struct node { int data; struct node *prev; struct node *next; }; class dl { struct node *start; public: dl() { start=NULL; } void traverse_forward(); void traverse_reverse(); void insert_item(int item); void delete_item(); }; void dl::insert_item(int item) { struct node *p; p=(struct node *)malloc(sizeof(struct node)); p->prev=NULL; p->next=start; p->data=item; if(start!=NULL) start->prev=p; start=p; } void dl::traverse_forward() { struct node *temp; temp=start; while(temp!=NULL) { cout<<temp->data<<endl; temp=temp->next; } } void dl::traverse_reverse() { struct node *temp; temp=start; while(temp->next!=NULL) { temp=temp->next; } while(temp!=NULL) { cout<<temp->data<<endl; temp=temp->prev; } } int main() { dl obj; obj.insert_item(11); obj.insert_item(1...

C++ Linked List Implementation of Circular Queue

#include<iostream> using namespace std; class _queue_ { int queue[50]; int maxsize; int front; int rear; public: _queue_() { front=-1; rear=-1; maxsize=0; } _queue_(int size) { front=-1; rear=-1; maxsize=size; } void insert_item(int item); int delete_item(); void display(); }; void _queue_::insert_item(int item) { if(front==(rear+1)%maxsize) { cout<<"overflow"<<endl; exit(0); } else { if(rear==-1) front=(front+1)%maxsize; rear=(rear+1)%maxsize; queue[rear]=item; } } int _queue_::delete_item() { int item; if(front==-1&&rear==-1) { cout<<"Queue is empty\n"; exit(0); } else if(front>rear) { cout<<"Queue underflow\n"; exit(0); } else { item=queue[front]; front++; } return item; } void _queue_::display() { for(int i=front;i<=rear;i++) { cout<<"\n...

C+ Linked List Implementation of Queue

#include<iostream> using namespace std; struct node { int data; struct node *next; }; class queue_list { struct node *front,*rear; public: queue_list() { front=NULL; rear=NULL; } void insert_item(int item); int delete_item(); void display(); }; void queue_list::insert_item(int item) { struct node *p; p=(struct node*)malloc(sizeof(node)); p->data=item; if(front==NULL) { front=p; } else { while(rear->next!=NULL) { rear=rear->next; } rear->next=p; } p->next=NULL; rear=p; } void queue_list::display() { struct node* temp; temp=front; while(temp!=NULL) { cout<<temp->data<<"->"; temp=temp->next; } } int queue_list::delete_item() { struct node *temp; int item; if(front!=NULL) { temp=front; front=front->next; item=temp->data; free(temp); } else { cout<<"list is empty...

C++ Linked List Implementation of Stack

#include<iostream> using namespace std; struct node { int data; struct node *next; }; class stack_list { struct node *start; public: stack_list() { start=NULL; } void push(int item); int pop(); void display(); }; void stack_list::push(int item) { struct node *temp,*p; p=(struct node*)malloc(sizeof(node)); p->data=item; p->next=start; start=p; } void stack_list::display() { struct node *temp; int item; temp=start; while(temp!=NULL) { cout<<temp->data<<"->"; temp=temp->next; } } int stack_list::pop() { struct node *temp; int item; if(start!=NULL) { temp=start; start=start->next; item=temp->data; free(temp); } else { cout<<"list is empty"; } return(item); } int main() { stack_list stl; int a; stl.push(100); stl.push(200); stl.push(300); stl.push(400); stl.push(500); stl....

C++ Evaluation of Postfix Using Stack

#include<iostream> using namespace std; class stack { char st[50]; int maxsize; public: int tos; stack() { tos=-1; maxsize=0; } stack(int size) { tos=-1; maxsize=size; } char peek() { return (st[tos]); } void push(int item) { if(tos>=maxsize-1) { cout<<"stack overflow\n"; exit(0); } else { tos++; st[tos]=item; } } int pop() { int item; if(tos<0) { cout<<"stack underflow\n"; exit(0); } else { item=st[tos]; tos--; } return item; } void display() { for(int i=0;i<=tos;i++) { cout<<st[i]<<"\t"; } } }; int precedence(char ch) { if(ch=='(') return 0; if(ch=='-') return 1; if(ch=='+') return 2; if(ch=='*') return 3; if(ch=='/') return 4; return 7; } int main() { int flag=0; stack obj(20);...

C++ Linked List all Operations Revealed

#include<iostream> using namespace std; struct node { int data; struct node *prev,*next; }; class dlist { struct node *start; public: dlist() { start=NULL; } void insert_beg(int); void insert_end(int); void insert_after(int,int); void insert_before(int,int); void delete_beg(); void delete_end(); void delete_pos(int,int); void display_forward(); void display_reverse(); }; void dlist::insert_beg(int element) { struct node *p,*temp; p=(struct node*)malloc(sizeof(struct node)); p->data=element; p->next=start; p->prev=NULL; start=p; if(start!=NULL) start->prev=p; } void dlist::insert_end(int element) { struct node *p,*temp; p=(struct node*)malloc(sizeof(struct node)); p->data=element; if(start==NULL) { p->next=start; p->prev=start; start=p; } else { temp=start; while(temp->next!=NULL) { temp=temp->next; } p->prev=temp;...

C++ Linked List Reverse and Merge

deepak848207@gmail.com #include<iostream> using namespace std; struct node { int data; struct node *next; }; class _list_ { struct node *start1,*start2,*start3,*start4; public: _list_() { start1=NULL; start2=NULL; start3=NULL; start4=NULL; } void insert(int); void display(); void merge_list(); void insert1(int); void reverse_list(); }; void _list_::reverse_list() { struct node *temp; if(start3!=NULL) { while(start3!=NULL) { temp=start3; start3=start3->next; temp->next=start4; start4=temp; } temp=start4; while(temp!=NULL) { cout<<temp->data<<"->"; temp=temp->next; } } } void _list_::merge_list() { struct node *temp1,*temp2,*p; temp1=start1; temp2=start2; while(temp1!=NULL&&temp2!=NULL) { cout<<"hello"; p=(struct node*)malloc(sizeof(struct node)); if(temp1->data>temp2->data) { p...