Wednesday, 28 August 2013

Circular Doubly Linked List & Output

//==========================================
// Name        : circulardoublyll.cpp
// Author      : your name
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//==========================================

#include <iostream>
#include<stdlib.h>

using namespace std;

class sll
{
private:
    struct node
    {
        int data;
        struct node *prev;
        struct node *next;
    }*head;
public:
    sll();
    void create();
    void display();
    void insert_first();
    void insert_rear();
};

sll::sll()
{
    head=NULL;
}

void sll::create()
{
    char ans;
    int flag=1;
    node *newnode,*temp;

    do
    {
        newnode=new node;
        cout<<endl<<"Enter the element : ";
        cin>>newnode->data;

        if(flag==1)
        {
            head=newnode;
            newnode->next=head;
            newnode->prev=head;
            flag=0;
        }
        else
        {
            temp=head;
            while(temp->next!=head)
                temp=temp->next;
            temp->next=newnode;
            newnode->prev=temp;
            newnode->next=head;
            head->prev=newnode;
        }

        cout<<"\nDo you want to enter more nodes ?";
        cin>>ans;
    }while(ans=='y'||ans=='Y');
}

void sll::display()
{
    node *temp;
    temp=head;
    if(temp==NULL)
        cout<<"\nSorry !!, The list is empty !!"<<endl;
    else
    {   cout<<"->>";
        do
        {
            cout<<"\t"<<temp->data;
            temp=temp->next;
        }while(temp!=head);
    }
}

void sll::insert_first()
{
    node *p,*temp;
    p=new node;
    cout<<"\n\nEnter the element you want to insert : ";
    cin>>p->data;

    if(head==NULL)
    {
        head=p;
        p->next=head;
        p->prev=head;
    }

    else
    {
        temp=head;
        while(temp->next!=head)
            temp=temp->next;
        temp->next=p;
        p->next=head;
        head->prev=p;
        p->prev=temp;
        head=p;
        cout<<"\nThe node is inserted";
    }
}

void sll::insert_rear()
{
    node *p,*temp;
    p=new node;

    cout<<"\n\nEnter the element you want to insert : ";
    cin>>p->data;

    if(head==NULL)
    {
        head=p;
        p->next=head;
        p->prev=head;
        insert_first();
        void insert_rear();
    }
    else
    {
        temp=head;
        while(temp->next!=head)
            temp=temp->next;
        temp->next=p;
        p->next=head;
        p->prev=temp;
        head->prev=p;
        cout<<"\nThe node is inserted";
    }
}

int main()
{
    sll l;
    char ans;
    int ch;
    do
    {
        cout<<endl<<"program for Circular Linked List"<<endl;
        cout<<"1. Creat a circular linked list"<<endl;
        cout<<"2. Display"<<endl;
        cout<<"3. Insert more node at front"<<endl;
        cout<<"4. Insert more node at rear"<<endl;
        cout<<endl<<"Enter your choice";
        cin>>ch;

        switch(ch)
        {
        case 1:l.create();
                break;
        case 2:l.display();
                break;
        case 3:l.insert_first();
                break;
        case 4:l.insert_rear();
                break;
        default:cout<<"\t\tWrong Entry !!!!!";
                break;
        }

        cout<<endl<<"do you want to go to the main manu to continue(y/n)";
        cin>>ans;
    }while(ans=='y'||ans=='y');

return 0;
}

                               Output of Program
program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice1

Enter the element : 11

Do you want to enter more nodes ?
y

Enter the element : 22

Do you want to enter more nodes ?
y

Enter the element : 33

Do you want to enter more nodes ?
y

Enter the element : 44

Do you want to enter more nodes ?
y

Enter the element : 55

Do you want to enter more nodes ?
n

do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice2
->>    11    22    33    44    55
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice3


Enter the element you want to insert : 00

The node is inserted
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice2
->>    0    11    22    33    44    55
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice4


