Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

linked list problem in print data

0 votes
asked Nov 15, 2017 by Danish Ali (120 points)
https://onlinegdb.com/S1xCTXHFJG

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxCODExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#include <iostream>

#include<string>

using namespace std;

class node

{

public:

  int data;

  node *next;

    node ()

  {

    data = 0;

    next = NULL;

  }

  void setdata ()

  {

    cout << "Enter value : ";

    cin >> data;

    next = NULL;

  }

};

class linkedlist

{

private:

  node * head;

  int counter =0;

public:

  void inserthead (node * p)

  {

    head = p;

  }

  node *gethead ()

  {

    return head;

  }

  void insertdata ()

  {

    node *p;

    p = new node ();

    p->setdata ();

    node *temp;

    temp = head;

    if (head == NULL)

      {

head = p;

counter++;

      }

    else

      {

for (int i = 1; i < counter; i++)

  {

    temp = temp->next;

  }

// temp->next = p;

temp = p;

counter++;

      }

  }

  void deletedata ()

  {

    if (counter == 0)

      {

cout << "\ncan't delete from an empty linked list:\n";

      }

    else

      {

    node *temp2 = head;

for (int i = 1; i < counter-1; i++)

  {

    temp2 = temp2->next;

  }

  temp2->next=NULL;

      }

  }

  void printdata ()

  {

    if (counter == 0)

      {

cout << "\ncan't print from an empty linked list:\n";

      }

    else

      {

node *temp2 = head;

for (int i = 1; i < counter; i++)

  {

    cout << "\nvalue : " << temp2->data;

    temp2 = temp2->next;

  }

      }

  }

};

int main ()

{

  linkedlist ll;

  int choice;

  do

    {

      cout << "\nPress 1 for enter data: ";

      cout << "\nPress 2 for print data: ";

      cout << "\nPress 3 for delete data: ";

      cout << "\nPress 4 for exit: ";

      cin >> choice;

      switch (choice)

{

case 1:

  {

    ll.insertdata ();

  }

  break;

case 2:

  {

    ll.printdata ();

  }

  break;

case 3:

  {

    ll.deletedata ();

  }

  break;

}

    }

  while (choice != 4);

  return 0;

}

1 Answer

0 votes
answered Nov 20, 2017 by sai yaddalapudi (300 points)
#include <stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
int printlist(struct node *n)
{
    printf("Wait......till Data Prints..........\n");
    while(n!=NULL)
    {
        printf("%d\n",n->data);
        n=n->next;
    }
}
int main()
{
    int a,b,c;
    printf("Enter data for lined list\t");
    scanf("%d %d %d",&a,&b,&c);
    
    struct node* head=NULL;
    struct node* second=NULL;
    struct node* third=NULL;
    head=(struct node*)malloc(sizeof(struct node));
    second=(struct node*)malloc(sizeof(struct node));
    third=(struct node*)malloc(sizeof(struct node));
    head->data=a;
    head->next=second;
    second->data=b;
    second->next=third;
    third->data=c;
    third->next=NULL;
    //printf("data=%u",second->next);
    printlist(head);
    
}
commented Jun 21 by Wesley Míčô (100 points)
wow every thing is clean
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...