100% found this document useful (1 vote)
244 views6 pages

C++ Data Structures

The program provides functions to perform 3 tasks: (1) check if a word is a palindrome using a stack, (2) convert a singly linked list to a doubly linked list, and (3) insert and delete elements from a queue while finding and removing a target element. It includes functions for stack operations like push and display, creating and traversing singly/doubly linked lists, and queue operations like insert, display, and deleting a target element. The main menu allows the user to choose which task to perform.

Uploaded by

ZEESHAN HAIDER
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
244 views6 pages

C++ Data Structures

The program provides functions to perform 3 tasks: (1) check if a word is a palindrome using a stack, (2) convert a singly linked list to a doubly linked list, and (3) insert and delete elements from a queue while finding and removing a target element. It includes functions for stack operations like push and display, creating and traversing singly/doubly linked lists, and queue operations like insert, display, and deleting a target element. The main menu allows the user to choose which task to perform.

Uploaded by

ZEESHAN HAIDER
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

FROM: IQRA UNIBERSITY ISLAMABAD CAMPUS

Q: Write a program base on user inputs wich performs these three functions:
( 01 ) Check for Plindrome by using stack
( 02 ) convert singlty link list to a doubly link list
( 03 ) Insert elements in queue and then find and delete the target element

#include <iostream.h>
#include <process.h>
void STACK();
void doubly_link_list();
void Qeue();
#define size 100
int queue[size];
int rear=0;
void insert();
void del();
void Show();
const int SIZE=10000;
static char stack[SIZE];
int top=0;
void push(int c);
void show();
int w;
typedef struct item
{
int val;
item*prev;
int data;
item*next;
}item;
void main()
{
int choice;
cout<<"ENTER YOUR CHOICE \n\t1->Plindrome by using stack";
cout<<"\n\t2->Doubly link list \n\t3->Queue\n\t4->exit\n";
cout<<"YOUR CHOICE---> ";
cin>>choice;
while(choice)
{
switch(choice)
{
case 1:STACK();
break;
case 2:doubly_link_list();
break;
case 3:Qeue();
break;
case 4:
exit(0);
break;
default:
cout<<"\nINVALID OPTION!!!!!!!!!!!\n\n";
}
cout<<"ENTER YOUR CHOICE \n\t1->Plindrome by using Stack \n\t2->Doubly link list \n\t3->Queue\n\t4-
>exit\n";
cout<<"\tYOUR CHOICE---> ";
cin>>choice;
};
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void STACK()
{
char word[SIZE];
int i;
cout<<"NUMBER OF CHARACTERS IN YOUR WORDS ARE: ";
cin>>w;
cout<<"NOW ENTER WORD OF "<<w<<" CHARACTERS: \n";
for(i=0;i<w;i++)
{
cout<<"CHAR NO "<<i+1<<" ";
cin>>word[i];
}

char temp;
for(i=0;i<SIZE;i++)
{
temp=word[i];
push(temp);
}
show();//DISPLAY FUNCTION (OPTIONAL FUNCTION)
int v=w-1;
int u=w-1;
int h=-1;
for(i=0;i<w;i++)
{
if(word[i]==stack[v])
{
h++;
}

v--;
}
if(h==u)
{
cout<<"YES IT IS A PALINDROM\n";

}
else
{
cout<<"NO IT IS NOT A PALINDROM\n";
}

void push(int c)
{
stack[top]=c;
top++;
}
void show()
{

if(top<=0)
{cout<<"STACK is empty \n";}
else
cout<<"THE WORD IN STACK IS: ";
for(int i=0;i<w;i++)
{
if(stack[i]!='\0')
{cout<<stack[i];}

}
cout<<"\n\n";

}
////////////////////////////////////////////////
///////////////////////////////////////////////
void doubly_link_list()
{
int i,n;
item*head;
item*curr;
curr=new item;
head=curr;
head->next=NULL;
item*link;
link=new item;
link->prev=NULL;
cout<<"Enter numbers of nodes: ";
cin>>n;
if(n<=0)
{
cout<<"NOT POSSIBLE \n";
}
else
{
for(i=1;i<=n;i++)
{
curr=new item;
link=new item;
cout<<"ENTER INTEGER: ";
cin>>curr->val;
cout<<"\n NODE NUMBER "<<i<<" \n";
cout<<"BY USING SINGL LINK LIST ----->"<<curr->val<<" \n\n";
link->data=curr->val;
cout<<"BY USING DOUBLY LINK LIST----->"<<link->data<<" \n\n";
link->next=link;
link->prev=link->next;
curr->next=curr;
}
curr->next=NULL;
link->next=NULL;
}
}
//////////////////////////////////////////////
//////////////////////////////////////////////
void Qeue()
{
int option;
cout<<"\nENTER 1->Insert 2->Delete 3->Show 4->Go back to main() ";
cin>>option;
while(option)
{
switch(option)
{
case 1:insert();
break;
case 2:del();
break;
case 3:Show();
break;
case 4:main();
break;
default:cout<<"Invalid option!!!";

}
cout<<"\nENTER 1->Insert 2->Delete 3->Show 4->Go back to main() ";
cin>>option;
};
}
void insert()
{
if(rear>=(size-1))
{
cout<<"Queue is full\n\n";
}
cout<<"Enter element: ";
cin>>queue[rear];
if(rear==(size-1))
{
cout<<"\nQueue is full\n\n";
}
else
rear++;
}
void Show()
{
if(rear==0)
{
cout<<"queue is empty\n";
}
else
for(int i=0;i<size;i++)
{
if(queue[i]!=0)
{
cout<<" "<<queue[i];
}
}
}
void del()
{
int search;
if(rear==0)
{
cout<<"queue is empty\n";
}
cout<<"Enter element to delete ";
cin>>search;
for(int j=0;j<size;j++)
{ if(search==queue[j])
{
cout<<"ELEMENT FOUND AT POSITION "<<j+1<<"\n"<<"AND HAS BEEN DELETED "<<endl;
queue[j]=0;
for(int k=0;k<size;k++)
{
if(j<=k)
{
queue[k]=0;

}
}rear--;
break;
}

}
}

You might also like