import java.io.*;
class Node
{
int data;
int priority;
Node link;
Node(int priority,int data)
{
this.data=data;
this.priority=priority;
}
}
class PriorityQueue
{
Node front;
BufferedReader br;
void creation()
{
int a,b;
boolean ch=true;
Node newnode;
do
{
try
{
System.out.print("enter the data for newnode : ");
a=Integer.parseInt(br.readLine());
System.out.println();
System.out.print("enter the priority for new node : ");
b= Integer.parseInt(br.readLine());
System.out.println();
newnode=new Node(b,a);
if(front==null||newnode.priority<front.priority)
{
newnode.link=front;
front=newnode;
}
else
{
Node temp=front;
while(temp.link!=null&&newnode.priority>=temp.link.priority)
temp=temp.link;
newnode.link=temp.link;
temp.link=newnode;
}
System.out.println("do u want to add one mode node [true/false] : ");
ch=Boolean.parseBoolean(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
while(ch);
}
void display()
{
Node temp=front;
if(temp==null)
System.out.println("list is empty");
else
{
while(temp!=null)
{
System.out.println(temp.priority+"--->"+temp.data);
temp=temp.link;
}
}
}
void delete()
{
Node temp=front;
if(temp==null)
System.out.println("list is empty");
else
{
System.out.println("deleted element is:" + front.data+" having a priority:"+front.priority);
front=front.link;
temp=null; /*whenever you are assining null to a reference of a class ,then the object associated with that reference is immediately deleted by the garbage collector due to automatic memory management */
display();
}
}
public static void main(String [] args)
{
int ch;
PriorityQueue pq=new PriorityQueue();
pq.br=new BufferedReader(new InputStreamReader(System.in));
try
{
while(true)
{
System.out.println("1.creation");
System.out.println("2.display");
System.out.println("3.delete");
System.out.println("4.exit");
System.out.print("Enter your choice : ");
ch=Integer.parseInt(pq.br.readLine());
System.out.println();
switch(ch)
{
case 1:
pq.creation();
break;
case 2:
pq.display();
break;
case 3:
pq.delete();
break;
case 4:
return;
default:
System.out.println("invalid choice...try again ");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
2.Stack implementation using single linked list in c language
program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void display();
typedef struct stack
{
int ele;
stack *link;
} node;
node *top=NULL;
void push(int x)
{
node *current=NULL;
current=(node *)malloc(sizeof(node));
if(current==NULL)
{
printf("\nmemory is not allocated properly");
return;
}
current->ele=x;
current->link=top;
top=current;
}
void length()
{
node *temp;
int len=0;
temp=top;
while(temp!=NULL)
{
++len;
temp=temp->link;
}
printf("\n the length of the stack is : %d",len);
}
void pop()
{
node *temp;
if(top==NULL)
printf("\n stack is empty ");
else
{
temp=top;
top=top->link;
printf("\n the deleted element is :%d\n",temp->ele);
free(temp);
}
}
void topmost()
{
if(top==NULL)
printf("\nstack is empty");
else
printf("top most element is : %d",top->ele);
}
void display()
{
if(top==NULL)
printf("\n stack is empty ");
else
{
node *temp;
temp=top;
printf("\n the stack elements are:");
while(temp!=NULL)
{
printf("%d<---",temp->ele);
temp=temp->link;
}
}
}
void main()
{
int i,ch;
clrscr();
ab:
printf("\n1.push\n2.pop\n3.topmost\n4.display\n5.length of stack\n6.exit\nenter your choice:");
scanf("%d",&i);
switch(i)
{
case 1:
printf("\n enter the element that you want to insert into d stack :");
scanf("%d",&ch);
push(ch);
break;
case 2:
pop();
break;
case 3:
topmost();
break;
case 4:
display();
break;
case 5:
length();
break;
case 6:
return;
default:
printf("\n invalid choice...try again..");
}
goto ab;
}
class Node
{
int data;
int priority;
Node link;
Node(int priority,int data)
{
this.data=data;
this.priority=priority;
}
}
class PriorityQueue
{
Node front;
BufferedReader br;
void creation()
{
int a,b;
boolean ch=true;
Node newnode;
do
{
try
{
System.out.print("enter the data for newnode : ");
a=Integer.parseInt(br.readLine());
System.out.println();
System.out.print("enter the priority for new node : ");
b= Integer.parseInt(br.readLine());
System.out.println();
newnode=new Node(b,a);
if(front==null||newnode.priority<front.priority)
{
newnode.link=front;
front=newnode;
}
else
{
Node temp=front;
while(temp.link!=null&&newnode.priority>=temp.link.priority)
temp=temp.link;
newnode.link=temp.link;
temp.link=newnode;
}
System.out.println("do u want to add one mode node [true/false] : ");
ch=Boolean.parseBoolean(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
while(ch);
}
void display()
{
Node temp=front;
if(temp==null)
System.out.println("list is empty");
else
{
while(temp!=null)
{
System.out.println(temp.priority+"--->"+temp.data);
temp=temp.link;
}
}
}
void delete()
{
Node temp=front;
if(temp==null)
System.out.println("list is empty");
else
{
System.out.println("deleted element is:" + front.data+" having a priority:"+front.priority);
front=front.link;
temp=null; /*whenever you are assining null to a reference of a class ,then the object associated with that reference is immediately deleted by the garbage collector due to automatic memory management */
display();
}
}
public static void main(String [] args)
{
int ch;
PriorityQueue pq=new PriorityQueue();
pq.br=new BufferedReader(new InputStreamReader(System.in));
try
{
while(true)
{
System.out.println("1.creation");
System.out.println("2.display");
System.out.println("3.delete");
System.out.println("4.exit");
System.out.print("Enter your choice : ");
ch=Integer.parseInt(pq.br.readLine());
System.out.println();
switch(ch)
{
case 1:
pq.creation();
break;
case 2:
pq.display();
break;
case 3:
pq.delete();
break;
case 4:
return;
default:
System.out.println("invalid choice...try again ");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
2.Stack implementation using single linked list in c language
program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void display();
typedef struct stack
{
int ele;
stack *link;
} node;
node *top=NULL;
void push(int x)
{
node *current=NULL;
current=(node *)malloc(sizeof(node));
if(current==NULL)
{
printf("\nmemory is not allocated properly");
return;
}
current->ele=x;
current->link=top;
top=current;
}
void length()
{
node *temp;
int len=0;
temp=top;
while(temp!=NULL)
{
++len;
temp=temp->link;
}
printf("\n the length of the stack is : %d",len);
}
void pop()
{
node *temp;
if(top==NULL)
printf("\n stack is empty ");
else
{
temp=top;
top=top->link;
printf("\n the deleted element is :%d\n",temp->ele);
free(temp);
}
}
void topmost()
{
if(top==NULL)
printf("\nstack is empty");
else
printf("top most element is : %d",top->ele);
}
void display()
{
if(top==NULL)
printf("\n stack is empty ");
else
{
node *temp;
temp=top;
printf("\n the stack elements are:");
while(temp!=NULL)
{
printf("%d<---",temp->ele);
temp=temp->link;
}
}
}
void main()
{
int i,ch;
clrscr();
ab:
printf("\n1.push\n2.pop\n3.topmost\n4.display\n5.length of stack\n6.exit\nenter your choice:");
scanf("%d",&i);
switch(i)
{
case 1:
printf("\n enter the element that you want to insert into d stack :");
scanf("%d",&ch);
push(ch);
break;
case 2:
pop();
break;
case 3:
topmost();
break;
case 4:
display();
break;
case 5:
length();
break;
case 6:
return;
default:
printf("\n invalid choice...try again..");
}
goto ab;
}
No comments:
Post a Comment