GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — C++ Linked List
C++ Linked ListThis post was written to demonstrate how to implement linked list in C++. So, without further ado, let us begin with a brief overview. A linked list is a linear data structure in which each item is referred to as a node, and each node has two sections, one containing data and the other containing the address of the next node. To learn more in depth, see the "Linked List in Data Structure" post. All of the C++ codes and programs in this post are for the singly linked list. But first, let's draw a picture of a linked list (a singly linked list). As an example of a linked list in C++, consider the following figure.
The words "Linked," "Lists," "Are," "Data," and "Structures" are the first part of the linked list nodes, which is also called the part or section that contains the data, and the other part (the second part) is the address part, which contains the address of the next node. C++ Linked List Example ProgramsIn this post, I'll write and include the C++ programs below as examples of how to implement the linked list in C++. These programs assist you in practically grasping the concept of linked list in C++.
Insertion at the beginning of a listIn this program,
Following is the program that illustrates the insertion at the beginning of a list in C++. #include <iostream>
using namespace std;
struct node
{
int info;
node *next;
} *start, *newptr, *save, *ptr;
node *create_new_node(int);
void insert_at_beg(node *);
void display(node *);
int main()
{
start = NULL;
int inf;
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
cout << "Enter information for the new node: ";
cin >> inf;
cout << "\nCreating a new node...!";
newptr = create_new_node(inf);
if (newptr != NULL)
{
cout << "\n\nA new node was successfully created!\n";
}
else
{
cout << "\nWe're sorry...!..we're unable to create a new node...!..we're aborting!";
return 0;
}
cout << "\nInserting this node at the top of the list now. \n";
insert_at_beg(newptr);
cout << "\nThe node was successfully inserted at the top of the list.\n";
cout << "Now the list is:\n";
display(start);
cout << "\nWant to enter more nodes? (y/n): ";
cin >> ch;
}
return 0;
}
node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
void insert_at_beg(node *np)
{
if (start == NULL)
{
start = np;
}
else
{
save = start;
start = np;
np->next = save;
}
}
void display(node *np)
{
while (np != NULL)
{
cout << np->info << " -> ";
np = np->next;
}
cout << "!!\n";
}
Following is the initial output produced by the above program: Now type a number, say 22, and hit the ENTER key to insert the new item at the beginning of the list. The following snapshot shows the sample output after doing so. To continue feeding the information or items, type "y" and press the ENTER key, or type anything else to exit the program. Let me try some more inputs and show you a sample run using the screenshot below: Insertion at the end of a listIn this program,
Consider the following program as an example of insertion at the end of a list: #include <iostream>
using namespace std;
struct node
{
int info;
node *next;
} *start, *newptr, *save, *ptr, *rear;
node *create_new_node(int);
void insert_in_end(node *);
void display(node *);
int main()
{
start = rear = NULL;
int inf;
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
cout << "Enter information for the new node: ";
cin >> inf;
cout << "\nCreating a new node...!";
newptr = create_new_node(inf);
if (newptr != NULL)
{
cout << "\n\nA new node was successfully created!\n";
}
else
{
cout << "\nWe're sorry...!..we're unable to create a new node...!..we're aborting!";
return 0;
}
cout << "\nThis node is now being added to the end of the list. \n";
insert_in_end(newptr);
cout << "\nThe node was successfully inserted at the end of the list.\n";
cout << "Now the list is:\n";
display(start);
cout << "\nWant to enter more nodes ? (y/n): ";
cin >> ch;
}
return 0;
}
node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
void insert_in_end(node *np)
{
if (start == NULL)
{
start = rear = np;
}
else
{
rear->next = np;
rear = np;
}
}
void display(node *np)
{
while (np != NULL)
{
cout << np->info << " -> ";
np = np->next;
}
cout << "!!\n";
}
The following box shows the sample run of the above program with some user inputs of 1, 2, and 3 as three items of the linked list. Enter information for the new node: 1 Creating a new node...! A new node was successfully created! This node is now being added to the end of the list. The node was successfully inserted at the end of the list. Now the list is: 1 -> !! Want to enter more nodes ? (y/n): y Enter information for the new node: 2 Creating a new node...! A new node was successfully created! This node is now being added to the end of the list. The node was successfully inserted at the end of the list. Now the list is: 1 -> 2 -> !! Want to enter more nodes ? (y/n): y Enter information for the new node: 3 Creating a new node...! A new node was successfully created! This node is now being added to the end of the list. The node was successfully inserted at the end of the list. Now the list is: 1 -> 2 -> 3 -> !! Want to enter more nodes ? (y/n): n Process returned 0 (0x0) execution time : 12.724 s Deletion from the beginning of a listIn this program,
Consider the following program as an example program that illustrates the deletion from the beginning of a list. #include <iostream>
using namespace std;
struct node
{
int info;
node *next;
} *start, *newptr, *save, *ptr, *rear;
node *create_new_node(int);
void insert_node(node *);
void display_node(node *);
void delete_node();
int main()
{
start = rear = NULL;
int inf;
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
cout << "Enter information for the new node: ";
cin >> inf;
newptr = create_new_node(inf);
if (newptr == NULL)
{
cout << "\nWe're sorry...!..we're unable to create a new node...!..we're aborting!";
return 0;
}
insert_node(newptr);
cout << "\nWant to enter more nodes ? (y/n): ";
cin >> ch;
}
do
{
cout << "Now the list is:\n";
display_node(start);
cout << "\nWant to delete the first node? (y/n): ";
cin >> ch;
if (ch == 'y' || ch == 'Y')
delete_node();
} while (ch == 'y' || ch == 'Y');
return 0;
}
node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
void insert_node(node *np)
{
if (start == NULL)
start = rear = np;
else
{
rear->next = np;
rear = np;
}
}
void delete_node()
{
if (start == NULL)
cout << "Underflow...!!\n";
else
{
ptr = start;
start = start->next;
delete ptr;
}
}
void display_node(node *np)
{
while (np != NULL)
{
cout << np->info << " -> ";
np = np->next;
}
cout << "!!\n";
}
Let me now demonstrate a sample run of the above program with some user inputs initially to add items to the list, and then I will provide other user inputs at program runtime to delete the items from the beginning of the newly created list. Enter information for the new node: 1 Want to enter more nodes ? (y/n): y Enter information for the new node: 2 Want to enter more nodes ? (y/n): y Enter information for the new node: 3 Want to enter more nodes ? (y/n): y Enter information for the new node: 4 Want to enter more nodes ? (y/n): n Now the list is: 1 -> 2 -> 3 -> 4 -> !! Want to delete the first node? (y/n): y Now the list is: 2 -> 3 -> 4 -> !! Want to delete the first node? (y/n): y Now the list is: 3 -> 4 -> !! Want to delete the first node? (y/n): y Now the list is: 4 -> !! Want to delete the first node? (y/n): y Now the list is: !! Want to delete the first node? (y/n): y Underflow...!! Now the list is: !! Want to delete the first node? (y/n): n Process returned 0 (0x0) execution time : 23.079 s Traversal of a listIn this program,
Consider the following program as an example. #include <iostream>
using namespace std;
struct node
{
int info;
node *next;
} *start, *newptr, *save, *ptr, *rear;
node *create_new_node(int);
void insert_node(node *);
void travers(node *);
int main()
{
start = rear = NULL;
int inf;
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
cout << "Enter information for the new node: ";
cin >> inf;
newptr = create_new_node(inf);
if (newptr == NULL)
{
cout << "\nWe're sorry...!..we're unable to create a new node...!..we're aborting!";
return 0;
}
insert_node(newptr);
cout << "\nWant to enter more nodes ? (y/n): ";
cin >> ch;
cout << "\n";
}
cout << "Now the list is:\n";
travers(start);
return 0;
}
node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
void insert_node(node *np)
{
if (start == NULL)
start = rear = np;
else
{
rear->next = np;
rear = np;
}
}
void travers(node *np)
{
while (np != NULL)
{
cout << np->info << " -> ";
np = np->next;
}
cout << " !!\n";
}
Let me now show you a sample run of the preceding program to help you understand it practically. Enter information for the new node: 10 Want to enter more nodes ? (y/n): y Enter information for the new node: 20 Want to enter more nodes ? (y/n): y Enter information for the new node: 30 Want to enter more nodes ? (y/n): n Now the list is: 10 -> 20 -> 30 -> !! Process returned 0 (0x0) execution time : 11.452 s « Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |