CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Hello! I'm new

  1. #1
    Join Date
    Jan 2015
    Posts
    3

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Join Date
    Jan 2015
    Posts
    3

    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:ush (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:op(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;
    }

  4. #4
    Join Date
    Jan 2015
    Posts
    3

    Re: Hello! I'm new

    "Inserisci" add a node to the list, and "elimina" delete a node from it.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured