Question

In: Computer Science

I am attempting to construct a queue through a linked list using a node data structure...

I am attempting to construct a queue through a linked list using a node data structure with different data types (char, int) in C++. Is this possible or can the queue be of only one data type? My queue has both names and numbers in it so what data type would be best? Can a "char" data type store integers? Can a "string" data type store integers?

Solutions

Expert Solution

If you want to construct a queue through a linked list using a node data structure with same data types (all the data types are charr or all the data types are int) then you should use template concept, But if you want to construct a queue data structure with different data types in the same queue, then you should use string type.

Yes it is possible if you use string type. Just remember, when you store other data type rather than string, then it is required to convert from other type to string, and when you read the data then also it is required to convert from string to other type.

If you use string type then you can store names as well as numbers, it is a best idea.

"char" data type can store limited integer value, only from -128 to +127. So if you store large number then you will lost the data. Therefore, do not try to store integer value to a char variable, except very low range integer value.

"string" data type can store other data including integer, but it may need some conversation.

Suppose, you want to read some integer value:

Example 1:

string s;

cout<<"Enter a number:";

cin>>s; //here if you enter an integer value then e.g 123 then 123 value will be into string variable s without conversation.

cout<<s; //print 123

Example 2:

int n;

cout<<"Enter a number:";

cin>>n; //here if you enter an integer value then e.g 123

string s = n; //illegal statement

string s = to_string(n); //you can use this statement or you can also use itoa() function for conversation

cout<<s; //print 123

Example 3:

string s = "123";

int n = s; //illegal statement

int n = stoi(s); //you can use this statement or you can also use atoi() function for conversation


Related Solutions

JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.
ONLY looking for part B!! a. Using C++, define a node structure of the linked list...
ONLY looking for part B!! a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list. b. Based on 2.a, (1) define a function which takes the header...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
What is a linked data structure? What is a node? What are the benefits of linked...
What is a linked data structure? What is a node? What are the benefits of linked structure? What are the drawbacks of linked structure? What are the differences between singly linked and doubly linked structures? Give examples of when a linked structure could be used.
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT