Single Link List

Hi,
Please help, im having a problem to output. Below is the output and code.

Counter : 0

aa bb cc
Total Characters : 8
ID: 1 Value: a
ID: 2 Value: a
ID: 3 Value:
ID: 4 Value: b
ID: 5 Value: b
ID: 6 Value:
ID: 7 Value: c
ID: 8 Value: c

Free start
trying to free pointer ID:1
OK
trying to free pointer ID:2
OK
trying to free pointer ID:3
OK
trying to free pointer ID:4
OK
trying to free pointer ID:5
OK
trying to free pointer ID:6
OK
trying to free pointer ID:7
OK
trying to free pointer ID:8
OK

Counter : 1

aa
Total Characters : 3
ID: 1 Value: a
ID: 2 Value: a
ID: 3 Value:

Free start
trying to free pointer ID:1
OK
trying to free pointer ID:2
OK
trying to free pointer ID:3
OK



name[0] = "aa bb cc"
name[1] = "aab"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>                                                                                                                            
#include<stdio.h>                                                                                                                             
#include<stdlib.h>                                                                                                                            
#include<string.h>                                                                                                                            
#include<vector>                                                                                                                              

using namespace std;

typedef struct link {
   int  id;          
   char c;           
   link *next;       
} mylink;            

mylink *first = NULL;
mylink *current = NULL;
mylink *ptr = NULL;    
mylink *last = NULL;   
const int total_loop = 2;

int countChar(){
        int ctr = 0;
        ptr = first;
        while(ptr != NULL){
                ctr++;     
                ptr = ptr->next;
        }                       
        return ctr;             
}                               

void freePtr(){
        ptr = first;
        mylink *tmp;
        cout << "Free start" << endl;
        while(ptr != NULL){          
                tmp = ptr->next;     
                cout << "trying to free pointer ID:" << ptr->id << endl;
                free(ptr);ptr=NULL;                                     
                if( ptr == NULL ){                                      
                        cout << "OK" << endl;                           
                }else{                                                  
                        cout << "FAILED" << endl;                       
                        cout << "Value of ID:" << ptr->id << ptr->c << endl;
                }                                                           
                ptr = tmp;                                                  
        }                                                                   
        last =NULL;                                                         
        first =NULL;                                                        
}                                                                           

int getLastID(){
        if( last == NULL ){
                return 1;  
        }                  
        return last->id+1; 
}                          

void printValue(){
        mylink *pTmp;
        pTmp = first;
        while(pTmp != NULL){
                cout << "ID: " << pTmp->id << " Value: "  << pTmp->c << endl;
                pTmp = pTmp->next;                                           
        }                                                                    
}                                                                            

char * getValue(){
        int total_char = countChar();
        char *pVal = NULL;           
        pVal = new char[ total_char ];
        mylink *tmp = first;          
        int ctr=0;                    
        while(tmp != NULL){           
                pVal[ctr] = tmp->c;   
                tmp = tmp->next;      
                ctr++;                
        }                             

        return pVal;
}                   

void printValue(vector<char*> name){
        for(int a=0; a<name.size(); a++){
                cout << "name[" << a  <<  "] = \"" << name.at(a) << "\"" << endl;
        }
}

int main(){
        char c;
        vector<char*> name(total_loop);
        for(int a=0; a<total_loop; a++){
                cout << "Counter : " << a << endl << endl;
                for(;;) {
                        c = cin.get();
                        if( c == '\n' ) break;
                                ptr = new mylink;
                                if (ptr == NULL) {
                                        cout << " Insufficient memory\n";
                                        exit(1);
                                }
                                ptr->c = c;
                                ptr->id = getLastID();
                                ptr->next = NULL;
                                if (first == NULL)
                                        first = current = ptr;
                                else
                                        current->next = ptr;
                                current = ptr;
                                last = current;
                }
                cout << "Total Characters : " << countChar() << endl;
                printValue();
                cout << endl;
                //cout << "Value fetch \"" << getValue() << "\"" << endl;
                name.at(a) = NULL;
                name.at(a) = getValue();
                freePtr();
                cout << endl;
        }
        cout << endl << endl;
        printValue(name);
        return 0;
}
Last edited on
What exactly is your question?
Topic archived. No new replies allowed.