C++ exercises solutions: Circularly Linked List C++ code


Circularly Linked List

Circularly Linked List C++ code

Step 9: Put the C++ code together

This is the complete C++ code of a circularly linked list data structure:

#include <iostream.h>

#include &tl;conio.h>

using namespace std;
typedef struct ListNode{

int data;
// the element data

struct ListNode *next;
// next link }ListElem;
//---------------------------------------------------------

void Insert (int,int);
void Delete(int);
//delete item from the Circularly Linked

List void printall(ListElem *L);
//print out all items on the screen

int countitem(ListElem *L);
//return the number of items in the Circularly Linked

List ListElem *findmin(ListElem *L);
//find the min element of the list

ListElem *findmax(ListElem *L);
//find the min element of the list

ListElem *find(ListElem *L,int);
//find the min element of the list

ListElem *pfirst;
//pfirst points to the first item of the list

ListElem *plast;
//plast points to the last item of the list

//insert item

void Insert (int val,int pos) {

int t,val_pos=1;
ListElem *item;
//new element to be inserted

item=(ListElem *)malloc(sizeof(ListElem));
//allocate space

if(!item) {cout<<"Memory problem..."< exit(100);
}
item->data=val;
//insert a new item to the empty Circularly Linked List

if(pfirst==NULL && plast==NULL){ //The first and last item point to the new item when they are null--empty Circularly Linked List.

pfirst=item;
plast=item;
cout<<"Inserted:"<data<<"\n";
}
//insert a new item at the beginning of the Circularly Linked List

else if(pos==1) { item->next=pfirst;
pfirst=item;
}
//insert a new item between items

else if(pos>1 && pos<=countitem(pfirst)){ ListElem *ta;
ta=pfirst;
for(t=1;
tt=t+1){ta=ta->next;
}
//move to the insertion point

item->next=ta->next;
ta->next=item;
}
//insert a new item at the end of the Circularly Linked List

else if(pos==countitem(pfirst)+1){

plast->next=item;
plast=item;
cout<<"Inserted: "<data<<"\n";
}
//show message if position is not valid.

else { val_pos=0;
cout<<"Invalid position! Position must be between 1 and "<<countitem(pfirst)+2<<"\n";
}
if(val_pos!=0 && plast!=NULL) plast->next=pfirst;
// let the plast points to the pfirst to make the list curcularly linked }
//Print out all items on the screen

void printall(ListElem *L) {

ListElem *i;
i=L;
if(L!=NULL){

cout<data<<"\n";
//show the first item

i=i->next;
//The element stays immediately next to the pfirts

while(i!=L){ cout<data<<"\n";
i=i->next;
}
}
else cout<<"This is no item.\n";
}
//count the number of items in the Circularly Linked List

int countitem(ListElem *L) {

ListElem *i;
int t;
i=L;
if(L!=NULL){ t=1;
i=i->next;
while(i->data!=L->data){ t=t+1;
i=i->next;
}
return t;
}
else return 0;
}
//delete item

void Delete(int pos){

int i,val_pos=1;
if(pfirst!=NULL){ //make sure the Circularly Linked List is not empty.

ListElem *temp,*del;
if(pos==1){//delete the first item

if(countitem(pfirst)==1){ //The Circularly Linked List contains only one item

pfirst=NULL;
plast=NULL;
}
else{ //The Circularly Linked List contains more than one item

temp=pfirst;
pfirst=pfirst->next;
temp=NULL;
}
}
else if(pos>1 && pos<=countitem(pfirst)){//delete middle item

temp=pfirst;
for(i=1;
ii=i+1){temp=temp->next;
}
//move to the item staying before the target item to be deleted

del=temp->next;
//target item to be deleted

temp->next=del->next;
if(del->data==plast->data)plast=temp;
//delete last item

del=NULL;
}
else {val_pos=0;
cout<<"Invalid position!\n";
}
if(val_pos!=0 && plast!=NULL) plast->next=pfirst;
//let the plast points to the pfirst to make the list curcularly linked

}
else cout<<"No item found\n";
}
ListElem *findmin(ListElem *L){

ListElem *min,*tr;
min=L;
if(L!=NULL){

tr=L->next;
while(tr!=pfirst){

if(tr->datadata) min=tr;
tr=tr->next;
}
return min;
}
else return NULL;
//empty list

}
ListElem *findmax(ListElem *L){

ListElem *max,*tr;
max=L;
if(L!=NULL){ tr=L->next;
while(tr!=pfirst){ if(tr->data>max->data) max=tr;
tr=tr->next;
}
return max;
}
else return NULL;
//empty list

}
ListElem *find(ListElem *L,int val){

ListElem *tr;
int f=0;
if(L!=NULL){

if(L->data==val) return L;
//The target item is the first item

else{ //search the next item

tr=L->next;
while(tr!=L){

if(tr->data==val) {

f=1;
break;
}
//found the tartget=>stop searching

else tr=tr->next;
//otherwise continue searching

}
}
if(f!=0) return tr;
//return found item

else return NULL;
//otherwise return NULL

}
else return NULL;
//empty list

 }
void makeEmpty(){

pfirst=plast=NULL;
}
void showmenu(){ cout<<"=================================\n";
cout<<"Circularly Linked List Operations Menu\n";
cout<<"=================================\n";
cout<<"1.Add a new item\n";
cout<<"2.Delete an item\n";
cout<<"3.Show number of items\n";
cout<<"4.Find min item\n";
cout<<"5.Find max item\n";
cout<<"6.Find item\n";
cout<<"7.Print all items\n";
cout<<"8.Exit\n";
}
void select(){

 int val, pos, ch;
ListElem *temp;
char yes='y';
makeEmpty();
//Make the list empty

while(yes=='y'){ cout<<"Enter your choice:";
cin>>ch;
switch(ch){

case 1: cout<<"Value:";
cin>>val;
cout<<"Position:";
cin>>pos;
Insert(val,pos);
break;
case 2: cout<<"Position:";
cin>>pos;
Delete(pos);
break;
case 3: cout<<"Number of items:"< break;
case 4: temp=findmin(pfirst);
if(temp!=NULL) cout<<"The min item:"<data<<endl;
else cout<<"Not found\n";
break;
case 5: temp=findmax(pfirst);
if(temp!=NULL) cout<<"The max item:"<data<<endl;
else cout<<"Not found\n";
break;
case 6: cout<<"Find what:";
cin>>val;
temp=find(pfirst,val);
if(temp!=NULL) cout<<"The found item:"<data<<endl;
else cout<<"Not found\n";
break;
case 7: cout<<"All items:\n";
printall(pfirst);
break;
case 8: exit(0);
default: cout<<"Invalid choice\n";
}
cout<<"Continue?y/n:";
cin>>yes;
}
}
int main(){

showmenu();
select();
getch();
return 0;
}




Comments

Alexander comment

 Alexander

There are so many errors in the code. It took me a while to figure out a functioning version on my own that works, provided, that the user input is correct.
Excuse me for not commenting my code.

//circulary linked list

#include "stdafx.h"
#include <iostream>
using namespace std;

struct element;
element *first_item;
element *last_item;
void add_item(int, int);
int count_items(element *);
void delete_item(int);
element *find_min();
element *find_max();
int find_item(int);
void print_list();
void show_menu();
void select();
int main()
{
do {
show_menu();
select();
} while (true);
return 0;
}
struct element{
int data;
element *next_item;
};
void add_item(int val, int pos) {
int val_pos = 1;
element *item;
item = (element*)malloc(sizeof(element));
if (!item) {
cout << "Memory problem!" << endl;
exit(100);
}
if (first_item == NULL && last_item == NULL) {
first_item = item;
last_item = item;
item->data = val;
cout << "Inserted: " << item->data << endl;
}
else if (pos == 1) {
item->next_item = first_item;
first_item = item;
item->data = val;
cout << "Inserted: " << item->data << endl;
}
else if (pos > 1 && pos <= count_items(first_item)) {
element *temp = first_item;
element *replace;
for (int i = 1; i < pos-1; i++) {
temp = temp->next_item;
}
replace = temp->next_item;
temp->next_item = item;
item->next_item = replace;
item->data = val;
cout << "Inserted: " << item->data << endl;
temp = NULL;
replace = NULL;
}
else if (pos == count_items(first_item) + 1) {
last_item->next_item = item;
last_item = item;
item->data = val;
cout << "Inserted: " << item->data << endl;
}
else {
val_pos = 0;
cout << "Invalid position! Position must be between 1 and " << count_items(first_item) + 2 << endl;
}
if (val_pos != 0 && last_item != NULL) {
last_item->next_item = first_item;
}
item = NULL;
}
int count_items(element * e) {
element *i;
int num;
i = e;
if (i != NULL) {
num = 1;
i = i->next_item;
while (i != e) {
num += 1;
i = i->next_item;
}
i = NULL;
return num;
}
else {
i = NULL;
return 0;
}
}
void delete_item(int pos) {
if (pos == 1) {
if(count_items(first_item) == 1){
first_item = NULL;
last_item = NULL;
cout << "Deleted " << pos << ". item!" << endl;
}
else {
first_item = first_item->next_item;
last_item->next_item = first_item;
}
}
else if (pos > 1 && pos < count_items(first_item)) {
element *temp = first_item;
element *del;
for (int i = 1; i < pos-1; i++) {
temp = temp->next_item;
}
del = temp->next_item;
temp->next_item = del->next_item;
temp = NULL;
del = NULL;
}
else if (pos == count_items(first_item)) {
element *temp = first_item;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next_item;
}
last_item = temp;
last_item->next_item = first_item;
temp = NULL;
}
else {
cout << "Invalid position!" << endl;
}
}
element *find_min() {
element *temp = first_item;
element *min = first_item;
if (first_item != NULL) {
while (temp->next_item != first_item) {
temp = temp->next_item;
if (temp->data < min->data) {
min = temp;
}
}
}
else {
temp = NULL;
min = NULL;
return NULL;
}
temp = NULL;
return min;
}
element *find_max() {
element *temp = first_item;
element *max = first_item;
if (first_item != NULL) {
while (temp->next_item != first_item) {
temp = temp->next_item;
if (temp->data > max->data) {
max = temp;
}
}
}
else {
temp = NULL;
max = NULL;
return NULL;
}
temp = NULL;
return max;
}
int find_item(int val) {
element *temp = first_item;
int pos = 1;
while (temp->next_item != first_item) {
if (temp->data == val) {
temp = NULL;
return pos;
}
temp = temp->next_item;
pos++;
}
temp = NULL;
return 0;
}
void print_list() {
element *temp = first_item;
if (first_item != NULL) {
for (int i = 0; i < count_items(first_item); i++) {
cout << temp->data << " ";
temp = temp->next_item;
}
cout << endl;
}
else
{
cout << "The list contains no items." << endl;
}
cout << endl;
temp = NULL;
}
void show_menu() {
cout << "======================================" << endl;
cout << "Circulary Linked List Operations Menu" << endl;
cout << "======================================" << endl;
cout << "1. Add a new item" << endl;
cout << "2. Delete an item" << endl;
cout << "3. Show number of items" << endl;
cout << "4. Find min item" << endl;
cout << "5. Find max item" << endl;
cout << "6. Find item" << endl;
cout << "7. Print all items" << endl;
cout << "8. Exit" << endl;
}
void select(){
int choice = 0;
int val, pos;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
switch (choice) {
case 1: cout << "Enter value and position: ";
cin >> val >> pos;
cout << endl;
add_item(val, pos); break;
case 2: cout << "Enter position: ";
cin >> pos;
cout << endl;
delete_item(pos); break;
case 3: cout << "Number of items: " << count_items(first_item) << endl; break;
case 4: cout << "Min value: " << find_min()->data << endl; break;
case 5: cout << "Max value: " << find_max()->data << endl; break;
case 6: cout << "Enter value to search for: " << endl;
cin >> val;
cout << endl;
pos = find_item(val);
if (pos != 0) {
cout << "Value " << val << " was found at position " << pos << "." << endl;
}
else {
cout << "The value was not found in the list." << endl;
} break;
case 7: print_list(); break;
case 8: exit(100); break;
default: break;
}
cout << endl;
}


2017-01-03
C.Q. comment

 C.Q.

Is it possible to provide a txt file with the codes in alignment? It is hard to align such a long code for studying.


2015-12-29



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.