Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. plea

ID: 3665145 • Letter: G

Question

Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help.

list.h:

//////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
//////////////////////////////////////////////////////////////////////////

namespace CS170
{
struct node
{
int value;
node *next;
};

class list
{
public:
// Constructor for list. Creates an empty list
list();

/* Destructor for list.
Empty the list and release the allocated memory
*/
~list();

// Prints out the values contained in the list
void print_list() const;

// Returns the current size of the list
unsigned size() const;

// Returns true if list is empty, false otherwise
bool empty() const;
  
// Frees (deletes) all of the nodes in the list
void clear();

/* Creates a node with val and
add it to the front of the list */
void push_front(int val);

// Return the first node in the list
node *front();

/* Removes nodes at position pos.
Position count starts from zero.
*/
void erase(int pos);

/* Removes nodes from position first to position last-1.
Position count starts from zero.
*/
void erase(int first, int last);

/* Resizes the list to contain n elements.
If n is smaller than the current size, then keep only
the first n elements, then destroy those beyond.
If n is larger than the current size, the new elements
are initialized as val.
*/
void resize(int n, int val = 0);

// Sorts the list ascendingly
void sort();
  
/* Assume the current list and l2 are both sorted ascendingly,
this function merges them into one, so that the elements
are still in ascending order.
The current list will store the merged elements,
while l2 will become empty.
*/
void merge(list &l2);

private:
unsigned list_size;
node *the_list;

node *make_node(int val);
};
}
#endif // LIST_H

main.cpp:

#include <fstream>
#include <iostream>
#include "list.h"

CS170::list& read_list_push_front(const char *filename,CS170::list &l)
{
int value;
std::ifstream fin(filename);
if (!fin.is_open())
std::cout <<filename<< " is not found!" << std::endl;
else
while (!fin.eof())
{
fin >> value;
l.push_front(value);
}

return l;
}

int main(int argc, char *argv[])
{
if(argc<2)
{   
std::cerr<<"Usage: "<<argv[0]<<" <filename> ";
return 1;
}
CS170::list l1, l2;

l1 = read_list_push_front(argv[1],l1);
std::cout << "l1: ";
l1.print_list();
  
std::cout << " Erase l1 at 100: ";
l1.erase(100);
l1.print_list();
  
std::cout << " Erase l1 at 200: ";
l1.erase(200);
l1.print_list();

std::cout << " Erase l1 from 100 to before 200: ";
l1.erase(100, 200);
l1.print_list();

std::cout << " Resize l1 to 500 elements, fill with 100: ";
l1.resize(500, 100);
l1.print_list();

std::cout << " Resize l1 to 200 elements: ";
l1.resize(200);
l1.print_list();
  
std::cout << " Sort l1: ";
l1.sort();
l1.print_list();
  
std::cout << " l2: ";
l2 = read_list_push_front(argv[1],l2);
l2.print_list();
  
std::cout << " Sort l2: ";
l2.sort();
l2.print_list();
  
l1.merge(l2);
std::cout << " l1 after merging: ";
l1.print_list();
std::cout << " l2 after merging: ";
l2.print_list();
  
l1.clear();
l2.clear();
  
return 0;
}

Explanation / Answer


Example 1: The head of the list is not a global pointer

#include <iostream>

using namespace std;

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

// only for the 1st Node
void initNode(struct Node *head,int n){
   head->data = n;
   head->next =NULL;
}

// apending
void addNode(struct Node *head, int n) {
   Node *newNode = new Node;
   newNode->data = n;
   newNode->next = NULL;

   Node *cur = head;
   while(cur) {
       if(cur->next == NULL) {
           cur->next = newNode;
           return;
       }
       cur = cur->next;
   }
}

void insertFront(struct Node **head, int n) {
   Node *newNode = new Node;
   newNode->data = n;
   newNode->next = *head;
   *head = newNode;
}

