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

C++ Project (Program)

The document discusses various functions related to data structures in C++ including linked lists, stacks, and queues. It includes functions to create, traverse, search, insert, and delete nodes from linked lists, stacks, and queues. It also includes functions for a main menu to allow selection of operations on these different data structures.

Uploaded by

Suman Lata
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

C++ Project (Program)

The document discusses various functions related to data structures in C++ including linked lists, stacks, and queues. It includes functions to create, traverse, search, insert, and delete nodes from linked lists, stacks, and queues. It also includes functions for a main menu to allow selection of operations on these different data structures.

Uploaded by

Suman Lata
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Structure

#include<iostream.h> //for clrscr(),wherey(),getch() functions #include<conio.h> #include<stdio.h> #include<ctype.h> #include<dos.h> //for rand() #include<stdlib.h> void menu(); //FUNCTION FOR THE MAIN MENU //STRUCTURE DEFINATION FOR THE NODE struct node { int info; node *next; }; //GLOBAL DECLARATION FOR THE NODE POINTERS node *start,*ne,*move; //GLOBAL DECLARATION FOR THE LINKED STACK node *top; //GLOBAL DECLARATION FOR THE LINKED QUEUE node *front,*rear; //FUNCTION FOR CHECKING THE ROWS IN A WINDOW void check() { if (wherey()>15) { getch(); clrscr(); } } //FUNCTION FOR THE CREATION OF WINDOWS void win(int f,int s,int t) { clrscr(); window(1,1,25,80); textbackground(f);

Data Structure

clrscr(); window(20,5,62,23); textbackground(s); clrscr(); window(18,4,60,22); textbackground(t); textcolor(WHITE); clrscr(); } //FUNCTION FOR THE CREATION OF LINKED LIST void creation() { win(WHITE,BLACK,RED); start=NULL; char ch; do { ne=new node; check(); cout<<"\n\t\t\tEnter the value for the node:> "; cin>>ne->info; ne->next=NULL; if(start==NULL) start=ne; else move->next=ne; move=ne; check(); cout<<"\n\t\t\tDo You Wish to continue(Y/N):> "; cin>>ch; }while (toupper(ch)=='Y'); } //FUNCTION FOR THE TRAVERSAL OR PRINTING OF LINKED LIST void print() { clrscr(); win(WHITE,BLACK,RED); clrscr(); if (start==NULL) { cout<<"\n\n\t\t\t!!List is empty!!"; return;

} Data Structure cout<<"\n\n\t\t\t Contents of Linked List\n"; cout<<"\t\t\t ~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t\t are given below\n"; cout<<"\t\t\t ~~~~~~~~~~~~~~~\n\n"; move=start; while(move!=NULL) { check(); cout<<"\t\t\t\t\t"<<move->info<<endl; move=move->next; } } //FUNCTION FOR SEARCHING THE VALUE IN THE LINKED LIST void search() { win(WHITE,BLACK,RED); int no,pos=0; cout<<"\n\n\n\t\t\tEnter the value to search:> "; cin>>no; move=start; while(move!=NULL) { pos++; if (move->info==no) break; move=move->next; } if (move==NULL) cout<<"\n\n\t\t\t!!Number not found in the list!!"; else cout<<"\n\n\t\t\tPosition of the given number is:> "<<pos; } //FUNCTION FOR INSERTING THE NEW NODE AT THE BEGINNING OF LIST void b_insert() { ne->next=start; start=ne; } //FUNCTION FOR INSERTING THE NEW NODE AT THE END OF LIST

void e_insert() { move=start; while(move->next!=NULL) move=move->next; move->next=ne; }

Data Structure

//FUNCTION FOR INSERTING THE NEW NODE AT THE SPECIFIED POSITION void m_insert() { int pos; cout<<"\n\t\t Enter the position for the insertion:> "; cin>>pos; int i=0; move=start; while(move!=NULL) { i++; if(i==pos-1) break; move=move->next; } ne->next=move->next; move->next=ne; } //FUNCTION FOR DELTETION OF FIRST NODE IN THE LIST void b_deletion() { ne=start; start=start->next; ne->next=NULL; delete(ne); } //FUNCTION FOR THE DELETION OF LAST NODE IN THE LIST void e_deletion() { move=start; while(move->next!=NULL) { ne=move;

move=move->next; } ne->next=NULL; delete(move); } //FUNCTION FOR DELETION AT THE MIDDLE IN A LINKED LIST:void m_deletion() { Data Structure int n; node *t1,*t2; cout<<"\n\t\t\tEnter the position:>"; cin>>n; t1=start; t2=t1->next; int i=1; while(1<(n-1)) { t1=t1->next; t2=t2->next; i++; } t1->next=t2->next; t2->next=NULL; delete(t2); } //FUNCTION FOR THE INSERTION OPTIONS IN LINKED LIST void insertion() { win(WHITE,BLACK,RED); ne=new node; cout<<"\n\n\t\t Enter the value to insert in the list:> "; cin>>ne->info; ne->next=NULL; m: cout<<"\n\n\t\t\t\t**INSERTION MENU**\n\n"; cout<<"\t\t\t\t~~~~~~~~~~~~~~~~~~\n\n"; cout<<"\t\t\t1> At the beginning"<<endl<<endl; cout<<"\t\t\t2> At the end"<<endl<<endl; cout<<"\t\t\t3> At the given position"; int ch; cout<<"\n\n\t\t\tYour choice :> "; cin>>ch; switch(ch)

{ case 1: b_insert(); break; case 2: e_insert(); break; case 3: m_insert(); break; default : cout<<"\n\n\n\t\t\t!!!Invalid Choice!!!"; getch(); goto m; } } //FUNCTION FOR THE DELETION IN A LINKED LIST:void deletion() { win(WHITE,BLACK,RED); m: cout<<"\n\n\t\t\t\t**DELETION MENU**\n\n"; cout<<"\t\t\t\t~~~~~~~~~~~~~~~~~\n\n"; cout<<"\t\t\t1> First Node"<<endl<<endl; cout<<"\t\t\t2> Last Node"<<endl<<endl; cout<<"\t\t\t3> Given Position"; int ch; cout<<"\n\n\t\t\tYour Choice :> "; cin>>ch; switch(ch) { case 1: b_deletion(); break; case 2: e_deletion(); break; case 3: m_deletion(); break; default : cout<<"\n\n\n\t\t\t!!!Invalid Choice!!!"; getch(); goto m; } } //FUNCTION FOR THE MENU OF SINGLE LINKED LIST void list()

Data Structure

{ int ch; while(1) { win(WHITE,BLACK,RED); gotoxy(12,2); cout<<"**LINKED LIST OPERATONS**"; gotoxy(12,3); cout<<"~~~~~~~~~~~~~~~~~~~~~~~~"; gotoxy(8,4); cout<<"0> Return to Main Menu"; gotoxy(8,6); Data Structure cout<<"1> Creation of Linked List"; gotoxy(8,8); cout<<"2> Traversal of Linked List"; gotoxy(8,10); cout<<"3> Searching in Linked List"; gotoxy(8,12); cout<<"4> Insertion in Linked List"; gotoxy(8,14); cout<<"5> Deletion in Linked List"; gotoxy(25,18); cout<<"Your choice:> "; cin>>ch; switch(ch) { case 0: menu(); case 1: creation(); break; case 2: print(); break; case 3: search(); break; case 4: insertion(); break; case 5: deletion(); break; default: window(45,22,58,22); textbackground(BLACK); textcolor(WHITE+BLINK); clrscr(); cout<<"!!!Invalid choice!!!"; getch(); list(); }

getch(); } } //FUNCTION FOR THE CREATION OF STACK void s_creation() { char ans; top=NULL; win(WHITE,BLACK,CYAN); do { ne=new node; check(); Data Structure cout<<"\n\t\t Enter the element to push:> "; cin>>ne->info; ne->next=NULL; if (top!=NULL) ne->next=top; top=ne; check(); cout<<"\n\t\t Do You Wish to Continue (Y/N) :> "; cin>>ans; }while(toupper(ans)=='Y'); } //FUNCTION FOR THE TRAVERSAL OR PRINTING OF LINKED STACK void s_print() { clrscr(); win(WHITE,BLACK,CYAN); clrscr(); if (top==NULL) { cout<<"\n\n\t\t\t!!Stack is empty!!"; return; } cout<<"\n\n\t\t\t Contents of Linked Stack\n"; cout<<"\t\t\t ~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t\t\tare given below\n"; cout<<"\t\t\t\t~~~~~~~~~~~~~~~\n\n"; move=top; while(move!=NULL) {

check(); cout<<"\t\t\t\t\t"<<move->info<<endl; move=move->next; } } //FUNCTION FOR PUSHING THE ELEMENT INTO THE STACK void push(node *temp) { if (top==NULL) top=temp; else { temp->next=top; top=temp; } } // FUNCTION FOR THE POPPING AN ELEMENT FROM THE LINKED STACK int pop() { if (top==NULL) return -1; else { int t=top->info; move=top; top=top->next; move->next=NULL; delete move; return t; } } // FUNCTION FOR THE MENU OF THE STACK void stack() { int ch; while(1) { win(WHITE,BLACK,CYAN); gotoxy(12,2); cout<<"**STACK OPERATIONS**"; gotoxy(12,3); cout<<"~~~~~~~~~~~~~~~~~~~~";
Data Structure

