What I need to be able to do:Code:#include "iostream.h" #include "bool.h" struct node { int number; node * next; }; void create(node * &head); void print(node * head); void kill(node * &head); void insert(node * & head); int main() { node * head=NULL; create(head); print(head); for(int i=0;i<4;i++) { kill(head); print(head); insert(head); print(head); } } void create(node * &head) { int num,flag=0; node * now; cout << "Enter a positive number, or a negative number to quit: "; cin >> num; if(num>0) { head = new node; now = head; } while (num > 0) { if(flag==0) { now->number=num; flag++; } else { node * newnode = new node; newnode->number = num; now->next = newnode; now = now->next; } cout << "Enter a positive number, or a negative number to quit: "; cin >> num; if(num<0) now->next = NULL; } } void print(node * head) { node * now = head; cout << "\nOriginal List\n"; if (now == NULL) cout << "List is empty"<<endl; while (now != NULL) { cout << now->number<<endl; now = now->next; } } void kill(node * &head) { bool found = false; int search; cout << "\nWhich number would you like to delete? "; cin >> search; if(head!=NULL) { node * before = head; node * now = head->next; node * after = now->next; if(before->number==search) { head=before->next; found = true; } else { while (now != NULL) { if (now->number == search) { found = true; before->next = now->next; delete now; now = before->next; if(after==NULL) break; after=now->next; } else { if(after==NULL) break; before=before->next; now=now->next; after = after->next; } } } } if (!found) cout << "\nThere is no "<<search<<" in the list\n"; } void insert(node * & head) { node * after; bool putIn = false; if(head==NULL) { head = new node; head->next=NULL; int newnumber; cout << "\nEnter the number you wish to insert: "; cin >> newnumber; head->number=newnumber; putIn = true; } else { bool putIn = false; node * before = head; node * now = head->next; if(now!=NULL) node * after = now->next; if(now==NULL) { int newnumber; cout << "\nEnter the number you wish to insert: "; cin >> newnumber; node * newnode = new node; newnode -> number = newnumber; if(before->number>newnumber) { newnode->next = head; putIn = true; head=newnode; } else if(before->number<newnumber) { before->next=newnode; newnode->next=now; putIn = true; } } else { int newnumber; cout << "\nEnter the number you wish to insert: "; cin >> newnumber; node * newnode = new node; newnode -> number = newnumber; while (now != NULL) { if(before->number>newnumber) { newnode->next = head; putIn = true; head=newnode; break; } if (now->number < newnumber) { if(after==NULL) break; before = before->next; now = now->next; after = after->next; } else { before->next = newnode; newnode -> next = now; putIn = true; break; } } if (!putIn) { now->next = newnode; newnode->next = after; } } } }
1. Create a list with 2,4,6,8
2. Delete a 2
3. Insert a 2
4. Delete a 6
5. Insert a 7
6. Delete a 8
7. Insert a 10
8. Create a list with nothing on it
9. Delete a 7
10. Insert a 7
11. Create a list with a 2 in it
12. Kill the 2
13. Insert a 3
14. Create a list with 10,2,8,3,7,11,1,14 and sort the list.
Now I really don't care about the sort, but everything else is pretty much a must.
I can get it to do steps 8-10 just fine.
I can get it to do steps 11-13 just fine.
Steps 1-4 work fine until I get to step 7.
When I input a 7 it overrides the original 8.





Reply With Quote