struct Node *searchNode(struct Node *head, int n) {
   Node *cur = head;
   while(cur) {
       if(cur->data == n) return cur;
       cur = cur->next;
   }
   cout << "No Node " << n << " in list. ";
}

bool deleteNode(struct Node **head, Node *ptrDel) {
   Node *cur = *head;
   if(ptrDel == *head) {
       *head = cur->next;
       delete ptrDel;
       return true;
   }
  
   while(cur) {
       if(cur->next == ptrDel) {
           cur->next = ptrDel->next;
           delete ptrDel;
           return true;
       }
       cur = cur->next;
   }
   return false;
}

/* reverse the list */
struct Node* reverse(struct Node** head)
{
   Node *parent = *head;
   Node *me = parent->next;
   Node *child = me->next;

   /* make parent as tail */
   parent->next = NULL;
   while(child) {
       me->next = parent;
       parent = me;
       me = child;
       child = child->next;
   }
   me->next = parent;
   *head = me;
   return *head;
}

/* Creating a copy of a linked list */
void copyLinkedList(struct Node *node, struct Node **pNew)
{
   if(node != NULL) {
       *pNew = new Node;
       (*pNew)->data = node->data;
       (*pNew)->next = NULL;
       copyLinkedList(node->next, &((*pNew)->next));
   }
}

/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node *node1, struct Node *node2)
{
   static int flag;

   /* both lists are NULL */
   if(node1 == NULL && node2 == NULL) {
       flag = 1;
   }
   else {
       if(node1 == NULL || node2 == NULL)
           flag = 0;
       else if(node1->data != node2->data)
           flag = 0;
       else
           compareLinkedList(node1->next, node2->next);
   }

   return flag;
}

void deleteLinkedList(struct Node **node)
{
   struct Node *tmpNode;
   while(*node) {
       tmpNode = *node;
       *node = tmpNode->next;
       delete tmpNode;
   }
}

void display(struct Node *head) {
   Node *list = head;
   while(list) {
       cout << list->data << " ";
       list = list->next;
   }
   cout << endl;
   cout << endl;
}

int main()
{
   struct Node *newHead;
   struct Node *head = new Node;
  
   initNode(head,10);
   display(head);

   addNode(head,20);
   display(head);

   addNode(head,30);
   display(head);

   addNode(head,35);
   display(head);

   addNode(head,40);
   display(head);

   insertFront(&head,5);
   display(head);

   int numDel = 5;
   Node *ptrDelete = searchNode(head,numDel);
   if(deleteNode(&head,ptrDelete))
       cout << "Node "<< numDel << " deleted! ";
   display(head);

   cout << "The list is reversed ";
   reverse(&head);
   display(head);

   cout << "The list is copied ";
   copyLinkedList(head,&newHead);
   display(newHead);

   cout << "Comparing the two lists... ";
   cout << "Are the two lists same? ";
   if(compareLinkedList(head,newHead))
       cout << "Yes, they are same! ";
   else
       cout << "No, they are different! ";
   cout << endl;

   numDel = 35;
   ptrDelete = searchNode(newHead,numDel);
   if(deleteNode(&newHead,ptrDelete)) {
       cout << "Node "<< numDel << " deleted! ";
       cout << "The new list after the delete is ";
       display(newHead);
   }
   cout << "Comparing the two lists again... ";
   cout << "Are the two lists same? ";
   if(compareLinkedList(head,newHead))
       cout << "Yes, they are same! ";
   else
       cout << "No, they are different! ";
  
   cout << endl;
   cout << "Deleting the copied list ";
   deleteLinkedList(&newHead);
   display(newHead);
   return 0;
}

Output:

10

10 20

10 20 30

10 20 30 35

10 20 30 35 40

5 10 20 30 35 40

Node 5 deleted!
10 20 30 35 40

The list is reversed
40 35 30 20 10

The list is copied
40 35 30 20 10

Comparing the two lists...
Are the two lists same?
Yes, they are same!

Node 35 deleted!
The new list after the delete is
40 30 20 10

Comparing the two lists again...
Are the two lists same?
No, they are different!

Deleting the copied list

Example :The head of the list is a global pointer

#include <iostream>

using namespace std;

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

node *head = NULL;

// returning the pointer to the element
// whose data is less than or equal to input data
struct node *searchNode(int n) {
   if(head == NULL) return head;
   node *cur = head;
   node *prev = head;
   while(cur) {
       if(cur->data == n) return cur;
       if(cur->data > n) return prev;
       prev = cur;
       cur = cur->next;
   }
}

// returning the pointer to the element
// whose data is equal to input data
struct node *searchNode2(int n) {
   if(head == NULL) return head;
   node *cur = head;
   node *prev = head;
   while(cur) {
       if(cur->data == n) return cur;
       prev = cur;
       cur = cur->next;
   }
   return cur;
}

void addNode(int n) {
   node *newNode = new node;
   newNode->data = n;
   newNode->next = NULL;

   if(head == NULL) {
       head = newNode;
       return;
   }
   node *cur = head;
   while(cur) {
       if(cur->next == NULL) {
           cur->next = newNode;
           return;
       }
       cur = cur->next;
   }
}

void insertNode(int n) {
   node *ptr = searchNode(n);
   node *newNode = new node;
   newNode->data = n;
   node *cur = head;
   while(cur) {
       if(cur == ptr ) {
           newNode->next = cur->next;
           cur->next = newNode;
           return;
       }
       cur = cur->next;
   }
}

void deleteNode(int n) {
   node *ptr = searchNode(n);
   if(ptr == NULL)
       cout << "No node with data = " << n << endl;
   if(ptr == head) {
       head = ptr->next;
       return;
   }
   node *cur = head;
   node *prev = head;

   while(cur) {
       if(cur == ptr) {
           prev->next = cur->next;
           return;
       }
       prev = cur;
       cur = cur->next;
   }
}

void display() {
   struct node *list = head;
   while(list) {
       cout << list->data <<" ";
       list = list->next;
   }
   cout << endl;
}

int main()
{
   addNode(10);
display();
   addNode(20);
   display();
   addNode(40);
   display();
   addNode(50);
   display();
   insertNode(30);
   display();
   deleteNode(40);
   display();
   return 0;
}

Example 3:
// linked list example - using struct
#include <iostream>
#include <cstring>

using namespace std;

struct node * initNode( char *, int );
void displayNode( struct node * );
void displayList( struct node * );
void addNode( struct node * );
struct node * searchName( struct node *, char * );
void deleteNode( struct node * );
void insertNode( struct node * );
void deleteList( struct node * );

struct node {
char name[20];
int id;
struct node *next;
};

struct node *head = (struct node *) NULL;
struct node *tail = (struct node *) NULL;

// allocates memory for the node
// assign values to the member of the structure
struct node * initNode( char *name, int id )
{
struct node *ptr = new node;

// error? then just return
if( ptr == NULL )
return static_cast<struct node *>(NULL);   
    // assign it
   // then return pointer to ne node
else {   
strcpy( ptr->name, name );   
ptr->id = id;
return ptr;   
}
}

// adding to the end of list
void addNode( struct node *newnode )   
{
   // if there is no node, put it to head
   if( head == NULL ) {   
head = newnode;
tail = newnode;
   }

   // link in the new_node to the tail of the list
   // then mark the next field as the end of the list
   // adjust tail to point to the last node

   tail->next = newnode;   
   newnode->next = NULL;
   tail = newnode;
}
  
