Showing posts with label linklist. Show all posts
Showing posts with label linklist. Show all posts

Thursday, 30 November 2017

Circular Linked List Implementation Data Structure in C/ C++

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *link;
};
void insert(struct node **start,int num)
{
cout<<"First";

node *temp=new node;
temp->info=num;

temp->link=NULL;

(*start)=temp;

}
void insertEnd(node *start,int num)
{
cout<<"\nLast:";
node *temp;
while(start->link!=NULL)
{
start=start->link;
}
temp=new node;
temp->info=num;
temp->link=NULL;
start->link=temp;

}

void makeCircular(node *start)
{
node *temp=start;
while(start->link!=NULL)
{

    start=start->link;
}
if(start->link==NULL)
{
start->link=temp;
}
}
void disp1(node *start)
{
node *temp=start;
while(temp->link!=start)
{
cout<<" "<<temp->info;
    temp=temp->link;
}
cout<<" "<<temp->info;
}


main()
{
struct node *start=NULL;

int num,x,y;
cout<<"In Main";
insert(&start,1);
insertEnd(start,5);
insertEnd(start,3);
insertEnd(start,4);
insertEnd(start,6);
insertEnd(start,7);
insertEnd(start,8);
insertEnd(start,9);
insertEnd(start,10);

makeCircular(start);
disp1(start);

}

Wednesday, 29 November 2017

Alternate Node Deletion in Singly Linked List in C/C++ Implementation

#include<iostream>
using namespace std;

struct node
{
int info;
node *link;
};

void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}

void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}

void deleteAlternate(node *start)
{
node *currnext=start->link;
node *nexttonext=start->link->link;
node *curr=start;
while(curr!=NULL &&curr->link!=NULL && curr->link->link!=NULL)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
curr=curr->link;
}
}




void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\n";
}





main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
        cout<<"Before Deleting Linked List\n:";
disp(start);
deleteAlternate(start);
        cout<<"After Deleting Alternate Node:\n";
disp(start);
}

Insertion into Linked List after an element specified by user in C++/C

#include<iostream>
using namespace std;

struct node
{
int info;
node *link;
};

void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}

void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}

void insertAtPos(node *start,int num,int num1)
{
node *temp=new node;
temp->info=num1;
temp->link=NULL;
node *prev=start;
while(start!=NULL)
{
if(start->info==num)
break;
else
prev=start;
start=start->link;
}
temp->link=start->link;
start->link=temp;

}

void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
}




main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
insertAtPos(start,2,6);
disp(start);
}


Header Linked List - Implementation in C++/C

#include<bits/stdc++.h>

using namespace std;

struct node
{
int info;
node *link;
};
struct header
{
node *next;
int total=0;
};

void insertFirst(node **start,int data,header *head)
{
node *temp=new node;
temp->info=data;
temp->link=NULL;
*start=temp;
head->next=temp;
head->total+=1;
}
void insertLast(node *start,header *head,int data)
{
node *temp=new node;

while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
temp->link=NULL;
temp->info=data;
head->total+=1;
}

void display(node *start,header *head)
{
cout<<"Elements are:\n";
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\nTotal Number Of Elements Are:"<<head->total;
}

void deleteAlternate(node *start,header *head)
{
cout<<"\nIn Delete Function:\n";
node *nexttonext;
node *curr=start;
while(curr!=NULL&&curr->link!=NULL)
{


if(curr->link->info%2!=0)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
head->total-=1;
curr=curr->link;
}


}


}
main()
{
int i=0;
char ch='y';
node *start=NULL;
header *head =new header;
cout<<"First Element Entered:\n";
while(ch=='y'||ch=='Y')
{

if(i==0)
insertFirst(&start,i,head),i++;
else
insertLast(start,head,i),i++;

cout<<"Do You Want More Record:";
cin>>ch;
}
display(start,head);
deleteAlternate(start,head);
display(start,head);
}




Friday, 10 November 2017

Program to Implement Insertion In Heap in C/C++

// Insertion In Heap Tree Data Structure

#include<bits/stdc++.h>
using namespace std;

int arr[10],n;

void insertHeap(int num,int loc)
{


int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}