gotoxy(8,4); cout<<"0> Return to Main Menu"; gotoxy(8,6); cout<<"1> Creation of the Stack"; gotoxy(8,8); cout<<"2> Traversal of the Stack"; gotoxy(8,10); cout<<"3> Push in the Stack"; gotoxy(8,12); cout<<"4> Pop in the Stack "; gotoxy(8,14); cout<<"\t\tYour choice:> "; cin>>ch; switch(ch) { case 0: menu(); case 1: s_creation(); break; case 2: s_print(); break; Data Structure case 3: clrscr(); ne=new node; if (ne==NULL) cout<<"\n\n\n\t\t\tStack is full"; else { cout<<"\n\t\t Enter the value for the element:> "; cin>>ne->info; ne->next=NULL; push(ne); cout<<"\n\n\n\t\t\t$ Element is pushed successfully $"; } break; case 4: int val=pop(); clrscr(); if (val==-1) cout<<"\n\n\n\t\t\tStack is empty"; else cout<<"\n\n\n\t\t Value of the popped element is:> "<<val; break; default:window(45,22,58,22); textbackground(BLACK);

textcolor(WHITE+BLINK); clrscr(); cout<<"!!!Invalid choice!!!"; getch(); stack(); } getch(); } } //FUNCTION FOR THE CREATION OF LINKED QUEUE void q_creation() { char ans; front=rear=NULL; win(WHITE,BLACK,YELLOW); do { ne=new node; check(); cout<<"\n\t\t Enter the element to insert:> "; cin>>ne->info; ne->next=NULL; if (front==NULL) Data Structure front=ne; else rear->next=ne; rear=ne; check(); cout<<"\n\t\t Do you wish to continue (Y/N) :> "; cin>>ans; }while(toupper(ans)=='Y'); } //FUNCTION FOR THE TRAVERSAL OR PRINTING OF LINKED QUEUE void q_print() { clrscr(); win(WHITE,BLACK,YELLOW); clrscr(); if (front==NULL) { cout<<"\n\n\t\t\t!!!No elements in the queue!!!";

return; } cout<<"\n\n\t\t\t Contents of Linked Queue\n"; cout<<"\t\t\t ~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t\t\tare given below\n"; cout<<"\t\t\t\t~~~~~~~~~~~~~~~\n\n"; move=front; while(move!=NULL) { check(); cout<<"\t\t\t\t\t"<<move->info<<endl; move=move->next; } } //FUNCTION FOR INSERTING THE ELEMENT INTO THE LINKED QUEUE void insert(node *temp) { if (front==NULL) front=temp; else rear->next=temp; rear=temp; } // FUNCTION FOR THE REMOVING AN ELEMENT FROM THE LINKED STACK int remove() { Data Structure if (front==NULL) return -1; else { int t=front->info; move=front; front=front->next; move->next=NULL; delete move; return t; } } // FUNCTION FOR THE MENU OF THE LINKED QUEUE void queue() {

int ch; while(1) { win(WHITE,BLACK,YELLOW); gotoxy(12,2); cout<<"**QUEUE OPERATIONS**"; gotoxy(12,3); cout<<"~~~~~~~~~~~~~~~~~~~~"; gotoxy(8,4); cout<<"0> Return to Main Menu"; gotoxy(8,6); cout<<"1> Creation of the Queue"; gotoxy(8,8); cout<<"2> Traversal of the Queue"; gotoxy(8,10); cout<<"3> Inserting in Queue"; gotoxy(8,12); cout<<"4> Deleting in Queue "; gotoxy(8,14); cout<<"\t\tYour choice:> "; cin>>ch; switch(ch) { case 0: menu(); case 1: q_creation(); break; case 2: q_print(); break; case 3: clrscr(); ne=new node; if (ne==NULL) cout<<"\n\n\n\t\t\tQueue is full"; Data Structure else { cout<<"\n\t\t Enter the value for the element:> "; cin>>ne->info; ne->next=NULL; insert(ne); cout<<"\n\n\t\t $ Element is inserted successfully $"; } break; case 4: int val=remove(); clrscr();

if (val==-1) cout<<"\n\n\n\t\t\t!!Queue is empty!!"; else cout<<"\n\n\n\t\t Value of the removed element is:> "<<val; break; default: window(45,22,58,22); textbackground(BLACK); textcolor(WHITE+BLINK); clrscr(); cout<<"!!!Invalid choice!!!"; getch(); queue(); } getch(); } } //FUNCTION FOR THE MAIN MENU OF DATA STRUCTURES void menu() { int choice; win(WHITE,BLACK,BLUE); gotoxy(12,3); cout<<"WELCOME TO THE WORLD OF "; gotoxy(12,4); cout<<"~~~~~~~~~~~~~~~~~~~~~~~"; gotoxy(17,5); cout<<"DATA STRUCTURES"; gotoxy(17,6); cout<<"~~~~~~~~~~~~~~~"; gotoxy(8,8); cout<<"1> LINKED LIST"; gotoxy(8,10); cout<<"2> STACK"; gotoxy(8,12); Data Structure cout<<"3> QUEUE"; gotoxy(8,14); cout<<"4> EXIT"; gotoxy(15,17); cout<<"YOUR OPTION:>"; gotoxy(30,17); cin>>choice; switch(choice)

{ case 1: list(); break; case 2: stack(); break; case 3: queue(); break; case 4: exit(0); default: window(45,22,58,22); textbackground(BLACK); textcolor(WHITE+BLINK); clrscr(); cout<<"!!!Invalid choice!!!"; getch(); menu(); } } void main() { clrscr(); menu(); }

You might also like