void insertNode( struct node *newnode )
{
struct node *temp, *prev;   

if( head == NULL ) { // if an empty list,
head = newnode; // set 'head' to it
tail = newnode;
head->next = NULL; // set end of list to NULL
return;   
}

temp = head; // start at beginning of list
                   // while currentname < newname
while( strcmp( temp->name, newnode->name) < 0 ) {
                   // to be inserted then
temp = temp->next; // goto the next node in list
if( temp == NULL ) // don't go past end of list
break;
}
                   // set previous node before we insert
                   // first check to see if it's inserting   
if( temp == head ) {           // before the first node
newnode->next = head; // link next field to original list
head = newnode; // head adjusted to new node
}
else {               // it's not the first node
prev = head;           // start of the list,
while( prev->next != temp ) {   // will cycle to node before temp
prev = prev->next;
}
prev->next = newnode;       // insert node between prev and next
newnode->next = temp;
if( tail == prev )       // if the new node is inserted at the
tail = newnode;       // end of the list the adjust 'end'
}
}

struct node * searchName( struct node *ptr, char *name )
{
while( strcmp( name, ptr->name ) != 0 ) {   
ptr = ptr->next;
if( ptr == NULL )   
break;
}
return ptr;
}

struct node* searchId(struct node* ptr, int id) {
while( id != ptr->id ) {
ptr = ptr->next;
if( ptr == NULL )
break;
}
return ptr;   
}

void reverse() {
   // we need at least two nodes for the reverse to have any effect
   if(head == NULL || head->next == NULL) return;

   // Starting 2nd list as 'me' and 'head' is now 'me->next'
   // and 'head->next' is pointing to NULL
   // So, the 3rd list is now 'child' of 'me'
   node *parent = head;
   node *me = head->next;
   node *child = me->next;

   // convert head to tail
   parent->next = NULL;
  
   while(child != NULL){
       me->next = parent;
       parent = me;
       me = child;
       child = child->next;
   }
   // when me reached the tail
   // me becomes head
   head = me;
   // the head is now pointing to the 2nd last node
   head->next = parent;
}

void deleteNode( struct node *ptr )
{
struct node *temp, *prev;
temp = ptr; // node to be deleted
prev = head; // start of the list, will cycle to node before temp

if( temp == prev ) { // deleting first node?
head = head->next; // moves head to next node   
if( tail == temp ) // is it end, only one node?   
tail = tail->next; // adjust end as well
delete temp ; // free up space
}
else { // if not the first node, then
while( prev->next != temp ) { // move prev to the node before
prev = prev->next; // the one to be deleted   
}
prev->next = temp->next; // link previous node to next
if( tail == temp ) // if this was the end node,   
tail = prev; // then reset the end pointer
delete temp; // free up space
}
}

void deleteList( struct node *ptr )
{
struct node *temp;

if( head == NULL ) return;    // don't try to delete an empty list

if( ptr == head ) {           // if we are deleting the entire list
head = NULL;           // then reset head and
tail = NULL;           // end to empty   
}
else {
temp = head;           // if it's not the entire list, readjust end   
while( temp->next != ptr ) // locate previous node to ptr   
temp = temp->next;
tail = temp; // set end to node before ptr   
}

while( ptr != NULL ) {       // while there are still nodes to delete
temp = ptr->next;           // record address of next node   
delete ptr;           // free this node   
ptr = temp;           // point to next node to be deleted   
}
}


void displayNode( struct node *ptr ) {
   cout << ptr->id << ": " << ptr->name << endl;
}


void displayList( struct node *ptr ) {
while( ptr != NULL ) {
displayNode( ptr );   
ptr = ptr->next;
}
}

#include <iostream>
using namespace std;

