Tags » Linked List

C program for Simple Linked List (insertion, deletion, traversing, searching)

#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node next;
}node;
int count=0;
int insert_at_last(node *pointer, int data)
{
/
Iterate through the list till we encounter the last node. 615 more words

Data Structure

Linked List in C++


#include <iostream>
using std::cin;
using std::cout;
using std::endl;

class Node {
    /* friend funtion can access class private data */
    /* friend class' function can access the class private data */
    friend class List; /* List member functions can
                          access Node private data */
    public:
        int getData() const;
        Node(const int &data);
  private:
        int data;
        Node *next;
};

Node::Node(const int &d) :
    data (d), next(0) {
}

int Node::getData() const {
    return data;
}

class List {
    public:
        List();
        ~List();
        List& insertOrder(const int &data);
        bool remove(const int &data);
        bool isEmpty() const;
        void print() const;
    private:
        Node *head;
        Node * allocNode(const int &data);

};

List::List() :
    head (0) {
}

List::~List() {
    if (!isEmpty()) {
        Node *currentptr = head->next;
        Node *tmp;
        while (currentptr != 0) {
            tmp = currentptr;
            currentptr = currentptr->next;
            delete tmp;
        }
    }
}

Node * List::allocNode(const int &data) {
    return new Node(data);
}

List& List::insertOrder(const int &data) {
    Node *newnode = allocNode(data);
    if (isEmpty()) {
        head = newnode;
        return *this;
    }
    Node *current = head->next;
    Node *prev = head;
    while(current != 0) {
        if (current->getData() > data) {
            // insert node
            prev->next = newnode;
            newnode->next = current;
            return *this; // for cascading
        }
        // move forward
        prev = current;
        current = current->next;
    }
    // inserting at end
    prev->next = newnode;
    newnode->next = 0;
    return *this; // for cascading
}

bool List::remove(const int &d) {
    Node *current = head->next;
    Node *prev = head;
    while (current != 0) {
        if (current->getData() == d) {
            prev->next = current->next;
            delete current;
            cout << d << " removed from list" << endl;
            return true;
        }
        prev = current;
        current = current->next;
    }
    cout << "Couldnt remove " << d << " from list" << endl;
    return false;
}

bool List::isEmpty() const {
     return head == 0 ? 65 more words
Data Structures

Generic Node

So for my first post I will be showing off some custom node classes GNode and GNodeList.

Here is a quick example of a generic node class I wrote for creating your own single linked list without the use of pre-defined data structures like arrays, dictionaries etc. 439 more words

C#

Reverse linear linked list

Function to reverse a single linear linked list

struct node {
        struct node *next;
        void *data;
};
void reverse(struct node *head) {
        // if list empty or one element, nothing to reverse
        if (head->next == NULL || head->next->next == NULL)
                return;
        struct node *temp;
        struct node *current = head->next->next;
        struct node *prev = head->next;
        prev->next = NULL; // first (after head) becomes last
        while (current != NULL) {
                temp = current->next;
                current->next = prev; // reverse list
                prev = current;
                current = temp;
        }
        head->next = prev; // last becomes first
}

Common C Interview Questions

DISPLAY LINKED LIST FUNCTION

/***********DISPLAY LINKED LIST FUNCTION START*****/

void display(node *p)
{
while(p!=NULL)
{
printf(“%d”,p->data);
p=p->next;
}
}

CREATING LINKED LIST FUNCTION

/********************CREATING LINKED LIST FUNCTION START***************/

node *create(node *p,int n)
{
node *new,*temp=p;
new=(node *)malloc(sizeof(node));
new->data=n;
new->next=NULL;
if(p==NULL)
return (new);
else
{
while(temp->next!=NULL)
temp=temp->next;

temp->next=new;
return p;
}
}