Delelte Nodes

When i call my delete node function with input 1, it should delete the #1 in the linked list. Instead it skips over 1 and deletes 2.
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
  #include <iostream>
#include <cstring>      // for getline
#include <cstdlib>      // for the system command
#include <cctype>       // For the letter checking functions, e.g. toupper( )
#include <fstream>      // For file input and output
#include <ctime> 

using namespace std;

class Node{
  public:
  int data;
  Node *pNext;
  
  void prepend(Node *&pHead1, int numbers){
      Node *pTemp;
      pTemp=new Node;
      pTemp->data=numbers;
      pTemp->pNext=pHead1;
      pHead1=pTemp;
      
  }
  
  Node *findlast(Node *pHead2){
      while(pHead2!=NULL && pHead2->pNext !=NULL){
          pHead2=pHead2->pNext;
      }
      return pHead2;
  }
  
  
  void append(Node *&pHead1, int numbers){
      Node *pTemp;
      pTemp=new Node;
      pTemp->data=numbers;
      pTemp->pNext=NULL;
      Node *end=findlast(pHead1);
      if(end==NULL){
          pHead1=pTemp;
      }else{
          end->pNext=pTemp;
      }
      
  }
  
  void display(Node *pHead1){
      Node *pTemp=new Node;
		pTemp=pHead1;
		
		while(pTemp!= NULL){
		
				cout<< pTemp -> data ;		
				pTemp=pTemp->pNext;
			
			
		}
  }
  
  void delete_at_n(Node *pHead1,int index){
      int counter=0;
      Node *pTemp=new Node;
      pTemp=pHead1;
      Node *pDelete;
      Node *pAfterDelete;
       
      while(pTemp!=NULL){
          pTemp=pTemp->pNext;
          
         
          if(counter==index-1){
              pDelete=pTemp->pNext;
              
              pAfterDelete=pTemp->pNext->pNext;
              pTemp->pNext=pAfterDelete;
              delete pDelete;
             
          }
           counter=counter+1;
          
      }
      
      
  }
    
  
};



int main()
{
    Node useNode;
    Node *pHead=NULL;
    
    
    for(int i =0;i<11;i++){
        useNode.append(pHead,i);
    }
    
    useNode.delete_at_n(pHead,1);
    
    useNode.display(pHead);
    
    
    

    return 0;
}




You have absolutely no understanding about how classes work.
L
@fivestar -- Node should be something fairly simple; just a tiny struct for example. Manage your list with something else, like a NodeManager class. You're also leaking a ton of memory -- what does the keyword new mean, and what does it imply?

See here for an example of what I'd written previously to help someone Quicksort a LinkedList https://repl.it/repls/DentalGainsboroHexagon
Last edited on
Topic archived. No new replies allowed.