0% found this document useful (0 votes)
50 views4 pages

Dsa Lab Quiz 3

The document contains C++ code for linked list operations including push, append and print functions. It creates a linked list with sample data and prints it out.

Uploaded by

Tanzeel Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Dsa Lab Quiz 3

The document contains C++ code for linked list operations including push, append and print functions. It creates a linked list with sample data and prints it out.

Uploaded by

Tanzeel Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TANZEEL HUSSAIN

31184
DSA
LAB QUIZ 3

Code:
#include <iostream>
using namespace std;

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

void push(Node** head_ref, int new_data)


{
//1. allocate node
Node* new_node = new Node();
//2. put in the data
new_node -> data = new_data;
//3. make next of new node as head
new_node->next = (*head_ref);
//4. move head to point to the new Node
(*head_ref) = new_node;
};

void append(Node ** head_ref, int new_data) //here Head mean Header which is to point the first node
{
//1. allocate node
Node * new_node = new Node ();
//Used in step 5
Node * last = *head_ref;
//2. assign data
new_node->data = new_data;
//3. This new node gonna be the last node so make next of it as NULL
new_node->next = NULL;
//4. If the Linked List is empty,then make the new code as head
if(*head_ref == NULL)
{
*head_ref = new_node;
return;
}
else
{
//5. Else, traverse to the last node
Node *last_node = *head_ref;
//last node's next address will be NULL.
while (last_node->next != NULL)
{

last_node = last_node -> next;

//6. Change the next of last node to new node


last_node -> next = new_node;
}
}

}
void printlist(Node *node)
{
while(node != NULL)
{
cout<< " "<<node->data;
node= node->next;
}
}

int main()
{
cout << "Tanzeel Hussain" << endl;
cout << "31184\n" << endl;

Node * head = NULL;

append(&head,789);

push(&head,31184);

push(&head, 456);

push(&head, 123);

cout<<"Created Linked List is: ";


printlist(head);

return 0;
}
OUTPUT

You might also like