Skip to content

Instantly share code, notes, and snippets.

@ahmubashshir
Last active September 2, 2022 10:53
Show Gist options
  • Save ahmubashshir/0cb95033a9a89bc703620b49561cf228 to your computer and use it in GitHub Desktop.
Save ahmubashshir/0cb95033a9a89bc703620b49561cf228 to your computer and use it in GitHub Desktop.

Revisions

  1. ahmubashshir revised this gist Sep 2, 2022. No changes.
  2. ahmubashshir created this gist Sep 2, 2022.
    20 changes: 20 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    debug ?= false

    OUT := list

    SRCS = list-impl.cpp list.cpp
    LDFLAGS += -lstdc++

    ifneq ($(filter yes true, $(debug)),)
    LDFLAGS += -g
    CXXFLAGS += -g
    endif

    all: $(OUT)

    $(OUT): $(SRCS:.cpp=.o)

    clean:
    rm -rf $(SRCS:%.cpp=%.o) $(OUT)

    .PHONY: all clean
    68 changes: 68 additions & 0 deletions list-impl.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #include <iostream>
    #include "list.h"

    #define until(expr) while(!(expr))

    template <class T>
    void List<T>::print()
    {
    Node<T> *head = this->node;
    int idx = 0;
    until(head == NULL)
    {
    std::cout << "list[" << idx-- << "] = " << head->value << std::endl;
    head = head->next;
    }
    }

    template <class T>
    Node<T>* List<T>::search(T value)
    {
    Node<T> *head = this->node;

    until(head->next == NULL || head->value == value) head = head->next;

    return head;
    }

    template <class T>
    bool List<T>::push(T value)
    {
    try
    {
    Node<T> *node = new Node<T>(value);
    if (this->node != NULL) node->next = this->node;
    this->node = node;
    }
    catch(const std::exception &e)
    {
    std::cout << e.what() << std::endl;
    throw;
    }
    this->length_ += 1;
    return true;
    }

    template <class T>
    T List<T>::pop()
    {
    if(this->node == NULL) return T(NULL);

    T val = this->node->value;
    Node<T> *del = this->node;

    this->node = this->node->next;
    delete del;

    this->length_ -= 1;

    return val;
    }

    template <class T>
    size_t List<T>::length()
    {
    return this->length_;
    }

    template class List<int>;
    17 changes: 17 additions & 0 deletions list.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #include <iostream>
    #include "list.h"

    int main()
    {
    List<int> list;
    int data;

    while (std::cin >> data)
    {
    list.push(data);
    }
    while(list.length() > 0)
    {
    std::cout << list.pop() << std::endl;
    }
    }
    33 changes: 33 additions & 0 deletions list.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #ifndef __LIST_H__
    #define __LIST_H__

    template<class T>
    class Node
    {
    public:
    T value;
    Node<T> *next;
    explicit Node(T val)
    {
    this->value = val;
    this->next = NULL;
    }
    };

    template <class T>
    class List
    {
    private:
    Node<T> *node = NULL;
    size_t length_ = 0;

    public:
    void print();
    Node<T>* search(T value);
    bool push(T value);
    T pop();
    T peak();
    size_t length();
    };

    #endif