linked list

Single Linked List -Python

Posted on Updated on

My practice implementation of a single linked list in Python.

C++ Linked List Implementation

Posted on Updated on

My Interpretation of a c++ Linked list enjoy!

list.cpp

#include "list.h"

void linkedlist::iniNode(struct Node *head, int n)
{
	head->data = n;
	head->next = NULL;
}

void linkedlist::addNode(struct Node *head, int n)
{
	Node *newNode = new Node();
	newNode->data = n;
	newNode->next = NULL;
	Node *cur = head;

	while(cur)
	{
		if(cur->next == NULL)
		{
			cur->next = newNode;
			return;
		}
		cur = cur->next;
	}	
}

struct Node *linkedlist::searchNode(struct Node *head, int n)
{
	Node *cur = head;
	while(cur)
	{
		if(cur->data == n)
			return cur;

		cur = cur->next;
	}
	return NULL;
}

bool linkedlist::delNode(struct Node **head, Node *delPtr)
{
	Node *cur = *head;
	if(delPtr == *head)
	{
		*head = cur->next;
		delete delPtr;
		return true;
	}

	while(cur)
	{
		if(cur->next == delPtr)
		{
			cur->next = delPtr->next;
			delete delPtr;
			return true;
		}
		cur = cur->next;
	}
	return false;
}

void linkedlist::printList(struct Node *head)
{
	Node *cur = head->next;
	while(cur)
	{
		cout << cur->data << '\n'; cur = cur->next;
	}
}

list.h

#include <Windows.h>
#include <iostream>


using namespace::std;


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

class linkedlist
{
	public:
		void iniNode(struct Node *, int);
		void addNode(struct Node *, int);	
		bool delNode(struct Node **, Node *);
		void printList(struct Node *);
		struct Node *searchNode(struct Node *, int n);
};

loader.cpp

#include "list.h"

int main()
{
	linkedlist list =  linkedlist();

	Node *head = new Node();

	list.addNode(head, 10);
	list.addNode(head, 20);

	list.printList(head);

	list.delNode(&head, list.searchNode(head,10));
	list.printList(head);

	return 0;
}