DATA STRUCTURE Programs








                           Drive link given below

1. Write a program to assign data to members of a structure variable and display using C ++ .

#include<iostream>
using namespace std;
struct stu_info
{
    string name;
    int id;
        struct marks
        {
            int midavg;
            int external;
        }m;
}s[2];
void insert()
{
    int i;
    for(i=0;i<2;i++)
    {
        cout<<"Enter name-";
        cin>>s[i].name;
        cout<<"enter Student id no.-";
        cin>>s[i].id;
        cout<<"midavg marks-";
        cin>>s[i].m.midavg;
        cout<<"external marks-";
        cin>>s[i].m.external;
    }
}

void display()
{
    int i;
    for(i=0;i<2;i++)
    {
        cout<<"Student name-";
        cout<<s[i].name;
        cout<<"\nStudent id no.-";
        cout<<s[i].id;
        cout<<"\n midavg marks-";
        cout<<s[i].m.midavg;
        cout<<"\n external marks-";
        cout<<s[i].m.external;
    }
}
int main()
{
    int ch;
    char c;
    do
    {
        cout<<"\n 1-Insert";
        cout<<"\n 2-Display";
        cout<<"\nEnter your choice-";
        cin>>ch;
        switch(ch)
        {
            case 1:insert();
            break;
            case 2:display();
            break;
            default:
            break;
        }
        cout<<"\n Do you want to continue (y/Y)-";
        cin>>c;
       
    } while(c=='y'||c=='Y');
    return 0;
   
}

output:-
1-Insert 2-Display Enter your choice-1 Enter name-mahesh enter Student id no.-2011 midavg marks-25 external marks-55 Enter name-hitesh enter Student id no.-2012 midavg marks-28 external marks-54 Do you want to continue (y/Y)-y 1-Insert 2-Display Enter your choice-2 Student name-mahesh Student id no.-2011 midavg marks-25 external marks-55Student name-hitesh Student id no.-2012 midavg marks-28 external marks-54 Do you want to continue (y/Y)-n

2. Implement stack operation using array in C ++.

#include<iostream>
#include<string.h>
using namespace std;
#define MAX 5

int top = -1;
void push(char);
char pop();
char STACK[MAX];

int main()
{
    char arr[6];
    int i;
    cout<<"ENTER THE STRING TO BE REVERSED "<<endl;
    cin>>arr;
   
    for(i=0;i<strlen(arr);i++)
    {
        push(arr[i]);
    }
    for(i=0;i<strlen(arr);i++)
    {
        arr[i]=pop();
    }
    cout<<"REVERSED STRING IS "<<endl;
    cout<<arr;
    return 0;
}

void push (char item)
{
    if(top >= MAX - 1)
    {
        cout<<"STACK OVERFLOWED";
        return;
    }
    else
    {
          top++;
        STACK[top]=item;
    }
}
char pop()
{
    char it;
    if(top==-1)
    {
        cout<<"STACK UNDERFLOWED";
        return 0;
    }
    else
    {
        it = STACK[top];
        top--;
        return it;
    }
}

output:-
ENTER THE STRING TO BE REVERSED hello REVERSED STRING IS olleh

3. Write a program to convert an infix operation to its postfix operation in 

C ++.

#include<iostream>
#include<stack>
using namespace std;

bool isOperator(char c)
{
    if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
    {
        return true;
    }
    else
    {
        return false;
    }
}

int precedence(char c)
{
    if(c == '^')
    return 3;
    else if(c == '*' || c == '/')
    return 2;
    else if(c == '+' || c == '-')
    return 1;
    else
    return -1;
}

string InfixToPostfix(stack<char> s, string infix)
{
    string postfix;
    for(int i=0;i<infix.length();i++)
    {
        if((infix[i] >= 'a' && infix[i] <= 'z')
        ||(infix[i] >= 'A' && infix[i] <= 'Z'))
        {
            postfix+=infix[i];
        }
        else if(infix[i] == '(')
        {
            s.push(infix[i]);
        }
        else if(infix[i] == ')')
        {
            while((s.top()!='(') && (!s.empty()))
            {
                char temp=s.top();
                postfix+=temp;
                s.pop();
            }
            if(s.top()=='(')
            {
                s.pop();
            }
        }
        else if(isOperator(infix[i]))
        {
            if(s.empty())
            {
                s.push(infix[i]);
            }
            else
            {
                if(precedence(infix[i])>precedence(s.top()))
                {
                    s.push(infix[i]);
                }  
                else if((precedence(infix[i])==precedence(s.top()))&&(infix[i]=='^'))
                {
                    s.push(infix[i]);
                }
                else
                {
                    while((!s.empty())&&( precedence(infix[i])<=precedence(s.top())))
                    {
                        postfix+=s.top();
                        s.pop();
                    }
                    s.push(infix[i]);
                }
            }
        }
    }
    while(!s.empty())
    {
        postfix+=s.top();
        s.pop();
    }
   
    return postfix;
}

int main()
{  

    string infix_exp, postfix_exp;
    cout<<"Enter a Infix Expression :"<<endl;
    cin>>infix_exp;
    stack <char> stack;
    cout<<"INFIX EXPRESSION: "<<infix_exp<<endl;
    postfix_exp = InfixToPostfix(stack, infix_exp);
    cout<<endl<<"POSTFIX EXPRESSION: "<<postfix_exp;
     
    return 0;
}

output:-
Enter a Infix Expression : a+b INFIX EXPRESSION: a+b POSTFIX EXPRESSION: ab+

4. Implement queue operation using array in C ++ .

#include<iostream>
using namespace std;
#define MAX 5
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();

int main()
{
    int option;
    cout<<"C program to implement queue operation";
    do{
        cout<<"\n 1. Insert an element-";
        cout<<"\n 2. Delete an element-";
        cout<<"\n 3. Display queue";
        cout<<"\n 4. Exit";
        cout<<"\n Enter Your choice-";
        cin>>option;
        switch(option)
        {
            case 1: insert_element();
                break;
            case 2:delete_element();
                break;
            case 3: display_queue();
                break;
            case 4: cout<<"Bye";
        }
    }while(option!=4);
}

void insert_element()
{
    int num;
    cout<<"Enter the number is to be inserted=";
    cin>>num;
    if(rear>=MAX-1){
                cout<<"\n Queue overflow Occured";
    }
    else{
        rear++;
        queue[rear]=num;
        if(front==-1){
                front=0;
        }
    }
}

void delete_element()
{
    int element;
    if(front==-1)
    {
        cout<<"\n underflow";
    }
    element=queue[front];
    if(front==rear)
    {
        front=rear=-1;
        cout<<"\n deleted element is :"<<element;
    }
    else{
        front++;
        cout<<"\nThe deleted element is:"<<element;
    }
}
void display_queue()
{
    int i;
    if(front==-1)
    {
        cout<<"\n No element to display";
    }
    else{
        cout<<"\n The queue element are:\n";
        for(i=front;i<=rear;i++)
        {
            cout<<queue[i];
        }
    }
}

output:-
C program to implement queue operation 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-1 Enter the number is to be inserted=10 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-1 Enter the number is to be inserted=20 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-3 The queue element are: 1020 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-2 The deleted element is:10 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-3 The queue element are: 20 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-4 Bye
5.Circular Queue Program in c++
#include<iostream>
using namespace std;
#define MAX 5
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();

int main()
{
    int option;
    cout<<"C program to implement queue operation";
    do{
        cout<<"\n 1. Insert an element-";
        cout<<"\n 2. Delete an element-";
        cout<<"\n 3. Display queue";
        cout<<"\n 4. Exit";
        cout<<"\n Enter Your choice-";
        cin>>option;
        switch(option)
        {
            case 1: insert_element();
                break;
            case 2:delete_element();
                break;
            case 3: display_queue();
                break;
            case 4: cout<<"Bye";
        }
    }while(option!=4);
}

void insert_element()
{
    int num;
    cout<<"Enter the number is to be inserted=";
    cin>>num;
    if(rear>=MAX-1){
               rear=0;
    }
    else{
        rear++;
    }
    if(front==rear)
    {
        cout<<"Queue Overflowed occured";
    }
    else{
        queue[rear]=num;
        if(front==-1)
        {
            front=0;
        }
    }
}

void delete_element()
{
    int element;
    if(front==-1)
    {
        cout<<"\n underflow";
        return;
    }
    element=queue[front];
    if(front==rear)
    {
        front=rear=-1;
    }
    else if(front==MAX-1)
        {
            front=0;
        }
    else
    {
        front++;
    }
    cout<<"\n deleted element is :"<<element;
   
}
void display_queue()
{
   int i,j;
        if(front==-1)
        {
            cout<<"No element to display";
        }
        else{
                if(front>rear)
                {
                    for(i=0;i<=rear;i++)
                    {
                        cout<<queue[i];
                     }
                    for(j=front;j<=MAX-1;j++)
                    {
                        cout<<queue[j];
                    }
                }
        else{
                 for(i=front;i<=rear;i++)
                 {
                    cout<<queue[i];
                 }
                cout<<"\nRear is at-"<<queue[rear];
                cout<<"\nfront is at-"<<queue[front];
            }
            cout<<"\n";
        }

}

output:-
C program to implement circular queue operation 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-1 Enter the number is to be inserted=10 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-1 Enter the number is to be inserted=20 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-1 Enter the number is to be inserted=30 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-3 102030 Rear is at-30 front is at-10 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-2 deleted element is :10 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-3 2030 Rear is at-30 front is at-20 1. Insert an element- 2. Delete an element- 3. Display queue 4. Exit Enter Your choice-4 Bye
6.Implement singly linked list and its operations.
#include<iostream>
#include<stdlib.h>
using namespace std;

struct node
{
        int info;
        struct node *link;
}*first=NULL,*first1=NULL;

struct node *INSERT_FRONT(int x,struct node *F)
{
        struct node *NEW = (struct node *)malloc(sizeof(struct node));
        NEW->info=x;
        NEW->link=F;
        return NEW;
}

struct node *INSERT_END(int x,struct node*F)
{
        struct node *SAVE;
        struct node * NEW =(struct node *)malloc(sizeof(struct node));
        NEW->info=x;
        NEW->link=NULL;
        if(F==NULL)
        {
                return NEW;
        }
        SAVE=F;
        while(SAVE->link!=NULL)
        {
                SAVE=SAVE->link;
        }
        SAVE->link=NEW;
        return F;
}

struct node *DELETE(int x,struct node*F)
{
        struct node *TEMP,*PRED;
        if(F==NULL)
        {
                cout<<"LIST UNDERFLOWED";
        }
        else
        {
                TEMP=F;
                while(TEMP->info!=x && TEMP->link!=NULL)
                {
                        PRED=TEMP;
                        TEMP=TEMP->link;
                }
                if(TEMP->info!=x)
                {
                        cout<<"THE NODE DOESN'T EXISTS ";
                }
                else
                {
                        if(x==F->info)
                        {
                                F=F->link;
                        }
                        else
                        {
                                PRED->link=TEMP->link;
                        }
                        return F;
                }
        }
}

void display(struct node *F)
{
        struct node *temp=F;
        while(temp!=NULL)
        {
                cout<< temp->info<<" ";
                temp= temp->link;
               
        }
}
int main()
{
        int data,choice;
        cout<<"SINGLY LINKED LIST PROGRAM\n";
do
{

        cout<<"\nPRESS 1 TO ENTER THE ELEMENT FROM FRONT"<<endl;
        cout<<"PRESS 2 TO ENTER THE ELEMENT FROM END"<<endl;
        cout<<"PRESS 3 TO DELETE THE ELEMENT "<<endl;
        cout<<"PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST"<<endl;
        cin>>choice;
        switch(choice)
        {
                case 1:  cout<<"ENTER THE ELEMENT TO BE INSERTED ";
                             cin>>data;
                             first=INSERT_FRONT(data,first);      
                             break;
                case 2 : cout<<"ENTER THE ELEMENT TO BE INSERTED ";
                             cin>>data;
                             first=INSERT_END(data,first);
                             break;
                case 3 : cout<<"ENTER THE ELEMENT TO BE DELETED ";
                             cin>>data;
                             first=DELETE(data,first);
                             break;
                case 4: display(first);
                            break;
}
}while(choice<6);
return 0;
}

output:-
SINGLY LINKED LIST PROGRAM PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 1 ENTER THE ELEMENT TO BE INSERTED 10 PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 1 ENTER THE ELEMENT TO BE INSERTED 20 PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 4 20 10 PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 2 ENTER THE ELEMENT TO BE INSERTED 30 PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 4 20 10 30 PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 3 ENTER THE ELEMENT TO BE DELETED 20 PRESS 1 TO ENTER THE ELEMENT FROM FRONT PRESS 2 TO ENTER THE ELEMENT FROM END PRESS 3 TO DELETE THE ELEMENT PRESS 4 TO DISPLAY THE ELEMENTS OF THE LIST 4 10 30
7.Implement Doubly linked list and its operations.
#include <iostream>
#include<stdlib.h>
using namespace std;
class node{
    public:

    int data;
    node* next;
    node* prev;

    node(int val){
        data=val;
        next=NULL;
        prev=NULL;
    }
};

void insertAtHead(node* &head,int val){
    node* n=new node(val);
    n->next=head;
    if(head!=NULL){
           head->prev=n;
    }
 

    head=n;
}
void insertAtTail(node* &head, int val){
    if(head==NULL){
        insertAtHead(head,val);
        return;
    }
    node* n=new node(val);
    node* temp=head;

    while(temp->next!=NULL){
        temp=temp->next;
    }

    temp->next=n;
    n->prev=temp;
}

void display(node* head){
    node* temp=head;
    while(temp!=NULL){
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
}

void deleteAtHead(node* &head){

    node* todelete=head;
    head=head->next;
    head->prev=NULL;

    delete todelete;
}
void deletion(node* &head,int pos){
 
  if(pos==1){
    deleteAtHead(head);
    return;
  }
    node* temp=head;
    int count=1;

    while(temp!=NULL && count!=pos){
        temp=temp->next;
        count++;
    }

    temp->prev->next=temp->next;
    if(temp->next!=NULL){
        temp->next->prev=temp->prev;
    }

    delete temp;
}
int main(){

        node* head=NULL;
        insertAtTail(head,1);
        insertAtTail(head,2);                    SOURCE:- APNA COLLEGE
        insertAtTail(head,3);
        insertAtTail(head,4);
        display(head);
        insertAtHead(head,5);
        display(head);
        deletion(head,1);
        display(head);
    return 0;
}

OUTPUT:
1 2 3 4 5 1 2 3 4 1 2 3 4

FOR EXPLANATION FOLLOW LINK:


For Downloading the Code Google Dive Link Is given below 



Comments