C++ :: Array Versus Linked List Implementation For Stack

Nov 27, 2014

Basically what are the advantages and disadvantages of having an array implementation vs linked list implementation for a stack?

View 1 Replies


ADVERTISEMENT

C/C++ :: Linked List Implementation Of Stack For ReverseRows In Matrix

Nov 29, 2014

I had the following question in my exam paper and only got 2.5 out of a possible 7 marks.

Given the Matrix class:

class Matrix {
public:
Matrix(unsigned r, unsigned c);
Matrix(const Matrix<T>& rhs);
~Matrix();
const Matrix<T>&operator=(const Matrix<T>& rhs);

[code]....

Use the linkedStackType class (Linked list implementation of stack) and write a function reverseRows to reverse the order of rows in the matrix. Note that reverseRows is not a member function of the Matrix class therefore only the public interface of matrix can be used.

template<class Type>
struct nodeType {
Type info;
nodeType<Type> *link;

[code]....

View 2 Replies View Related

C++ :: Stack Implementation Of Linked Lists

Jun 7, 2014

I have this code that I need to memorize for my final. Memorizing code is easy for me, but I'm trying pretty hard to fundamentally understand the functions, and what they are doing (even using pen and paper, to draw and trace).For example, in the push function below, I understand everything, except why I'm setting ptr = p. I feel like p should be equal to NULL, then the next node I push should be equal to p, etc.

Stack & Stack::push(double x)
{
Node * p = NULL;
try
{
p = new Node;
}

[code].....

Also, are LL Queues that hard to implement once you can do them w/stacks - That will probably be something I have to code for my final, as well. Below is the full code for my Stack class.

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Stack
}

[code]....

View 1 Replies View Related

C++ :: 2D Array To Simulate Linked List For Stack Construction

Feb 13, 2013

I'm trying to implement 2d array to simulate linked list for stack and I faced several difficulties doing so.

How do I implement from Top to Bottom ordering? How do I check random index value for validity?

Source-code:

Code:
#include <iostream>
using namespace std;
int myTop, index, nex = -1;
int twoDimentionalArray[25][3], L[25];

[Code].....

And one more thing: how do I get straight 0 from 25 index in the first coulumn?

View 1 Replies View Related

C/C++ :: Doubly Linked List Versus Double Ended Queue

Jun 27, 2014

A while ago i was asked to write a program for a class that used a "Double ended queue with a current-position marker."

An example of some of the functions that i created are:

boolean atFirst() // Returns true if first element is current. Pre: !isEmpty().
boolean atLast() // Returns true if last element is current. Pre: !isEmpty().
void makeEmpty() // Sets this List to the empty state. Post: isEmpty().
void moveFirst() // Sets current marker to first element.
void movePrev() // Moves current marker one step toward first element.
void moveNext() // Moves current marker one step toward last element.
void insertBeforeFirst(int data) // Inserts new element before first element.

My question is whether a double ended queue with pointer is the same thing as a "doubly linked list" in this case. The terminology is throwing me of a little. If the two concepts are different, how is a doubly linked list different?

View 2 Replies View Related

C :: Queue Implementation - Linked List

Mar 23, 2013

I am trying to create a code to represent a queue using a linked list but i get the disturbing "Segmentation fault coredumped" after compilation.

Code:

#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>

typedef int Item;
typedef struct queue_type *Queue;
typedef struct node *return_node;

[Code] .....

View 2 Replies View Related

C++ :: Linked List - Implementation At Nth Node

Feb 27, 2015

I have to write a program which has the user be able to enter a specific value at a specific position of the linkedlist, replacing that node with the user defined value

EX: Enter the value 5 at 2nd node, which will override the old value at the 2nd node with the new one

I am getting a compiler error which terminates my program right after the user presses the return key after he/she has given a position to change the value

Error: Unhandled exception at 0x013C50C1 in Linked(1).exe: 0xC0000005: Access violation reading location 0x0000812B.

I am not going to show the whole code as the problem resides solely on the insert function:

void TheNode::insert(double num, int choice) {
int post = 0;
MyNode *ptr = new MyNode;
(*ptr).value = num;
MyNode *previous = head;
MyNode *current = head->next;

[Code] .....

View 1 Replies View Related

C++ :: Double Linked List Implementation

Dec 27, 2013

I'm trying to set up a simple implementation of a double linked list... But I seem to some mistakes.

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define ValueType int
struct vertex {

[Code] ....

I get 'Test: root exists' printed out, but not the loop through my linked list.

View 11 Replies View Related

C/C++ :: Array Implementation Of Stack To Reverse Columns In Matrix

Dec 1, 2014

I had the following question in my exam paper and only got 1.5 out of a possible 6 marks. This is the question:

Given the Matrix class:

class Matrix {
public:
Matrix(unsigned r, unsigned c);
Matrix(const Matrix<T>& rhs);
~Matrix();
const Matrix<T>&operator=(const Matrix<T>& rhs);
[Code] ....

Use the linkedStackType class (Array implementation of stack) and write a function reverseCols to reverse the order of columns in the matrix. Note that reverseCols is not a member function of the Matrix class therefore only the public interface of matrix can be used.

//Implementation of Stacks as Array
template<class Type>
class stackType: public stackADT<Type> {
public:
const stackType<Type>& operator=(const stackType<Type>&);

[Code] ....

What is the correct solution must be to reverse the columns of the matrix?

View 1 Replies View Related

C++ :: Mid Of Stack Using Linked List

Apr 1, 2013

I am unable to make any logical algorithm to access middle of stack. I am using stack implemented with linkedlists, I have following functions available for stack like push,pop,top.

View 6 Replies View Related

C++ :: Linked List Class Node Implementation

Mar 26, 2014

I am studying this sample code for linked list class node implementation. I am not 100% sure how this code keeps track of the head node. Here's what I know so far, and if you want to add/correct anything feel free to do so:

class Node {
public:
Node(); // constructor of class node without arguments
Node(int, Node*); //constructor with arguments of int type and a pointer node type
Node* next() const;
void setNext(Node*); //member fun, returning a pointer of node type
void setKey(int);

[Code] ......

View 8 Replies View Related

C :: Program To Evaluate Postfix Expression Using Array Implementation Of Stack

Apr 26, 2014

Write a program that evaluates postfix expression using array implementation of stack.

The expression [the input] is evaluated from left to right using a stack. When the element read from the expression is an operand, push it into the stack.When the element read from the expression is an operator: Pop two operands from the stack.Evaluate the two operandsPush the result of the evaluation into the stack.

The final result lies on the top of the stack at the end of the calculation. Make sure to display the result before terminating the program.Write a program that evaluates postfix expression using array implementation of stack.

Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define M 20

typedef struct{

[Code] ....

View 1 Replies View Related

C++ :: Stack Based Linked List

Oct 23, 2014

I am trying to program a stack based linked list. Something very weird is happening with my program. Whenever i want to print with cout<< in specific places, it actually changes the data of the stack. Also, using endl after the cout also changes the data of the program.

#include <iostream>
using namespace std;
class node {
public:
int number;
node* nextPtr=NULL;

[Code] ....

View 7 Replies View Related

C++ :: Linked List Interface / Implementation And Driver File

Jun 12, 2013

I am having difficulty calling the constructor in interface portion of my program. I get the error: no matching function for call to ‘Node::Node(int, NULL)’ when I try to call it on line 26 within the main function.

code:
interface: [URL]
implementation: [URL]
main file: [URL]

View 7 Replies View Related

C/C++ :: Implementing Stack Using A Double Linked List

May 12, 2014

So im trying to write a code that will take data in from a user and when that user enters specific character then i want to pop the top of the stack and place that node on a new stack. Then if the user enters a different specific character then i want to place what was just popped off the stack back on to the original stack. Anyways i'm just testing what i have without all the complicated stuff with the specific characters, but i get an error and i'm not not well versed in exception handling. So this is what i have so far. the push seems to work well but the pop seems to be giving me problems.

Stack::Stack(){
top = NULL;
bottom = NULL;
}
//method to push a line of text onto the stack
void Stack::push(string data)

[Code] .....

View 4 Replies View Related

C++ :: Overload Operator With Stack That Implements Linked List

Nov 10, 2013

struct Node {
int entry;
Node *next;
Node(); //1
Node(int item, Node *link = NULL); //2

[Code] .....

Implement: 1 2 3 4 5
and overload operator <<, >>, =

View 1 Replies View Related

C++ :: Array Based Implementation Of Linked Lists

Jul 19, 2014

I first want to say that i am trying to solve my code without Pointers.

My goal is to..
1. Construct an empty 2D array with a capacity of 25. (list[25][2];)
2. Empty(): test if the stack is empty
3. Push(): add a value to the stack in the (list[i][0] = value;) position and (list[i][1] = previous list[i][0] position)
4. Top(); read the value(list[i][0]) at the top(count) of the stack
5. Pop(); remove the value at the top of the stack (list[i]= 0;)
6. Display(); displays all the elements in the stack going from the top to bottom order. (shows array index, data value, and next array index)

I have hit a road block and don't know what to fix or where to go from here.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
char name[5];
srand(time(0));
//Protoypes
void construction(int table[25][2]);

[Code]...

View 1 Replies View Related

C++ :: Implementation File Versus Header File - Eclipse Giving Errors

Feb 10, 2013

I have written my program and it works when I keep everything in the header files, and then have my main. I am now splitting them up into implementation files, but Eclipse keeps giving me errors. It gives me error at every opening brace of the constructor and functions. It says on all of them "Redefinition of (name of constructor or method), Previously declared here." What am I doing wrong, because it works in the header file?

#include "KeyValuePair.h"
template<typename Key,typename Value>
KeyValuePair<Key,Value>::KeyValuePair()

[Code] .....

View 3 Replies View Related

C/C++ :: How To Access Linked List Functions From Stack Class Without Functions

Mar 20, 2014

I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.

We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.

As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.

NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.

Stack.h

#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;

[Code]...

View 3 Replies View Related

C :: Stack Implementation Using Arrays

May 31, 2014

Code:

#include <stdio.h>#include <unistd.h>
#include <stdlib.h>
#define STACKSIZE 100
struct stackk
{
int top;
int items[STACKSIZE];
};
typedef struct stackk *s;

[Code]...

View 1 Replies View Related

C++ :: Stack Implementation With Struct

Apr 7, 2014

I am trying to implement a stack class which has struct data type as its private member. I am getiing the following error ,

bash-3.2$ g++ stack_str_arr.cpp
stack_str_arr.cpp: In member function ‘void stack::push(int)’:
stack_str_arr.cpp:39: error: ‘top’ was not declared in this scope
stack_str_arr.cpp: At global scope:
stack_str_arr.cpp:43: error: no ‘void stack::display()’ member function declared in class ‘stack’

Code:

#include<iostream>
using namespace std;
#define MAX 5
class stack {
public :
stack();

[Code] ....

View 2 Replies View Related

C++ :: Static Versus Dynamic Array

Feb 27, 2013

there is a performance difference (e.g access time, speed, ...) between allocating static memory vs dynamic memory?

For example, if am reading data from a file, and storing them inside a huge buffer, what would be the differences between storing these data inside a static buffer vs a dynamic one?

View 3 Replies View Related

C :: Dynamic Array Of Linked List

Jan 29, 2014

I am trying to create an dynamic array (lno) This array will store addressess of different Linked list. What exactly I want is:- Take N Number of Linked List user want to create> eg. 2 now It will create 2 linked list for which I am trying to allocate memory.

Code:

struct node{
int data;
struct node *next;
}

first;
lno[0] Node 0's first address stored in ln[0] lno[1] Node 1's first address stored in ln[1] Here is the code in which I am facing problem with error Illegal structure Operation

Code:

#include<conio.h>#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

[code]....

View 1 Replies View Related

C :: Making Linked List From 2D Array

Nov 24, 2014

I have a 2D array, array[20][3]. Each row represents a storm and each column represents month #, wind speed, and pressure in that order. This data is read from a file and auto-fills the array. I need to be able to give users the ability to add an additional storm or delete a storm. I know that a linked list would be the best approach but I'm not very familiar with linked lists.

View 1 Replies View Related

C++ :: Linked List Or Sorting An Array?

Feb 2, 2015

I have an algorithm and I want to make it as efficient as possible. Basically it just involves putting numbers in order. I have two options, but which one would be more efficient:

1. Using a doubly linked list. Every time a user wants to add a new number, the algorithm will start searching the correct place for the number from the beginning of the list. This is efficient per se, but once there are about a million numbers and a number has to be put in at the end of the list, the algorithm must go through all the 999 999 numbers before it.

2. Using a container to store all the numbers first, then sorting the numbers. In this case, adding all the numbers is fast, but the actual sorting will take a long time.

Which option would be more efficient? I was thinking of using maybe merge sort or quick sort in option 2. Yes, I'm aware I could just use vector and sort, but that's not my goal here.

View 4 Replies View Related

C++ :: Sorting Array By Using Linked List

Apr 3, 2014

I have two arrays of characters that I want to combine and sort according to an internal variable (init) using a forward-iterating linked list. The two arrays must stay separated, as one of the arrays (the enemies) is contained within the object (encounter), the other is passed in via pointers (the players). The array inside the object will be destroyed later (when the encounter is over and the enemies are hopefully dead) while the one that is passed in must survive to be passed into other objects at a later time (the next encounter). My thought is to sort each array by linked list separately first, then iterate through and combine the two lists, But how to do this and no support IRL.

// DECLARATION OF CLASSES //
class character{
public:
character(); // Constructor

[Code]....

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved