Java program here I am stump with trying to fix this program we given. Basically
ID: 3834534 • Letter: J
Question
Java program here I am stump with trying to fix this program we given. Basically I need to convert it from using a linkedlist and have it run by using a stack.
Thanks for the help
Program
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Random;
class Player{
String playerCards[]=new String[5];
}
public class Cards {
public static void main(String[] args) throws FileNotFoundException,IOException{
int choice;
do{
BufferedReader in=new BufferedReader(new FileReader("<Enter the file path>"));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
LinkedList<String> card=new LinkedList<>();
String str;
while((str=in.readLine())!=null){//reading card from file
card.add(str);
}
Random ran=new Random();
LinkedList<String> suffledCard=new LinkedList<>();
while(card.size()!=0){//suffling the card
suffledCard.add(card.remove(ran.nextInt(card.size())));
}
System.out.println("Enter the number of player");
int n=Integer.parseInt(br.readLine());
Cards c=new Cards();
LinkedList<Player> players=new LinkedList<>();
for(int i=1;i<=n;i++){ //dealing the cards to player
Player p=new Player();
for(int j=0;j<5;j++){
p.playerCards[j]=suffledCard.removeFirst();
}
players.add(p);
}
//displaying cards of player
int i=1;
for(Player p:players){
System.out.println("Player "+(i++));
for(int j=0;j<5;j++){
System.out.print(p.playerCards[j]+" ");
}
System.out.println(" ");
}
System.out.println("Do you wish to continue?? Enter 1 for YES and 2 for NO");
choice=Integer.parseInt(br.readLine());
}while(choice==1);
}
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
/* structure of a linked list node */
struct node
{
int data;
struct node *next;
};
void deleteNode(struct node *head, struct node *n)
{
// When node to be deleted is head node
if(head == n)
{
if(head->next == NULL)
{
printf("There is only one node. The list can't be made empty ");
return;
}
/* Copy the data of next node to head */
head->data = head->next->data;
// store address of next node
n = head->next;
// Remove the link of next node
head->next = head->next->next;
// free memory
free(n);
return;
}
// When not first node, follow the normal deletion process
// find the previous node
struct node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
// Check if node really exists in Linked List
if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return;
}
// Remove node from Linked List
prev->next = prev->next->next;
// Free memory
free(n);
return;
}
/* Utility function to insert a node at the begining */
void push(struct node **head_ref, int new_data)
{
struct node *new_node =
(struct node *)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
/* Utility function to print a linked list */
void printList(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf(" ");
}
/* Driver program to test above functions */
int main()
{
struct node *head = NULL;
/* Create following linked list
12->15->10->11->5->6->2->3 */
push(&head,3);
push(&head,2);
push(&head,6);
push(&head,5);
push(&head,11);
push(&head,10);
push(&head,15);
push(&head,12);
printf("Given Linked List: ");
printList(head);
/* Let us delete the node with value 10 */
printf(" Deleting node %d: ", head->next->next->data);
deleteNode(head, head->next->next);
printf(" Modified Linked List: ");
printList(head);
/* Let us delete the the first node */
printf(" Deleting first node ");
deleteNode(head, head);
printf(" Modified Linked List: ");
printList(head);
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.