int main()
{
    char* name;
    int id, ch = 1;
    struct node *ptr;

   // add
   ptr = initNode( "s1", 1 );
   addNode(ptr);
   ptr = initNode( "s2", 2 );
   addNode(ptr);
   ptr = initNode( "s3", 3 );
   addNode(ptr);
   ptr = initNode( "s4", 4 );
   addNode(ptr);
   ptr = initNode( "s5", 5 );
   addNode(ptr);
   displayList(head);

   // delete
   name = "s2";
   ptr = searchName(head, name );
   if( ptr == NULL ) {
       cout << " Name: " << name << " not found" << endl;
   }
   else {
       cout << " Deleting a node ... ";
   displayNode(ptr);
       deleteNode( ptr );
   }
   displayList(head);

   // insert
   name = "s2";
   id = 2;
   ptr = initNode( name, id );
   insertNode( ptr );
   cout << " Inserting a node ... ";
   displayNode(ptr);
   displayList(head);

   // reverse
   cout << " Reversing the list ... ";
   reverse();
   displayList(head);

   // delete
   cout << " In the end, deleting the list ... ";
   deleteList(head);
   displayList(head);
   return 0;
}

Example :
Used class & structure in that class
#include <iostream>
#include <string>
using namespace std;

class list
{
public:
   struct node {
       int id;
       string name;
       struct node *next;
   } *head, *tail, *ptr;  

   list():head(NULL),tail(NULL){}   // constructor  
   ~list();           // destructor

   struct list::node* searchName(struct list::node*, string);  
   struct list::node* searchId(struct list::node*, int);
   struct list::node* initNode(string, int);

   void reverse();
   void addNode( struct list::node*);
   void insertNode( struct list::node*);
   void deleteNode( struct list::node*);
   void deleteList( struct list::node*);
   void displayList( struct list::node*)const ;
    void displayNode( struct list::node*) const;  
};

list::~list() {
   node *current,*temp;
   current = head;
   temp = head;
   while(current != NULL) {
       current = current->next;
       delete temp;
       temp = current;
   }
}

struct list::node* list::initNode(string s, int i) {
   struct node *ptr = new node;

   // error? then just return
   if( ptr == NULL )   
       return static_cast<struct node *>(NULL);
   // assign it
   // then return pointer to ne node
   else {
       ptr->name = s ;
       ptr->id = i;
       return ptr;   
   }
}

// adding to the end of list
void list::addNode( struct node *newNode ) {
   // if there is no node, put it to head
   if( head == NULL ) {
       head = newNode;
       tail = newNode;
   }

   // link in the new_node to the tail of the list
   // then mark the next field as the end of the list
   // adjust tail to point to the last node

   tail->next = newNode;   
   newNode->next = NULL;   
   tail = newNode;   
}

void list::insertNode( struct node *newnode ) {
struct node *temp, *prev;

if( head == NULL ) { // if an empty list,   
head = newnode; // set 'head' to it   
tail = newnode;
head->next = NULL; // set end of list to NULL   
return;   
}

temp = head; // start at beginning of list
                   // while currentname < newname
while( temp->name < newnode->name) {   // to be inserted then
temp = temp->next; // goto the next node in list
if( temp == NULL ) // don't go past end of list
break;
}                             
                   // set previous node before we insert
                   // first check to see if it's inserting   
if( temp == head ) {           // before the first node
newnode->next = head; // link next field to original list
head = newnode; // head adjusted to new node
}
else {               // it's not the first node
prev = head;           // start of the list,
while( prev->next != temp ) {  
prev = prev->next;       // will cycle to node before temp
}
prev->next = newnode; // insert node between prev and next   
newnode->next = temp;
if( tail == prev )       // if the new node is inserted at the
tail = newnode;       // end of the list the adjust 'end'
}
}

struct list::node* list::searchName(struct node* ptr, string name) {
while( name != ptr->name ) {
ptr = ptr->next;
if( ptr == NULL )
break;
}
return ptr;   
}

struct list::node* list::searchId(struct node* ptr, int id) {
while( id != ptr->id ) {
ptr = ptr->next;
if( ptr == NULL )
break;
}
return ptr;   
}

