Home > database >  How Can I Create A Single function that can create multiple Linked Lists
How Can I Create A Single function that can create multiple Linked Lists

Time:08-09

as shown in the code , i have to use 2 similar functions for creating 2 linked lists . isn't there a way i can create as many lists as i want with just one function , i tried using struct Node **p and struct Node *p as a parameter to the function but the didn't work can someone help me to create multiple linked lists using this same function and i want to create a append function not a insert function which asks for position as well.

#include <iostream>
using namespace std;

struct  Node
{
    int data = 10 ;
    struct Node *next;
} *first , *second , *third;

void Display(struct Node *p)
{
    while (p)
    {
        cout<<p->data<<" ";
        p = p->next ;
    }
    cout<<"\n";
}
void Append_1(int elem)
{
    Node* t , *last;
    t = new Node;
    t->data = elem;
    t->next = NULL;
    if(first == 0)
        first = last = t;
    else 
    {
        last->next = t;
        last = t;
    }
}
void Append_2(int elem)
{
    Node* t , *last;
    t = new Node;
    t->data = elem;
    t->next = NULL;
    if(second == 0)
        second = last = t;
    else 
    {
        last->next = t;
        last = t;
    }
}
//void SortMerge(struct Node *p , struct Node *q);

int main()
{
    Append_1(3);
    Append_1(7);
    Display(first);

    Append_2(10);
    Append_2(14);
    Append_2(21);
    Display(second);

    //SortMerge(first , second);
    Display(third);
    
    return 0;
}

CodePudding user response:

You can create a class like here:

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

class Graph{
    public:
    Graph(int = 0);
    ~Graph();
    void display_left_right();
    void display_right_left();
    void append(int);
    void append_at_pos(int,int);
    void prepend(int);
    int get_num_elt();
    int get_data_at_pos(int);
    private:
    Node* head;
    Node* tail;
    int num_elt=0;
};

Graph::Graph(int first_data){
    head = new Node;
    head->next = NULL;
    head->previous = NULL;
    head->data = first_data;
    tail = head;
    num_elt  ;
}

Graph::~Graph(){
    Node* main_traverser = head;
    while(main_traverser){
        main_traverser = head->next;
        delete head;
        head = main_traverser;
    }
    std::cout <<"Graph deleted!" << std::endl;
}

void Graph::display_left_right(){
    Node* traverser = head;
    while(traverser != NULL){
        std::cout << traverser->data << " ";
        traverser = traverser->next;
    }
    std::cout << std::endl;
}

void Graph::display_right_left(){
    Node* traverser = tail;
    while(traverser != NULL){
        std::cout << traverser->data << " ";
        traverser = traverser->previous;
    }
    std::cout << std::endl;
}

void Graph::append(int new_data){
    Node* add = new Node;
    add->data = new_data;
    add->next = NULL;
    add->previous = tail;
    tail->next = add;
    tail = add;
    num_elt  ;
}
void Graph::append_at_pos(int pos, int new_data){
    if(pos > num_elt 1 || pos<=0){std::cout << "Wrong position!" << std::endl; return;}
    if(pos==1){
        prepend(new_data);
        return;
    }
    if(pos==num_elt 1){
        append(new_data);
        return;
    }
    Node* add = new Node;
    Node* traverser = head;
    add->data = new_data;
    for(int i=0; i<pos-2; i  ){
        traverser = traverser->next;
    }
    add->next = traverser->next;
    add->previous = traverser;
    traverser->next->previous = add;
    traverser->next = add;
}
void Graph::prepend(int new_data){
    Node* add = new Node;
    add->next = head;
    add->previous = NULL;
    add->data = new_data;
    head->previous = add;
    head = add;
    num_elt  ;
}

int Graph::get_num_elt(){
    return num_elt;
}

int Graph::get_data_at_pos(int pos){
    Node* traverser = head;
    if(pos <=0 || pos> num_elt){std::cout << "Wrong position!" << std::endl; return 0;}
    for(int i=0; i<pos-1; i  ){
        traverser = traverser->next;
    }
    return traverser->data;
}

main(){
    Graph a(2);
    a.append(3);
    a.append(4);
    a.prepend(1);
    a.display_left_right();
    a.append_at_pos(1,6);
    a.display_left_right();
    std::cout << "data at 1: " << a.get_data_at_pos(1) << std::endl;
}

CodePudding user response:

When you say "create multiple linked lists," I think you mean creating nodes to a linked list, which you have 2 append functions. I think the reason you have these 2 functions is because you do not know where to start traversing your linked list. For this reason, I think in your main function you should declare the head of the linked list, a single node that is the start. Set it's data and next to null, and then pass the head value into a function so it can start traversing from the head. Here is a generic append function that adds a node on the end, where the parameters are a reference to the head node, and the value for the new node:

void append(Node ** head, int new_data)
{
    Node * select_node = * head;
    // select node is set to the head node, and will traverse until it is at the end
    while (select_node -> next != NULL)
    {
        // select node is set to the next node until it is NULL (end of linked list)
        select_node = select_node -> next;
    }
    // now that select node is the last node, we need to make it's next value a node
    // and that node should be a new node (allocated in heap) with the value of the input value
//and the next value be NULL (because it's the end of the linked list)
    Node * next_node = new Node();
    next_node -> data = new_data;
    next_node -> next = NULL;
    select_node -> next = next_node;
}
  • Related