arr[loc]=arr[par];
loc=par;
}

arr[0]=num;
}
void disp(int n)
{
for(int i=0;i<n;i++)
cout<<" "<<arr[i];
}


main()
{
n=0;

insertHeap(50,n);
n+=1;
insertHeap(30,n);
n+=1;
insertHeap(40,n);
n+=1;
insertHeap(29,n);
n+=1;
insertHeap(55,n);
n+=1;
cout<<"Insertion In HEAP: ";
disp(n);


}



Monday, 6 November 2017

C program to Implement Priority Queue Menu Driven Program

/*Program of priority queue using linked list*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
int priority;
int info;
struct node *link;
}*front=NULL;

void insert(int item, int item_priority);
int del();
void display();
int isEmpty();

main()
{
int choice,item,item_priority;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("03.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Input the item to be added in the queue : ");
scanf("%d",&item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
insert(item, item_priority);
break;
case 2:
printf("Deleted item is %d\n",del());
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

void insert(int item,int item_priority)
{
struct node *tmp,*p;

tmp=(struct node *)malloc(sizeof(struct node));
if(tmp==NULL)
{
printf("Memory not available\n");
return;
}
tmp->info=item;
tmp->priority=item_priority;
/*Queue is empty or item to be added has priority more than first element*/
if( isEmpty() || item_priority < front->priority )
{
tmp->link=front;
front=tmp;
}
else
{
p = front;
while( p->link!=NULL && p->link->priority<=item_priority )
p=p->link;
tmp->link=p->link;
p->link=tmp;
}
}/*End of insert()*/

int del()
{
struct node *tmp;
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
else
{
tmp=front;
item=tmp->info;
front=front->link;
free(tmp);
}
return item;
}/*End of del()*/

int isEmpty()
{
if( front == NULL )
return 1;
else
return 0;

}/*End of isEmpty()*/

void display()
{
struct node *ptr;
ptr=front;
if( isEmpty() )
printf("Queue is empty\n");
else
{    printf("Queue is :\n");
printf("Priority       Item\n");
while(ptr!=NULL)
{
printf("%5d        %5d\n",ptr->priority,ptr->info);
ptr=ptr->link;
}
}
}/*End of display() */

C program to Implement Queue using Stacks in C++

/*
 * C Program to Implement Queues using Stacks
 */
#include <stdio.h>
#include <stdlib.h>

void push1(int);
void push2(int);
int pop1();
int pop2();
void enqueue();
void dequeue();
void display();
void create();

int st1[100], st2[100];
int top1 = -1, top2 = -1;
int count = 0;

void main()
{
    int ch;

    printf("\n1 - Enqueue element into queue");
    printf("\n2 - Dequeu element from queue");
    printf("\n3 - Display from queue");
    printf("\n4 - Exit");
    create();
    while (1)
    {
        printf("\nEnter choice");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            enqueue();
            break;
        case 2:
            dequeue();
            break;
        case 3:
            display();
            break;
        case 4:
            exit(0);
        default:
            printf("Wrong choice");
        }
    }
}

/*Function to create a queue*/
void create()
{
    top1 = top2 = -1;
}

/*Function to push the element on to the stack*/
void push1(int data)
{
    st1[++top1] = data;
}

/*Function to pop the element from the stack*/
int pop1()
{
    return(st1[top1--]);
}

/*Function to push an element on to stack*/
void push2(int data)
{
    st2[++top2] = data;
}

/*Function to pop an element from th stack*/

int pop2()
{
    return(st2[top2--]);
}

/*Function to add an element into the queue using stack*/
void enqueue()
{
    int data, i;

    printf("Enter data into queue");
    scanf("%d", &data);
    push1(data);
    count++;
}

/*Function to delete an element from the queue using stack*/

void dequeue()
{
    int i;

    for (i = 0;i <= count;i++)
    {
        push2(pop1());
    }
    pop2();
    count--;
    for (i = 0;i <= count;i++)
    {
        push1(pop2());
    }
}

/*Function to display the elements in the stack*/

void display()
{
    int i;

    for (i = 0;i <= top1;i++)
    {
        printf(" %d ", st1[i]);
    }
}

Amazon1Ads