void list::reverse() {
   // we need at least two nodes for the reverse to have any effect
   if(head == NULL || head->next == NULL) return;

   // Starting 2nd list as 'me' and 'head' is now 'me->next'
   // and 'head->next' is pointing to NULL
   // So, the 3rd list is now 'child' of 'me'
   node *parent = head;
   node *me = head->next;
   node *child = me->next;

   // convert head to tail
   head->next = NULL;

   // reverse pointer direction
   me->next = head;
  
   while(child != NULL){
       me->next = parent;
       parent = me;
       me = child;
       child = child->next;
   }
   // when me reached the tail
   // me becomes head
   head = me;
   // the head is now pointing to the 2nd last node
   head->next = parent;
}

void list::deleteNode( struct list::node *ptr )
{
struct node *temp, *prev;
temp = ptr; // node to be deleted
prev = head; // start of the list, will cycle to node before temp

if( temp == prev ) { // deleting first node?
head = head->next; // moves head to next node   
if( tail == temp ) // is it end, only one node?   
tail = tail->next; // adjust end as well
delete temp ; // free up space
}
else { // if not the first node, then
while( prev->next != temp ) { // move prev to the node before
prev = prev->next; // the one to be deleted   
}
prev->next = temp->next; // link previous node to next
if( tail == temp ) // if this was the end node,   
tail = prev; // then reset the end pointer
delete temp; // free up space
}
}

void list::deleteList( struct node *ptr )
{
struct node *temp;

if( head == NULL ) return;    // don't try to delete an empty list

if( ptr == head ) {           // if we are deleting the entire list
head = NULL;           // then reset head and
tail = NULL;           // end to empty   
}
else {
temp = head;           // if it's not the entire list, readjust end   
while( temp->next != ptr ) // locate previous node to ptr   
temp = temp->next;
tail = temp; // set end to node before ptr   
}

while( ptr != NULL ) {       // whilst there are still nodes to delete
temp = ptr->next;           // record address of next node   
delete ptr;           // free this node   
ptr = temp;           // point to next node to be deleted   
}
}

void list::displayNode( struct list::node *ptr ) const
{
   cout << ptr->id << ": " << ptr->name << endl;
}

void list::displayList( struct list::node *ptr ) const
{
   if(!ptr) cout << "Nothing to display" << endl;
   while(ptr) {
       displayNode(ptr);
       ptr = ptr->next;
   }
}

int main()
{
   int id;
   string name;
    list myList;
   list::node* ptr;

   // add
   ptr = myList.initNode( "s1", 1 );
   myList.addNode(ptr);
   ptr = myList.initNode( "s2", 2 );
   myList.addNode(ptr);
   ptr = myList.initNode( "s3", 3 );
   myList.addNode(ptr);
   ptr = myList.initNode( "s4", 4 );
   myList.addNode(ptr);
   ptr = myList.initNode( "s5", 5 );
   myList.addNode(ptr);
   myList.displayList(myList.head);

   // delete
   name = "s2";
   ptr = myList.searchName( myList.head, name );
   if( ptr == NULL ) {
       cout << " Name: " << name << " not found" << endl;
   }
   else {
       cout << " Deleting a node ... ";
   myList.displayNode(ptr);
       myList.deleteNode( ptr );
   }
   myList.displayList(myList.head);

   // insert
   name = "s2";
   id = 2;
   ptr = myList.initNode( name, id );
   myList.insertNode( ptr );
   cout << " Inserting a node ... ";
   myList.displayNode(ptr);
   myList.displayList(myList.head);

   // reverse
   cout << " Reversing the list ... ";
   myList.reverse();
   myList.displayList(myList.head);

   // delete
   cout << " In the end, deleting the list ... ";
   myList.deleteList(myList.head);
   myList.displayList(myList.head);
   return 0;
}

Example:Detecting circular (loop) linked list

This code has the following

   1. Adding Nodes
   2. Function returning the size of the list
   3. Making the list circular (loop)
   4. Detecting circular list
   5. Recursive printing

#include <iostream>
using namespace std;

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

Node *root = 0;

void addNode(int n)
{
   if(root==0) {
       root = new Node;
       root->data = n;
       root->next = 0;
       return;
   }
   Node *cur = root;
   while(cur) {
       if(cur->next == 0) {
           Node *ptr = new Node;
           ptr -> data = n;
           ptr -> next = 0;
           cur -> next = ptr;
           return;
       }
       cur = cur->next;
   }
}

void makeCircular()
{
   if(!root || !root->next) return;
   Node *cur = root;
   while(cur) {
       if(cur->next == 0) {
           cur->next = root;
           return;
       }
       cur = cur->next;
   }
}

int sizeOfList()
{
   Node *cur = root;
   int size = 0;
   while(cur) {
       size++;
       if(cur->next == 0) {
           return size;
       }
       cur = cur->next;  
   }
   return size;
}

bool detectCircular()
{
   // Assuming the list may not be circular

   if(!root || !root->next) return false;
   Node *ptr1 = root;
   Node *ptr2 = root;

   while(ptr1 && ptr2) {
       ptr1 = ptr1->next;
       ptr2 = ptr2->next;
       if(ptr2) {
       ptr2 = ptr2->next;
       if(!ptr2) return false;
       }
       else {
           return false;
       }
       cout << ptr1->data << "," << ptr2->data << endl;
       if(ptr1==ptr2) {
           return true;
       }
   }
   return false;
}

void printRecursive(Node *ptr)
{
   if(!ptr) {
       cout << endl;
       return;
   }
   cout << ptr->data << " ";
   printRecursive(ptr->next);
}

int main(int argc, char **argv)
{
   addNode(10);addNode(20);addNode(30);addNode(40);addNode(50);
   addNode(60);addNode(70);addNode(80);addNode(90);addNode(100);
  
   printRecursive(root);

   cout << "size of list = " << sizeOfList() << endl;
     
   makeCircular();

   if(detectCircular()) cout <<"Circular ";
   else cout << "Normal ";
     
}

Output:

10 20 30 40 50 60 70 80 90 100
size of list = 10
20,30
30,50
40,70
50,90
60,10
70,30
80,50
90,70
100,90
10,10
Circular

Example 6 - Stack Using Linked List

This stack is using linked list as its data structure.

/* Stack operations */

#include <iostream>

using namespace std;

typedef struct Element
{
   void *data;
   struct Element *next;
} Element;

bool push(Element **top, void *data)
{
   Element *elem = new Element;
   if(!elem) return false;

   elem->data = data;
   elem->next = *top;
   *top = elem;
   return true;
}

bool createStack(Element **top)
{
   *top = NULL;
   return true;
}

bool pop(Element **top, void **data )
{
   Element *elem;
   if( !(elem = *top) ) return false;

   *data = elem->data;
   *top = elem->next;
   delete elem;
   return true;
}

bool deleteStack(Element **top)
{
   Element *elem;
   while(*top) {
       elem = (*top)->next;
       delete *top;
       *top = elem;
   }
   return true;

}

void printStack(Element *top)
{
   if(top==NULL) {
       cout << "Empty!" << endl;
   }
   Element *cur = top;
   while(cur) {
       cout << *(static_cast<int *>(cur->data)) << " ";
       cur = cur->next;
   }
   cout << endl << endl;
}

int main()
{
   Element *head;
   createStack(&head);
   int n1 = 10, n2 = 20, n3 = 30, n4 = 40, n5 = 50;
   push(&head, &n1);
   push(&head, &n2);
   push(&head, &n3);
   push(&head, &n4);
   push(&head, &n5);

   printStack(head);

   void * n;
   pop(&head, &n);
   cout << "popped " << *(static_cast<int*>(n)) << endl;
   pop(&head, &n);
   cout << "popped " << *(static_cast<int*>(n)) << endl;
   cout << endl;

   printStack(head);

   cout << "deleting stack..." << endl;
   deleteStack(&head);
   printStack(head);
}


Output:

50 40 30 20 10

popped 50
popped 40

30 20 10

deleting stack...
Empty!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote