MS Rumi. Powered by Blogger.
Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Tuesday, 21 October 2014

Free Download Project Of Linked List In C++

Linked List Basic And Simple Program 

In this program user can have six functions to select. User can Add  Node In Beginning,  In Last  can display link list, user can delete the node from specific position and also can add node for specific position in linked list and user can reverse complete linked list, finally user will have exit option to exit program.   

Header Files For Linked List Function 

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip.h>   // For Set word

// structre for node

struct node{
    int value;
    node *next;
}*start=NULL;

int count=0;// Global Integer For Counting

// Function To Create Node In Link List (C++)

node* creatNode(int data)
{
    node *temp;
    temp= new node;  // Creating Node
    temp->value=data; //assiging value
    temp->next=NULL;
    return temp;
}

// Function To Add Node At Given Position In  Linked List C++

void addNode()
{
    int value,pos=0,key;
    node *temp=start;
    node *newNode;
    node *pre=start;
    if(start==NULL)
    {
        cout<<"Enter value:";
        cin>>value;
        temp=creatNode(value);
        start=temp;
        start->next=NULL;
        return;
    }
    cout<<"Enter Position To Add New Node:";
    cin>>key;
    if(pos==key)
    {
        cout<<"Enter value:";
        cin>>value;
        temp=creatNode(value);
        start=temp;
        start->next=pre;
        return;
    }
    else
    {
        int i=1;
        while(temp!=NULL)
        {   
            pre=temp;
            temp=temp->next;
            if(i==key)
            {
                cout<<"Enter value:";
                cin>>value;
                newNode=creatNode(value);
                newNode->next=temp;
                pre->next=newNode;
                return;
            }
            i++;
        }
    }
    cout<<"Node Position Is Not Available";   
   

}

// Function To Add Node At Beginning of Linked List C++


void insertBeg()
{
    int value;
    node *temp, *pre;
    cout<<"Enter value:";
    cin>>value;
    temp=creatNode(value);
    if(start==NULL)
    {
        start=temp;
        start->next=NULL;
    }
    else
    {
        pre=start;
        start=temp;
        start->next=pre;

    }

}

// Function To Add Node At Last of Linked List C++

void insertLast()
{
    int value;
    node *temp, *last;
    cout<<"Enter value:";
    cin>>value;
    temp=creatNode(value);//Storing addres of created Node
    if(start==NULL) // If there is no node
    {
        start=temp; 
    }
    else
    {   
        last=start;
        while(last->next!=NULL) // Checking Adress in Next
        {
            last=last->next;   //Moving Pointer to Last
        }
        temp->next=NULL; // To Make It Last
        last->next=temp;    // Connecting Last Node To New Node
    }
       

   

}



// Function To Delete Node From Linked List C++




void deleteNode(int num)
{
    int pos=1;
    node *temp, *pre;
    temp=start;            // assigning start node address to temp
    if(temp==NULL)   // Checking list is empty?
    {
        cout<<"*************List Is empty************";
        return;
    }
    if(num==pos)                    // If user want to delte first node
    {
        if(temp->next==NULL)    // checking that is first node is last?
        {
            start=NULL;        // List Will Become Empty
            cout<<"There Was Only One Node ... Now List Is Empty";
            return;
        }
        else
        {
            start=temp->next;
            return;
        }
    }
    while (temp!=NULL)
        {
            pos++;
            pre=temp;
            temp=temp->next;
           
            if(pos==num)
            {
                if(temp->next=NULL)
                {
                    pre->next=NULL;
                    cout<<"You Deleted Last Node Of Your List..."<<endl;
                    return;
                }
                else
                {
                    pre->next=temp->next;
                    return;
                }
            }
        }
}



// Displaying Linked List Function In C++

