yeasir007

Tuesday, January 30, 2018

Implement Link List without STL in C++/JAVA

Linked list is a linear collection of data elements pointing to the next node by means of a pointer.
It is a data structure consisting of a group of nodes which together represent a sequence.

[Problem]
Insert the given N integers to a linked list.
Print the remainder after
1. removing the first number pointed
2. removing next->next recursively
   1 // remove the first number pointed 2 3 4 5 2 // pointer moved to 2, remove next->next 4 3 4 5 2 3 5 // pointer moved to 5, remove next->next 3 2 5 // pointer moved to 5, remove next->next 5 2 // remainder

[Input]
The first input line contains the number of test cases, T.
The second line contains the number of input, N (2 <= N <= 100).
On the next line, N data will be given.

[Output]
For each given test case, print the remainder




 INPUT         OUTPUT
============= ===================
2 //number of Test cases #1 2
5 //number of Input #2 5
1 2 3 4 5 //Input Data
6
1 2 3 4 5 6



C++ Source :

#include<iostream>
#include <stdio.h>

using namespace std;

#define MAX_NODE 100

typedef struct listNode
{
   int data;
   struct listNode* prev;
   struct listNode* next;
} ListNode;

typedef struct
{
  int use;
  ListNode node;
} ListNodeHeap;

ListNodeHeap heap[MAX_NODE];
void initHeap(void);
void initListNode(ListNode* node);
ListNode* getListNode(void);
void destroyListNode(ListNode* node);
ListNode* appendListNode(ListNode* list, int data);
ListNode* removeListNode(ListNode* list, ListNode* node);

int main(int argc, char* argv[])
{

int T, N;

setbuf(stdout, NULL);

freopen("CreateLinkList.txt", "r", stdin);
cin >> T;

for (int test_case = 1; test_case <= T; ++test_case)
{
    ListNode* list = NULL;
    ListNode* node;
    int i;

    initHeap();

    cin >> N;
    for (i = 0; i < N; i++)
    {
        int data;
        cin >> data;
        list = appendListNode(list, data);
    }

    node = list;
    while (list != list->next)
    {
        ListNode* nextNode = node->next;
        list = removeListNode(list, node);
        node = nextNode->next->next;
    }

    cout << "#" << test_case << " " << list->data << endl;
  }
  return 0;
}

void initHeap(void)
{
int i;
for (i = 0; i < MAX_NODE; i++)
{
    heap[i].use = 0;
}
}

void initListNode(ListNode* node)
{
node->data = 0;
node->prev = node;
node->next = node;
}

ListNode* getListNode(void)
{
int i;
for (i = 0; i < MAX_NODE; i++)
{
    if (!heap[i].use)
    {
        heap[i].use = 1;
        initListNode(&heap[i].node);
        return &heap[i].node;
    }
}
return NULL;
}

void destroyListNode(ListNode* node)
{
ListNodeHeap* heap_node = (ListNodeHeap*)((int*)node - 1);
heap_node->use = 0;
}

ListNode* appendListNode(ListNode* list, int data)
{
ListNode* node = getListNode();
node->data = data;
if (list == NULL)
{
    return node;
}
else
{
    ListNode* last = list->prev;
    last->next = node;
    list->prev = node;
    node->prev = last;
    node->next = list;
    return list;
}
}

ListNode* removeListNode(ListNode* list, ListNode* node)
{
if (list == list->next)
{
    destroyListNode(node);
    return NULL;
}
else
{
    ListNode* prev = node->prev;
    ListNode* next = node->next;
    prev->next = next;
    next->prev = prev;
    destroyListNode(node);
    return (list == node) ? next : list;
 }
}



Java Source:

package com.datastructure;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LinkedList {

static private final String INPUT = System.getProperty("user.dir") + "\\src\\data\\linkList.txt";
static Scanner sc;


public static void main(String[] args) {
    FileInputStream instream = null;
    try {
        instream = new FileInputStream(INPUT);
        System.setIn(instream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    sc = new Scanner(System.in);

    int T = sc.nextInt();
    for (int test_case = 1; test_case <= T; test_case++)
    {
        LinkListNode head = null;
        int N = sc.nextInt();
        for (int i = 0; i < N; i++)
        {
            int data = sc.nextInt();
            head = LinkListNode.appendListNode(head, data);
        }
        LinkListNode node = head;
        while(head != head.next)
        {
            LinkListNode nextNode = node.next;
            head = LinkListNode.removeListNode(head, node);
            node = nextNode.next.next;
        }
        System.out.printf("#%d %d\n", test_case, head.data);
    }
    sc.close();

}

}

class LinkListNode
{
int data;
LinkListNode prev;
LinkListNode next;

public LinkListNode()
{
    data = 0;
    prev = this;
    next = this;
}

public static LinkListNode removeListNode(LinkListNode head, LinkListNode node) {

    if(head == head.next)
    {
        return null;
    }
    else
    {
        LinkListNode prevNode = node.prev;
        LinkListNode nextNode = node.next;
        prevNode.next = nextNode;
        nextNode.prev = prevNode;

        return (head == node ? nextNode : head);
    }

}

public static LinkListNode appendListNode(LinkListNode head, int data)
{
    LinkListNode node = new LinkListNode();
    node.data = data;

    if(head == null)
    {
        head = node;
    }
    else
    {
        LinkListNode last = head.prev;
        last.next = node;
        head.prev = node;
        node.prev = last;
        node.next = head;
    }

    return head;
}


}

No comments:

Post a Comment

Thanks for your comments