Last active
September 2, 2022 10:53
-
-
Save ahmubashshir/0cb95033a9a89bc703620b49561cf228 to your computer and use it in GitHub Desktop.
Singly linked list implementation in cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment