Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Q1) WAP to implement a linked list with three nodes to accept data at those nodes
- //and then display the nodes.
- #include <iostream>
- #include<stdlib.h>
- using namespace std;
- struct node{
- int data;
- node* next;
- void setData(int d){
- data=d;
- next=NULL;
- }
- };
- int main(){
- node* node1=(node*)malloc(sizeof(node));
- node1->setData(10);
- node* node2=(node*)malloc(sizeof(node));
- node2->setData(20);
- node* node3=(node*)malloc(sizeof(node));
- node3->setData(30);
- node1->next=node2;
- node2->next=node3;
- node* temp=node1;
- int nodeNumber=1;
- while(nodeNumber<=3){
- cout<<"Node: "<<nodeNumber<<endl;
- cout<<"Address: "<<temp->next<<", Data: "<<temp->data<<endl;
- temp=temp->next;
- nodeNumber++;
- }
- cout<<endl;
- return 0;
- }
- /*
- OUTPUT:
- Node: 1
- Address: 0x5557701cf2d0, Data: 10
- Node: 2
- Address: 0x5557701cf2f0, Data: 20
- Node: 3
- Address: 0, Data: 30
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement