Question

C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

C++

pointers and linked lists

1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable
named someInt.

Assign the value 451 to someInt and output (cout) the variable someInt and output (cout)
the value pointed to by intPointer.

Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer.

Output (cout) the value pointed to by intPointer and output (cout) the variable someInt,


2. Declare a pointer variable charArrPointer and initialize it to point to the first element
of a three-element char array named initials.

Write assignment statements to store 'A', 'E', and 'W' into the first three elements of
the array POINTED to by charArrPointer. Do NOT store any values directly into the array named
initials.

3. Output (cout) the data in array charArrPointer and the data in array initials.

4. Copy the following code into your program above main:

struct NodeType {
int num;
NodeType* next;
};

5. Copy the following code into your program after main:

NodeType *newNode, *first, *last, *current;

6. Copy the following code into your program after the code you wrote for steps 1 - 3:

newNode = new NodeType;
newNode->num = 10;
newNode->next = NULL;

first = newNode;
last = newNode;

newNode = new NodeType;
newNode->num = 20;
newNode->next = NULL;

last->next = newNode;
last = newNode;

newNode = new NodeType;
newNode->num = 30;
newNode->next = NULL;
  
last->next = newNode;
last = newNode;


5. Create a new NodeType node. Assign the value 1 to the num data member.
Write the code to insert the new node at the beginning of the list.

6. Write the code to print the values of the data member num in the entire linked list.

  

If you like to include the following line of code: system("pause");
You MAY need to: #include <cstdlib>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

#include <iostream>

using namespace std;

struct NodeType {

int num;

NodeType* next;

};

int main() {

// Integer pointer

cout << "Ques 1 :";

int someInt;

int *intPointer;

someInt = 45;

intPointer = &someInt;

cout << *intPointer << endl;

// Char Arr pointer

cout << "Ques 2 :";

char *charArrPointer;

char initials[3];

char temp = 'A';

initials[0] = temp;

initials[1] = temp+4;

initials[2] = temp+22;

charArrPointer = initials;

cout << endl;

// Output the char

cout << "Ques 3 :";

cout << charArrPointer << " " << (charArrPointer+1) << " " << *(charArrPointer+2) << endl;

// Node

cout << "Ques 4,5,6 :";

NodeType newNode, first, last, current;

newNode = new NodeType;

newNode->num = 10;

newNode->next = NULL;

first = newNode;

last = newNode;

newNode = new NodeType;

newNode->num = 20;

newNode->next = NULL;

last->next = newNode;

last = newNode;

newNode = new NodeType;

newNode->num = 30;

newNode->next = NULL;

last->next = newNode;

last = newNode;

// To insert it in begining of the list

newNode = new NodeType;

newNode->num = 1;

newNode->next = NULL;

newNode->next = first;

first = newNode;

// To print all the values

newNode = first;

while (newNode != NULL) {

cout << newNode->num << " ";

newNode = newNode->next;

}

cout << endl;

return 0;

}

OUTPUT:

OUTPUT:137 input Ques 1: 45 Ques 2 : Ques 3: A E W Ques 4,5,6 :1 10 20 30 . . Program finished with exit code 0 Press ENTER to exit

Add a comment
Know the answer?
Add Answer to:
C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • How would you get this lab to work with the numbers 37 14 68 47, the...

    How would you get this lab to work with the numbers 37 14 68 47, the book we are using is Data structures using C++ by D.S. Malik. Please I need help? Just keeps repeating the same question. Code: #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType...

  • 1) Declare and initialize a pointer px to an integer variable x with the initialized value...

    1) Declare and initialize a pointer px to an integer variable x with the initialized value 3. Consider two cases of the value of x allocate on (a) the stack memory (b) the heap memory 2) Write a comment on each instruction down below and provide a memory diagram for heap and stack to illustrate these statements. int* iptr = new int[5]; // int* iptr_1 = new int(5); //

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • Please code in c 1. Write a program which does the following: Declare and initialize three...

    Please code in c 1. Write a program which does the following: Declare and initialize three pointers. One points to an integer, one points to a float and one points to a character variable Ask the user to enter appropriate values for these variables. Output the values to the screen by accessing the variables directly and then by using pointer references. Print the address of each variable. Modify the variables by performing any arithmetic operation only using pointer refer- ences...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT