-
Hello! I'm new
Hello guys. I registered on this site because i need some code to implement a STATIC linked list! I have an example on my PC but it is totally nosense and it absolutely doesn't work: despite it is a linked list, and not a queue or a stack it still has "Push" and "Pop" functions that doesn't make sense in a linked list, and so many other things. So, please, post me some examples of a list implemented with a static vector. Thanks so much
-
Re: Hello! I'm new
Sigh. Google not working for you? Alternately you could post what you have and we'll help you fix it.
-
Re: Hello! I'm new
Google is not working for me unfortunately.
Here's the code:
#ifndef LISTAS_H
#define LISTAS_H
typedef Libro E; //sostituire "Libro"
struct Nodo {
E elem;
int punt;
};
typedef Nodo L[N];
class Lista {
public:
Lista() {nelem=primo=0;}
bool empty() const {return (nelem==0);}
bool full() const {return (nelem>=N-1);}
void push(const E e);
void top(E& e) const;
void pop(E& e);
void inserisce(const E e);
void elimina(const E e);
bool inlist(const E e) const;
private:
enum{N=100}; //massimo numero di elementi della lista
L l; //l รจ un array di N elementi di tipo Nodo
int nelem; //numero di elementi presenti nella lista
int primo; //puntatore al primo elemento della lista
};
#endif
#include "ListaS.h"
#include <iostream>
using namespace std;
void Lista::push (const E e) {
if(nelem>=N-1) exit(1);
else {
nelem++;
l[nelem].elem=e; //inserisce e in posizione nelem
l[nelem].punt=primo; //collega ex con il primo
primo=nelem; //rende nuovo elemento primo nella lista
}
}
void Lista::top(E& e) const {
if(nelem>0) e=l[primo].elem;
else {
cout << "Lista vuota per richiedere top!" << endl;
system("PAUSE");
exit(1);
}
}
void Lista::pop(E& e) {
if(nelem=0) {
cout << "Lista vuota per richiedere pop!" << endl;
system("PAUSE");
exit(1);
}
else {
e=l[primo].elem; //carica in e il primo elemento
primo=l[primo].punt; //ora il puntatore al primo punta al secondo elemento
nelem--;
}
}
void Lista::inserisce(const E e) {
if(nelem>=N) exit(1);
else if(nelem==0) { //inserisce in testa
l[0].elem=e;
primo=0;
}
else { //inserisce in mezzo
l[nelem].elem=e; //inserisce e in ultima posizione
int i=primo;
while ((e>l[l[i].punt].elem) && (l[i].punt!=0))
i=l[i].punt;
l[nelem].punt=l[i].punt; //collega nuovo elemento con seguente
l[i].punt=nelem; //collega element che precede con nuovo
nelem++;
}
}
void Lista::elimina(const E e) {
int ip;
bool trovato=false;
if(nelem==0) {
cout << "Lista vuota per richiedere cancellazione!" << endl;
system("PAUSE");
exit(1);
}
else if(l[primo].elem==e) primo=l[primo].punt; //elimina in testa
else {
int i=primo;
while((l[i].punt!=0) && (!trovato)) {
trovato=(e==l[l[i].punt].elem);
ip=i;
i=l[i].punt;
}
if(trovato) {
l[ip].punt=l[l[ip].punt].punt;
nelem--;
}
}
}
bool Lista::inlist(const E e) const {
bool trovato;
if(primo==0) return false;
else {
int i=primo;
trovato=(l[primo].elem==e);
while(l[i].punt!=0 && !trovato) {
trovato=(e==l[l[i].punt].elem);
i=l[i].punt;
}
}
return trovato;
}
-
Re: Hello! I'm new
"Inserisci" add a node to the list, and "elimina" delete a node from it.
-
Re: Hello! I'm new
When posting code, please use code tags as without using them it is almost impossible to read code. Go Advanced, select the formatted code and click '#'.
What's wrong with using the STL list container? See http://www.cplusplus.com/reference/list/list/
If you must implement this yourself, see the references from this https://social.msdn.microsoft.com/Se...mark=true&ac=5