User
Pass
2FA
 
 

Concatenarea a douã liste dublu înlãnțuite

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Freakz Forum Index -> Trash Bin -> Trash -> Programming / Scripting / Database
Author Message1958
tenegabi

[Freakazoid]



Status: Offline
(since 09-05-2022 16:18)
Joined: 09 Oct 2010
Posts: 1794, Topics: 68
Location: Maldaeni

Reputation: 42.2
Votes: 35

           
Post Posted: 16-01-2018, 09:26:16 | Translate post to: ... (Click for more languages)

Am și eu de fãcut un proiect pentru facultate în java și nu m-am descurcat de loc cu el. Problema sunã în felul urmãtor:
Code:
24. Concatenarea a douã liste dublu înlãnțuite:
a. Circulare pe sens
b. Circulare pe ambele sensuri

Dacã e vreun cunoscãtor pe aici. Mulțumesc
Ce am lucrat pânã acum
Code:

class Node {

   Node next;
   int value;
   
   Node(int val) {
      this.value = val;
      this.next = null;
   }
   
   @Override
   public String toString() {
      Node cur = this;
      String str = "";
      
      while(cur != null) {
         str += cur.value+"->";
         cur = cur.next;
      }
      
      return str;
   }
}

class MergeLL {

   public static Node merge(Node n1, Node n2) {
      
        Node result = null;
       
        if(n1 != null && n2 != null) {
            if(n1.value < n2.value) {
               result = n1;
               result.next = merge(n1.next, n2);
            } else {
               result = n2;
               result.next = merge(n1, n2.next);
            }
        } else if(n1!=null ){
            result=n1;
        } else if(n2!=null){
            result=n2;
        }
      return result;
   }
   
   public static void main(String[] args) {
      Node n1 = new Node(1);
      Node n3 = new Node(3);
      Node n5 = new Node(5);
      Node n7 = new Node(7);
      Node n9 = new Node(9);
      
      n1.next = n3;
      n3.next = n5;
      n5.next = n7;
      n7.next = n9;
      n9.next = null;
      
      Node n2 = new Node(2);
      Node n4 = new Node(4);
      Node n6 = new Node(6);
      Node n8 = new Node(8);
      Node n10 = new Node(10);
      
      n2.next = n4;
      n4.next = n6;
      n6.next = n8;
      n8.next = n10;
      n10.next = null;
      
      System.out.println("Merge : " + merge(n1, n2));
   }
}



0 0
  
Back to top
View user's profile Send private message Yahoo! Messenger ID
sTbA

[Dev C# && C++]



Status: Offline
(since 19-08-2022 13:09)
Joined: 15 Dec 2012
Posts: 963, Topics: 71
Location: United Kingdom

Reputation: 349.6
Votes: 52

    Battletag: postrow.ID_BATTLE_NET} 
Post Posted: 13-03-2018, 13:38:03 | Translate post to: ... (Click for more languages)

Am facut ceva similar in C++, poate te va ajuta. Nu e fix ceea ce cauti tu, dar nu e departe si f. similiar cu java (exceptii fiind).
Code:
#include "stdafx.h"
#include <iostream>

/*Author = Bogdan -'sTbA' */

class Node
{
public:
   int value;
   Node* next;
   Node* prev;
   Node(int val)
   {
      std::cout << "Node constructor!" << std::endl;
      this->value = val;
      this->next = (Node*)0;
      this->prev = (Node*)0;
   }
   ~Node()
   {
      std::cout << "Node destructor" << std::endl;
      std::cout << "I had the value " << this->value << std::endl;
      if (next != NULL && prev != NULL)
         delete next;

   }
};
class List
{
public:
   Node* head;
   Node* tail;

   List()
   {
      std::cout << "List Constructor!" << std::endl;
      this->head = 0;
      this->tail = 0;
   }
   ~List()
   {
      std::cout << "List destructor!" << std::endl;
      Node* n = this->head;
      while (n)
      {
         Node* x = n->next;
         delete n;
         n = x;
      }

   }
   void insert(Node* n, Node* x)
   {
      if (n != 0)
      {
         x->next = n->next;
         n->next = x;
         x->prev = n;
         if (x->next != 0)
            x->next->prev = x;
      }
      if (this->head == 0)
      {
         this->head = x;
         this->tail = x;
         x->prev = 0;
         x->next = 0;
      }
      else if (this->tail == n)
      {
         this->tail = x;
      }
   }

   void RemoveDuplicates() {
      /*removing duplicates from the unsorted list*/
      Node* n = this->head;  //-> n holds current node
      while (n != NULL && n->next != NULL) {
         Node* ncurr = n;
         while (ncurr->next != NULL) {
            if (ncurr->next->value == n->value) {  // check equality
               ncurr->next = ncurr->next->next;
            }
            else {
               ncurr = ncurr->next;
            }
         }
         n = n->next;
      }
   }

   void Deletion(Node* n)
   {
      if (n->prev != 0)
         n->prev->next = n->next;
      else
         this->head = n->next;
      if (n->next != 0)
         n->next->prev = n->prev;
      else
         this->tail = n->prev;
   }

   void display()
   {
      Node* i = this->head;
      std::cout << "List: ";
      while (i != 0)
      {
         std::cout << i->value << ",";
         i = i->next;
      }
      std::cout << std::endl;
   }
};

int main(int argc, char *argv[])
{
   List* l = new List();
   Node* n = new Node(1);
   l->insert(0, new Node(6));
   l->insert(l->head, n);
   l->insert(l->head, new Node(8));
   l->insert(l->head, new Node(1));
   l->insert(l->head, new Node(6));
   l->insert(l->head, new Node(8));
   l->insert(l->head, new Node(1));
   l->display();
   l->RemoveDuplicates();
   // l->Deletion(n);
   l->display();
   return 0;
}


Daca tot nu o poti rezolva lasa-mi un msj si o sa termin problema.

0 0
  
Back to top
View user's profile Send private message Yahoo! Messenger ID
tenegabi

[Freakazoid]



Status: Offline
(since 09-05-2022 16:18)
Joined: 09 Oct 2010
Posts: 1794, Topics: 68
Location: Maldaeni

Reputation: 42.2
Votes: 35

           
Post Posted: 14-03-2018, 00:27:41 | Translate post to: ... (Click for more languages)

Am terminat-o până la urmă în java. Ms


0 0
  
Back to top
View user's profile Send private message Yahoo! Messenger ID
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Freakz Forum Index -> Trash Bin -> Trash -> Programming / Scripting / Database  


The time now is 28-03-2024, 21:41:40
Copyright info

Based on phpBB ro/com
B

 
 
 







I forgot my password


This message appears only once, so
like us now until it's too late ! :D
x