|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionLinked list is one of the fundamental data structures, and can be used to implement other data structures. In a linked list there are different numbers of nodes. Each node is consists of two fields. The first field holds the value or data and the second field holds the reference to the next node or null if the linked list is empty.
Figure: Linked list Pseudocode: Linkedlist Node {
data // The value or data stored in the node
next // A reference to the next node, null for last node
}
The singly-linked list is the easiest of the linked list, which has one link per node. PointerTo create linked list in C/C++ we must have a clear understanding about pointer. Now I will explain in brief what is pointer and how it works. A pointer is a variable that contains the address of a variable. The question is why we need pointer? Or why it is so powerful? The answer is they have been part of the C/C++ language and so we have to use it. Using pointer we can pass argument to the functions. Generally we pass them by value as a copy. So we cannot change them. But if we pass argument using pointer, we can modify them. To understand about pointers, we must know how computer store variable and its value. Now, I will show it here in a very simple way.
Let us imagine that a computer memory is a long array and every array location has a distinct memory location.
int a = 50 // initialize variable a
Figure: Variable value store inside an array
It is like a house which has an address and this house has only one room. So the full address is- Name of the house: a Name of the person/value who live here is: 50 House Number: 4010
If we want to change the person/value of this house, the conventional way is, type this code line a = 100 // new initialization
But using pointer we can directly go to the memory location of 'a' and change the person/value of this house without disturbing ‘a’. This is the main point about pointer.
Now the question is how we can use pointer. Type this code line:
int *b; // declare pointer b
We transfer the memory location of
b = &a; // the unary operator & gives the address of an object
Figure: Integer
pointer
Now, we can change the value of
*b = 100; // change the value of 'a' using pointer ‘b’ cout<<a; // show the output of 'a'
When you order the computer to access to access
Now the question is, if it is possible to read and change the content of
int **c; //declare a pointer to a pointer c = &b; //transfer the address of ‘b’ to ‘c’
So, we can change the value of
**c = 200; // change the value of ‘a’ using pointer to a pointer ‘c’ cout<<a; // show the output of a
Now the total code is: #include<iostream> using namespace std; int main() { int a = 50; // initialize integer variable a cout<<"The value of 'a': "<<a<<endl; // show the output of a int * b; // declare an integer pointer b b = &a; // transfer the address of 'a' to pointer 'b' *b = 100; // change the value of 'a' using pointer 'b' cout<<"The value of 'a' using *b: "<<a<<endl;// show the output of a int **c; // declare an integer pointer to pointer 'c' c = &b; // transfer the address of 'b' to pointer to pointer 'c' **c = 200; // change the value of 'a' using pointer to pointer 'c' cout<<"The value of 'a' using **c: "<<a<<endl;// show the output of a return 0; }
Output:
This program will give you the inside view of the pointer. #include<iostream> using namespace std; int main() { int a = 50; // initialize integer variable a cout<<"Value of 'a' = "<<a<<endl; // show the output of a cout<<"Memory address of 'a': "<<&a<<endl; // show the address of a cout<<endl; int * b; // declare an integer pointer b b = &a; // transfer the address of 'a' to pointer 'b' cout<<"Value of Pointer 'b': "<<*b<<endl; // show the output of *b cout<<"Content of Pointer 'b': "<<b<<endl; // show the content of *b cout<<"Memory address of Pointer 'b': "<<&b<<endl; // show the address of *b cout<<endl; int **c; // declare an integer pointer to a pointer c = &b; // transfer the address of 'b' to 'c' cout<<"Value of Pointer 'c': "<<**c<<endl; // show the output of **c cout<<"Content of Pointer 'c': "<<c<<endl; // show the content of **c cout<<"Memory address of Pointer 'c': "<<&c<<endl; // show the address of **c cout<<endl; return 0; }
Output:
We can observe that the memory address of
Linked list operationNow we have a clear view about pointer. So we are ready for creating linked list.
Linked list structure typedef struct node { int data; // will store information node *next; // the reference to the next node };
First we create a structure “node”. It has two members and
first is 1) Insert from frontAt first initialize node type. node *head = NULL; //empty linked list
Then we take the data input from the user and store in the node *temp; //create a temporary node temp = (node*)malloc(sizeof(node)); //allocate space for node
Then place
Figure: Insert at first
temp->data = info; // store data(first field) temp->next=head; // store the address of the pointer head(second field) head = temp; // transfer the address of 'temp' to 'head'
2) TraverseNow we want to see the information stored inside the linked
list. We create node
We can get the data from first node using
while( temp1!=NULL ) { cout<< temp1->data<<" ";// show the data in the linked list temp1 = temp1->next; // tranfer the address of 'temp->next' to 'temp' }
Figure: Traverse
This process will run until the linked list’s next is NULL. 3) Insert from backInsert data from back is very similar to the insert from front in the linked list. Here the extra job is to find the last node of the linked list.
node *temp1; // create a temporary node temp1=(node*)malloc(sizeof(node)); // allocate space for node temp1 = head; // transfer the address of 'head' to 'temp1' while(temp1->next!=NULL) // go to the last node temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'
Now, Create a temporary node
Figure: Insert at last
node *temp; // create a temporary node temp = (node*)malloc(sizeof(node)); // allocate space for node temp->data = info; // store data(first field) temp->next = NULL; // second field will be null(last node) temp1->next = temp; // 'temp' node will be the last node | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|