Enter the element you want to insert : 66

The node is inserted
do you want to go to the main manu to continue(y/n)
y

program for Circular Linked List
1. Creat a circular linked list
2. Display
3. Insert more node at front
4. Insert more node at rear

Enter your choice2
->>    0    11    22    33    44    55    66
do you want to go to the main manu to continue(y/n)
n

Dictionary Program for insert,display,modify,search,delete & sort

//====================================
// Name        : dictionary.cpp
// Author      : your name
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//====================================

#include <iostream>
#include<string.h>
#include<stdlib.h>
#define SIZE 30

struct dict
{
    char keyword[20];
    char meaning[20];
}D[SIZE];

using namespace std;

int main()
{
    int choice,n=0;
    char ans;
    void init();
    int insert(int n);
    void display(int n);
    void modify(int n);
    void search(int n);
    void del(int n);
    void sort(int n);

    init();

    do
    {
        cout<<"Prpgram for dictionary";
        cout<<"\n\nDictionary Menu\n";
        cout<<"1.insert"<<endl<<"2.display"<<endl<<"3.Modify"<<endl<<"4.Search"<<endl<<"5.Delete"<<endl<<"6.sort";
        cout<<"\nEnter your choice\n";
        cin>>choice;
        switch(choice)
        {
        case 1:n=insert(n);
                break;
        case 2:display(n);
                break;
        case 3:modify(n);
                break;
        case 4:search(n);
                break;
        case 5:del(n);
                break;
        case 6:sort(n);
               display(n);
                break;
        default:cout<<"Wrong Entry !!!";
                break;
        }

        cout<<"Want to Enter more ??\n";
        cin>>ans;
    }while(ans=='y'||ans=='Y');

    return 0;
}

void init()
{
    int i;
    for(i=0;i<SIZE;i++)
        {
            strcpy(D[i].keyword,"");
            strcpy(D[i].meaning,"");

        }
}

int insert(int i)
{
    if(i<SIZE)
    {
        cout<<"\nEnter the keyword : ";
        cin>>D[i].keyword;
        cout<<"\nEnter the meaning : ";
        cin>>D[i].meaning;
        i++;
    }
    else
        cout<<"Database full";
    return i;
}

void display(int n)
{
    int i;
    for(i=0;i<SIZE;i++)
    {
        if(strcmp(D[i].keyword,"")!=0)
        {
            {
                cout<<D[i].keyword<<"\t\t"<<D[i].meaning<<endl;
            }
        }
    }
}