void display()
{
    node *temp;
    int num=0;
    temp=start;
    if(temp==NULL)
    {
        cout<<"Link List Is Empty...."<<endl;
        return;
    }
    cout<<"Node #"<<setw(10)<<"Value"<<endl;
    while(temp!=NULL)
    {
        cout<<setw(2)<<num<<setw(11)<<temp->value<<endl;
        temp=temp->next;
        num++;
        count++;
    }
    cout<<endl;
}


// Function Reverse Linked List C++

void reverse ()
{
    node *frw;
    node *pre=NULL;
    node *curent=start;
    while(curent!=NULL)
    {
        frw=curent->next;
        curent->next=pre;
        pre=curent;
        curent=frw;
    }
    start=pre;

}


// Main Function
int main ()
{
    int option,num;
    while(1)
    {
        system("CLS");
        cout<<endl;
        cout<<"*****Select Option********"<<endl;
        cout<<"* 1. Add In Begining     *"<<endl;
        cout<<"* 2. Add In Last         *"<<endl;
        cout<<"* 3. Display Link List   *"<<endl;
        cout<<"* 4. Delete Node         *"<<endl;
        cout<<"* 5. Reverse Node        *"<<endl;
        cout<<"* 6. Add Node At Nth Pos *"<<endl;
        cout<<"* 7. Exit                *"<<endl;
        cout<<"**************************"<<endl;
        cout<<"Enter Option From Above:";
        cin>>option;
        switch(option)
        {
        case 1:
            {
                insertBeg();
            }
            break;
        case 2:
            {
                insertLast();
            }
            break;
        case 3:
            {
                display();
                cout<<"\n Total Nodes"<<count;
                system("pause");
            }
            break;
        case 4:
            {
                display();
                if(start)
                {
                    cout<<"Enter Node Number To Delete:";
                    cin>>num;
                    deleteNode(num);
                    display();
                    system("pause");
                }
                else
                    system("pause");
            }
            break;
        case 5:
            {
                cout<<"Before Reverse"<<endl;
                display();
                reverse();
                cout<<"After Reverse"<<endl;
                display();
                system("pause");
            }
            break;
        case 6:
            {
                display();
                addNode();
            }
            break;
        case 7:
            {
                return 0;
            }
            break;
        default:
            cout<<"Invalid Option"<<endl;
        }
    }
}


If you have any issue regarding to this linked list program, please ask in the comment section.
Or if you have any question about linked list coding or other c++ programing issue post it here I will give you solution of your problem with explanation. 

Sunday, 19 October 2014

Bubble Sort With Pointer

Bubble Sort With Pointer In Data Structure

C++ Coding In Microsoft Visual 6

#include<iostream.h>
void bubleSort (int *arr);
//Main Function
void main ()
{
    int arr[5];
    cout<<"Enter Numbers To Store In Array"<<endl;
    for(int i=0; i<5; i++)
    {
        cin>>arr[i];
    }
   
   
    cout<<"Beofre Sorting"<<endl;
    for(i=0; i<5; i++)
    {
        cout<<arr[i]<<",";
    }

    //sorting array
    bubleSort(arr);
   
    cout<<"After Sorting"<<endl;
    for(i=0; i<5; i++)
    {
        cout<<arr[i]<<",";
    }

}

// Sorting Function
void bubleSort (int *arr)
{
    for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                if(arr[j]>arr[j+1])
                {
                    arr[j]=arr[j]+arr[j+1];
                    arr[j+1]=arr[j]-arr[j+1];
                    arr[j]=arr[j]-arr[j+1];
                }
            }
        }
}

Sunday, 11 May 2014

Quick Sort In C++ With Example

Quick Sort Algorithm

Quick sort is invented by C.A.R Hoare and its very useful sorting algorithm. This algorithm has two parts 1st is partition part and 2nd is sorting part. In the first part most of the work is done where it divide the work and the 2nd part simple sort the problems.
It use divide and conquer to get the result same like the marge sort without using extra storage. In the quick sort we have to select a value, which is called pivot value. There are different ways to select the pivot value(first value, last, middle, it has no criteria ), here I will select the first value in the list and it is also called the split point. After selecting pivot value we will select two value leftmark and rightmark from start and end of the remaining elements. The role of the partition is to move the element those are out of order. 
From the leftmark we will locate a value which is greater then the pivot value and on the other hand from the rightmark we decrements until we find a value which is less then pivot value. Pivot value is 54, leftmark is 26 and rightmark is 20 so leftmark is not greater then pivot value so we will move to next element of the list and rightmark is less then the pivot value so it will not move. Now left mark is on 93 and rightmark is on 20, both elements are out of order so we will swap them and repeat the process. When rightmark is less then leftmark we will stop and replace rightmark with pivot.
Now we have small number on the left and greater number on the right side of the pivot value. The list can be divided and the quick sort invoked on the both half. 

C++ Code Of Quick Sort With Out Put

#include<iostream.h>
void quickSort (int *arr, int left, int right);
void print (int *arr);
void main ()
{
int arr[10];
cout<<"Enter Ten Unsorted Numbers";
for(int i=0;i<=9;i++)
{
cout<<"Enter Number "<<i+1<<":";
cin>>arr[i];
}
int left=0; // left is the start of the array
int right=9; // right is the end of the array 

quickSort(arr,left,right);

//After Sorting
print(arr);
}


void quickSort (int *arr, int left, int right)
{

int leftmark=left, rightmark=right, temp;
// selecting pivot 
int pivot=arr[0];


// Partition 
while(leftmark<=rightmark)
{
while(leftmark<pivot)
leftmark++;
while(rightmark>pivot)
rightmark--;
if(leftmark<=rightmark)
{
temp=arr[leftmark];
arr[leftmark]=arr[rightmark];
arr[rightmark]=temp;
leftmark++;
rightmark--;
}
};

if(left<rightmark)
quickSort(arr, left, rightmark);
if(leftmark<right)
quickSort(arr,leftmark,right);
}


void print (int *arr)
{
for ( int i = 0; i < 10; i++ )
        cout << arr[i] << " ";
   cout << endl;
}

Thursday, 8 May 2014

Structure in C++ With Example

What is Structure?

Collection of variable under same name and these variable can be different type (int, float, char etc) and each have a name that is used to selected from structure. Structure is user define data type which can combine different data types.

How to create structure in c++?

To create structure you must use struct statement and also called keyword after that there is a tag which is user defined, structure Name. Then there are structure member with their data type and variable name. At the end close the struct body with braces and semicolon };.

Example of Structure

Write A Program to get 4 students data (Name, Course, Age, GPA) and print average age, average GPA  and maximum GPA.

#include<iostream.h>
void main ()
{
//Struct Defination 
struct Student
{
char name[20];
char course[10];
int age;
float gpa;
};

float sumage=0,sumgpa=0,max=0,avgGpa,avgAge;
int sno;
//Struct Variable as array
Student std[4];
cout<<"Enter Students Data"<<endl;

for(int i=0; i<4; i++)
{
cout<<"**Student No "<<i+1<<"**"<<endl;

cout<<"Enter Name:";
cin>>std[i].name;

cout<<"Enter course:";
cin>>std[i].course;

cout<<"Enter age:";
cin>>std[i].age;
//Suming Age for average
sumage=std[i].age+sumage;

cout<<"Enter GPA:";
cin>>std[i].gpa;
//Sum GPA for average
sumgpa=std[i].gpa+sumgpa;
//For Maximum GPA
if(max<std[i].gpa)
{
max=std[i].gpa;
sno=i;
}
}

//Average 
avgAge=sumage/4;
avgGpa=sumgpa/4;

cout<<"Age Average Is:"<<avgAge<<endl;
cout<<"GPA Average Is:"<<avgGpa<<endl;
cout<<std[sno].name<<" Got Maximum GPA Is:"<<max;
}

OutPut of Example 



E-mail Newsletter

Sign up now to receive breaking news and to hear what's new with us.

Recent Articles

TOP