Write a C++ program which does following: Simulate an array by a linear linked l
ID: 3667350 • Letter: W
Question
Write a C++ program which does following:
Simulate an array by a linear linked list:
Let A be an array that stores up to n (n=100 here) integers. We will use a linked list L to represent A. Your task is to write a C++ program that simulates operations on A by using L, and outputs results.
The following is an example snapshot of L and A at some given time:
int A[8]; A[1]=5; A[3]=2; A[4]=1; A[6]=7
/* node definitions for L */
struct node {
int index;
int data;
node* next;
};
typedef node *link;
AGAIN ASSUME THAT THE LIST DOES NOT CONTAIN MORE THAN 100 ELEMENTS
Do the following operations on L (the operations are defined on A; we need to perform equivalent operations on L):
HERE WE IMAGINE OPERATIONS ON A, BUT ACTUALLY DO THEM ON L, AND ALL ELEMENTS IN THE LIST ARE DISTINCT
A i x : Set A[i]=x
T : Output all data values in A (only the final assigned value, other parts are not used in A)
R: Resets A. All elements are reset meaning that there is no data in A.
I: Increment by 1 all elements of A.
Sample input/output:
A 1 5
A 3 4
A 4 1
A 6 7
A 3 2
I
T
index: 1 value: 6
index: 3 value: 3
index: 4 value: 2
index: 6 value: 8
R
A has been reset
T
No data in A now
A 1 5
T
index: 1 value: 5
Explanation / Answer
// A program to simulate a linear linked list using an array /*********************************************************************************************************************/ #include #include using namespace std; struct node { int data; int prev; int next; // Constructor: node() { data = -1; prev = -1; next = -1; } }; node A[100]; // Array of nodes used to simulate the linked list int slot = 1; // Array index of the next free space (beginning from index 1 initially) int head = -1; // Array index of the starting element in the list (-1 implies no entries yet) int tail = -1; // Array index of the tail in the list int elements = 0; // Number of elements in the list // Function prototypes: void insert(int); void insertAfter(int,int); void deleteElement(int); void reverse(); void print(); int findIndex(int); int main() { int a, b; char c; cout > c; switch(c) { case 'A': cin >> a; insert(a); break; case 'I': cin >> a >> b; insertAfter(a,b); break; case 'D': cin >> a; deleteElement(a); break; case 'R': reverse(); coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.