You are on page 1of 2

LINK LIST head = head->next;

#include <iostream>
delete n;
using namespace std;
return ret;
class LinkedList{
}
struct Node {
private:
int x;
Node *head; // this is the private member variable. It
Node *next; is just a pointer to the first Node

}; };

public: int main() {

LinkedList(){ LinkedList list;

head = NULL; list.addValue(5);

} list.addValue(10);

~LinkedList(){ list.addValue(20);

Node *next = head;

while(next) { cout << list.popValue() << endl;

Node *deleteMe = next; cout << list.popValue() << endl;

next = next->next; // save pointer to the next cout << list.popValue() << endl;
element
// because there is no error checking in popValue(),
delete deleteMe; // delete the current entry the following

}} // is undefined behavior. Probably the program will


crash, because
// This prepends a new value at the beginning of the
list // there are no more values in the list.

void addValue(int val){ // cout << list.popValue() << endl;

Node *n = new Node(); // create new Node return 0;

n->x = val; // set value }

n->next = head; // make the node point to the


next node.
#include <stdio.h>
// If the list is empty, this is NULL, so
#include <stdlib.h>
the end of the list --> OK
struct node {
head = n; }
int data;
int popValue(){
struct node *next;
Node *n = head;
};
int ret = n->x;
struct node *head = NULL;

struct node *current = NULL; #include<span class="code-


keyword"><iostream></span>
void printList() {
using namespace std;

int main()
struct node *ptr = head;
{

int a = 50;
printf("\n[head] =>");
cout<<"Value of 'a' = "<<a<<endl;
while(ptr != NULL) {
cout<<"Memory address of 'a': "<<&a<<endl;
printf(" %d =>",ptr->data);
cout<<endl;
ptr = ptr->next;

}
int * b;
printf(" [null]\n");
b = &a;
}
cout<<"Value of Pointer 'b': "<<*b<<endl;
void insert(int data) {
cout<<"Content of Pointer 'b': "<<b<<endl;
//create a link
cout<<"Memory address of Pointer 'b':
struct node *link = (struct node*) malloc(sizeof(struct
"<<&b<<endl; // show the address of *b
node));
cout<<endl;
//link->key = key;
int **c
link->data = data;
c = &b;
link->next = head;
cout<<"Value of Pointer 'c': "<<**c<<endl;
head = link;}
cout<<"Content of Pointer 'c': "<<c<<endl; // show
int main() {
the content of **c
insert(10); insert(20); insert(30);
cout<<"Memory address of Pointer 'c': "<<&c<<endl;
insert(1); insert(40); insert(56);
cout<<endl;

return 0;
printList();
}
return 0;

You might also like