Q BgQuestion:

Guru
Karma Points: 2,026
Respect (90%):
posted by  Duke12 on 7/21/2008 12:38:28 PM  |  status: Live  

creating a link list c++

Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
xTest.txt looks like:
Z
z
C
c
b
B

and yTest.txt looks like:
A
b
D
e
f
g
i
j
l
n
P
r
T
u
W
Z
a
c
D
G
i
k
m
O
q
s
U
V
y

Part 1) Implement a main.cpp that create linked list. Then read files of the the same format as xTest.txt, insert each character into the linked list. After all elemenets of the file are inserted in the list display the entire list. Then delete the entire list. To sort upper and lower case, toupper, could be useful.

Output Example:
The list now contains:

B

b

C

c

Z

z



Deleting the list:

Deleting element B

Deleting element b

Deleting element C

Deleting element c

Deleting element Z

Deleting element z

Deletion complete



Press any key to continue . . .



And the yTest outputs what it is in like the output above

I take the time to answer your question. Please take the time to rate it.
Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

AAnswers:

Answer Question
Expert
Karma Points: 811
posted by sam_GT on 7/22/2008 1:02:17 AM  |  status: Live
Asker's Rating: Helpful   
Duke12's comment:
"thanks for the help"
Response Details:
//compiled in g++ compiler

#include<iostream.h>
#include<stdio.h>
#include<ctype.h>
int insert_node(char ch);
int l_alpha(char ch) // to find is it lower or upper alpahbat
{
if(ch >= 'a' && ch <='z')
return 1;
else
return 0;
}

int u_alpha(char ch)
{
if(ch >= 'A' && ch <='Z')
return 1;
else
return 0;
}
struct node
{
char ch;
struct node *next;
}*first,*last,*ptr; //list of ch
int delete_nodes(struct node *ptr) //function that will delete all nodes
{
struct node *tptr;

cout<<"Deleting the list:\n";
while(ptr != NULL)
{
tptr= ptr;
ptr=ptr->next;
cout<<"Deleting element "<<tptr->ch<<"\n";
delete tptr;
}
}
int main()
{
char ch;
FILE *fp;
fp = fopen("xTest.txt","r");
while((ch=fgetc(fp) ) != EOF)
{
insert_node(ch); // insert node in the list
}
fclose(fp);
ptr=first;

cout<<"\n\n\nThe list now contains:\n";
while(ptr != NULL)
{
cout<<"\n "<<ptr->ch;
ptr=ptr->next;
}
cout<<"\n";
delete_nodes(first);
first=NULL;
fp = fopen("yTest.txt","r");
while((ch=fgetc(fp) ) != EOF)
{
insert_node(ch);
}
fclose(fp);
ptr=first;

cout<<"The list now contains:\n";
while(ptr != NULL)
{
cout<<"\n "<<ptr->ch;
ptr=ptr->next;
}
cout<<"\n";
delete_nodes(first);


}

int insert_node(char ch)
{
struct node *tptr;
char CH;
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) //valid ch or not
{

}else
{
return -1;
}


if(first ==NULL)// first node insertion
{
first = last = (struct node*)new struct node;
last->ch = ch;
last->next = NULL;
}else
{
ptr= first;
while(ptr !=NULL)
{
if((ch >= 'a' && ch <= 'z'))
{
if((ptr->ch >= 'a' && ptr->ch <= 'z'))
{
tptr=ptr->next;
if(tptr != NULL)
{
if(ch>= ptr->ch && ch <= tptr->ch && l_alpha(tptr->ch))
break;
if(ch>= ptr->ch && toupper(ch) <= tptr->ch && u_alpha(tptr->ch))
break;
}

}
else
{
tptr=ptr->next;

CH=toupper(ch);
if(tptr == NULL)
{
if( ch <= tolower(ptr->ch))
break;
}else
{
if(CH >= ptr->ch && ch <= tptr->ch && l_alpha(tptr->ch))
break;
else
if(CH >= ptr->ch && CH <= tptr->ch && u_alpha(tptr->ch))
break;

}
}
}
if((ch >= 'A' && ch <= 'Z'))
{
if((ptr->ch >= 'A' && ptr->ch <= 'Z'))
{
tptr=ptr->next;
if(tptr!=NULL)
{
if(ch>= ptr->ch && ch <= tptr->ch && u_alpha(tptr->ch) )
break;
if(ch>= ptr->ch && tolower(ch) <= tptr->ch && l_alpha(tptr->ch) )
break;
}

}
else
{
tptr=ptr->next;

CH=toupper(ptr->ch);
if(tptr == NULL)
{
}else
{
if(ch > CH && ch <= tptr->ch && u_alpha(tptr->ch))
break;
else
if(ch > CH && tolower(ch) <= tptr->ch && l_alpha(tptr->ch))
break;

}


}
}

last=ptr;
ptr=ptr->next;
}
if(ptr ==NULL)
{
if(l_alpha(ch) && l_alpha(first->ch))
{
if(ch <= first->ch)
{
tptr=first;
ptr = (struct node*)new struct node;
ptr->ch = ch;
ptr->next = tptr;
first = ptr;
return 0;
}
}
else
{
if(u_alpha(ch) && u_alpha(first->ch))
{
if(ch <= first->ch)
{
tptr=first;
ptr = (struct node*)new struct node;
ptr->ch = ch;
ptr->next = tptr;
first = ptr;
return 0;
}
}
else
{
if(l_alpha(ch) && u_alpha(first->ch))
{
ch=toupper(ch);
if(ch <= first->ch)
{
tptr=first;
ptr = (struct node*)new struct node;
ptr->ch = tolower(ch);
ptr->next = tptr;
first = ptr;
return 0;
}ch=tolower(ch);
}
else
{
if(u_alpha(ch) && l_alpha(first->ch))
{
ch=tolower(ch);
if(ch <= first->ch)
{
tptr=first;
ptr = (struct node*)new struct node;
ptr->ch = toupper(ch);
ptr->next = tptr;
first = ptr;
return 0;
}ch=toupper(ch);
}


}


}
}
last->next = (struct node*)new struct node;
last = last->next;
last->ch = ch;
last->next = NULL;
}else
{
tptr=ptr->next;
ptr->next = (struct node*)new struct node;
ptr = ptr->next;
ptr->ch = ch;
ptr->next = tptr;

}
}
}


Guru
Karma Points: 2,026
posted by Duke12 on 7/22/2008 1:48:38 AM  |  status: Live
Asker's Rating: N/A-Posted by Person Asking Question   
Response Details:
thanks...I have the code for part 1 but what I need help on now is the outputting what element is being deleted. I have it not outputting anything

Part1.cpp

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

void part1::appendList(char num)
{
    listNode *newNode;
    listNode *nodePtr;

    newNode = new listNode;
    newNode->letter = num;
    newNode->next = NULL;

    if(!head)
        head = newNode;
    else
    {
        nodePtr = head;

        while(nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}

void part1::displayList() const
{
    listNode *nodePtr; // move through the list

    nodePtr = head; //position nodePtr at head of the list

    while(nodePtr)
    {
        cout << nodePtr->letter << endl;//display value

        nodePtr = nodePtr->next;//move to next node
    }
}

void part1::insertNode(char num)
{
    // get a new node to insert
    listNode *newNode;
    listNode *nodePtr; // to traverse the list
    listNode *previousNode = NULL;  // the previous node
    // allocate a new node
    newNode= new listNode;
    newNode->letter=num;

    if(!head)
    {
        head = newNode;
        newNode->next=NULL;
    }
    else
    {
        nodePtr = head;
        previousNode= NULL;
   
      while ( nodePtr!=NULL && toupper(nodePtr->letter) <= toupper (num))
      {
         if (nodePtr->letter >num&&toupper (nodePtr->letter) == toupper (num))   //current is the last in list
         {
               break;
         }
              previousNode  = nodePtr;
              nodePtr=nodePtr->next;
            
      }
        if(previousNode ==NULL)
        {
            head=newNode;
            newNode->next=nodePtr;
        }
        else
        {
            previousNode->next=newNode;
            newNode->next=nodePtr;
        }
         
      }

   }


part1::~part1()
{
    listNode *nodePtr;
    listNode *nextNode;
    cout << "Deleting the list" << endl;
    nodePtr=head;

    while(nodePtr != NULL)
    {
        nextNode=nodePtr->next;
        cout << "Deleting Element:" << endl;
        delete nodePtr;
        nodePtr=nextNode;
    }
    cout << "Deleting complete" << endl;
}

main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "part1.h"

using namespace std;

int main()
{
    char name;
    ifstream infile;
    infile.open("demoTest.txt");
    part1 list;
    
if(!infile)
    cout << "Can not open file" << endl;
else
{
    infile >> name;

    while(infile)
    {
        list.insertNode(name);
        infile >> name;
    }

    cout << "The list now contains:" << endl;
    list.displayList();
}
    return 0;
}

I take the time to answer your question. Please take the time to rate it.
Apprentice
Karma Points: 116
posted by APPOLLO on 7/22/2008 2:25:05 AM  |  status: Live
Asker's Rating: Helpful   
Duke12's comment:
"thanks"
Response Details:
   while(nodePtr != NULL)
    {
        nodePtr=nextNode; 
        cout << "Deleting Element:" << endl;
        nextNode=nextNode->next;
        delete nodePtr;
    }
 
Hi, you should change the sequence of your sentences and make some changes, since u need to change the pointer to the next element first and then delete the previous element. Otherwise, you will not get the value which element pointer points to.  Check it again! 
(Cramster SME)
Moderator
posted by Charlie (Cramster SME) on 7/22/2008 2:40:58 AM  |  status: Live
Asker's Rating: Helpful   
Duke12's comment:
"thanks for the code....I had already started on my own that i posted so could not use much of this."
Response Details:
 
Dear User
 
Hope the following solution program will help you. Still need any assistance / clarification ? Remember, CRAMSTER is always there to SUPPORT YOU.
 
 
 
Guru
Karma Points: 2,026
posted by Duke12 on 7/22/2008 3:33:57 AM  |  status: Live
Asker's Rating: N/A-Posted by Person Asking Question   
Response Details:
thanks apollo. I am stuck with trying to initialize variable 'nextNode' in that while loop. It is a warning but it is what is messing it up. I am to tired and will mess with it tomorrow.
I take the time to answer your question. Please take the time to rate it.
Apprentice
Karma Points: 116
posted by