void search(int n)
{
    int i,flag=0;
    int count=0;
    char key[20];

    cout<<endl<<"Enter the keyword to be searched";
    cin>>key;

    for(i=0;i<n;i++)
    {
        count++;
        if(strcmp(D[i].keyword,key)==0)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
    {
        cout<<endl<<"Meaning : "<<D[i].meaning;
        cout<<endl<<"Totle comparisons are "<<count<<endl;
    }
    else
        cout<<endl<<"The keyword is not present";
}


void modify(int n)
{
    int i;
    char newkey[20],newmean[20];
    char key[20];

    cout<<endl<<"Enter the keyword for the entry to be modified";
    cin>>key;

    for(i=0;i<n;i++)
    {
        if((strcmp(D[i].keyword,key))==0)
        {
            cout<<"Enter the new keyword : "<<endl;
            cin>>newkey;
            cout<<"Enter the new meaning : "<<endl;
            cin>>newmean;
            strcpy(D[i].keyword,newkey);
            strcpy(D[i].meaning,newmean);
            cout<<endl<<"The entry is modified !!\n";
        }
    }
}


void del(int n)
{
    int i,flag=0;
    char key[20];
    cout<<endl<<"Enter the keyword to be deleted ";
    cin>>key;

    for(i=0;i<n;i++)
    {
        if(strcmp(D[i].keyword,key)==0)
        {
            strcpy(D[i].keyword,"");
            strcpy(D[i].meaning,"");
            flag=1;
        }
    }
    if(flag==0)
    {
        cout<<endl<<"The word is not in dictionary";
    }
}

void sort(int n)
{
    int i,j;
    char tempkey[20];
    char tempmean[20];

    for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
        {
            if(strcmp(D[i].keyword,D[j].keyword)>0)
            {
            strcpy(tempkey,D[i].keyword);
            strcpy(tempmean,D[i].meaning);

            strcpy(D[i].keyword,D[j].keyword);
            strcpy(D[i].meaning,D[j].meaning);

            strcpy(D[j].keyword,tempkey);
            strcpy(D[j].meaning,tempmean);

            }
        }
}


                                          Output of Program--

Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : bird

Enter the meaning : bird
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : cat

Enter the meaning : animal
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : dog

Enter the meaning : animal
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : ant

Enter the meaning : ant
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
2
bird        bird
cat        animal
dog        animal
ant        ant
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
6
ant        ant
bird        bird
cat        animal
dog        animal
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
3

Enter the keyword for the entry to be modifiedant
Enter the new keyword :
ant
Enter the new meaning :
animal

The entry is modified !!
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
4

Enter the keyword to be searchedcat

Meaning : animal
Totle comparisons are 3
Want to Enter more ??
n

Thursday, 22 August 2013

Binary Tree Traversal

from collections import deque

class Node:
    left , right, data = None, None, 0
 
    def __init__(self, data):
        # initializes the data members
        self.left = None
        self.right = None
        self.data = data

class BTree:
    def __init__(self):
        # initializes the root member
        self.root = None
 
    def addNode(self, data):
        # creates a new node and returns it
        return Node(data)

    def insert(self, root, data):
        # inserts a new data
        if root == None:
            # it there isn't any data
            # adds it and returns
            return self.addNode(data)
        else:
            # enters into the tree
            if data <= root.data:
                # if the data is less than the stored one
                # goes into the left-sub-tree
                root.left = self.insert(root.left, data)
            else:
                # processes the right-sub-tree
                root.right = self.insert(root.right, data)
            return root
     
    def pre_order_traversal(self, root, result=[]):
        if root == None:
            pass
        else:
            result.append(root.data)
            self.pre_order_traversal(root.left, result)
            self.pre_order_traversal(root.right, result)
        return result
 
    def in_order_traversal(self, root, result=[]):
        if root == None:
            pass
        else:
            self.in_order_traversal(root.left, result)
            result.append(root.data)
            self.in_order_traversal(root.right, result)
        return result

    def post_order_traversal(self, root, result=[]):
        if root == None:
            pass
        else:
            self.post_order_traversal(root.left, result)
            self.post_order_traversal(root.right, result)
            result.append(root.data)
        return result
 
    # breadth first traversal
    def bfs_traversal(self, root):
        if root == None:       # empty tree
            return

        # keep a queue which will hold all the nodes
        q = deque([root])

        while len(q) != 0:     # keep going until queue is empty
            node = q.popleft()
            if node == None:
                break
         
            print node.data
            if node.left != None:
                q.append(node.left)
            if node.right != None:
                q.append(node.right)

if __name__ == "__main__":
    # create the binary tree
    BTree = BTree()
    # add the root node
    root = BTree.addNode(0)
 
    # ask the user to insert values
    for i in range(0, 3):
        data = int(raw_input("insert the node value for node %d: " % i))
        # insert values
        BTree.insert(root, data)
 
    result = BTree.in_order_traversal(root, result=[])
    print "Inorder Traversal  "
    print ", ".join(map(str, result))
 
    result = BTree.pre_order_traversal(root, result=[])
    print "Preorder Traversal "
    print ", ".join(map(str, result))
 
    result = BTree.post_order_traversal(root, result=[])
    print "PostOrder Traversal "
    print ", ".join(map(str, result))
 
    print "BFS Traversal "



    BTree.bfs_traversal(root)