| | |
problem with singly linked list
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 12
Reputation:
Solved Threads: 0
Ok i'm trying to build a header file which then i can include it for programs that use singly linked lists but somthing is fishy here...:
here's the code
and here's the program which tests these functions:
here's the code
C++ Syntax (Toggle Plain Text)
#include <iostream.h> struct nod { int info; nod* next_adr;}; void add(nod* &v, nod* &sf, int val) { nod *c; if(v==0) //if list empty { v=new nod; v->info=val; v->next_adr=0; sf=v; } else { c=new nod; sf->next_adr=c; c->info=val; c->next_adr=0; sf=c; } } void insert_after(nod* v, nod*& sf, int val, int val1) { nod* c=v,*d; while(c->info!=val) c=c->next_adr; d=new nod; d->info=val1; //the member _info_ of the node represented by _d_ takes the value of _val1_ d->next_adr=c->next_adr; if(d->next_adr==0) sf=d; } void insert_before(nod* &v, int val, int val1) { nod* c,* d; if(v->info==val) { d=new nod; d->info=val1; d->next_adr=v; v=d; } else { c=v; while(c->next_adr->info!=val) c=c->next_adr; d=new nod; d->info=val1; d->next_adr=c->next_adr; c->next_adr=d; } } void _delete(nod* &v, nod* &sf, int val) { nod *c,*man; if(v->info==val) { man=v; v=v->next_adr; } else { c=v; while(c->next_adr->info!=val) c=c->next_adr; man=c->next_adr; c->next_adr=man->next_adr; if(man==sf) sf=c; } delete man; } void list_it(nod* v) { nod* c=v; while(c) { cout<<c->info<<endl; c=c->next_adr; } cout<<endl; }
and here's the program which tests these functions:
C++ Syntax (Toggle Plain Text)
#include <liste.h> #include <conio.h> nod* v,* sf; void main() { clrscr(); int i; for(i=1;i<=10;i++) add(v,sf,i); list_it(v); insert_after(v,sf,7,11); insert_after(v,sf,10,12); insert_after(v,sf,1,13); list_it(v); /// PROBLEM: There are no changes made insert_before(v,13,14); insert_before(v,1,15); list_it(v); /// PROBLEM: Program enters in an infinite loop and when breaked points me to "while(c->next_adr->info!=val)" @ insert_before() function in the header file _delete(v,sf,15); _delete(v,sf,13); _delete(v,sf,12); list_it(v); /// PROBLEM: same problem as before except this time points me to the while in the _delete() function getch(); }
Last edited by xeption12; Dec 22nd, 2007 at 10:19 am.
![]() |
Similar Threads
- Deleting Pointed node in Singly Linked List (C)
- Linked List problem (C)
- linked list question (C)
- wat list??? (Computer Science)
- Singly-Linked Lists problem (C++)
- File writing Error (C)
- I need help with a linked list program (C++)
Other Threads in the C++ Forum
- Previous Thread: joining two chars together and converting to INT
- Next Thread: ReadFile crashes needs more
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





the bug was in insert_after() ... i forgot to link the node that has the value val to the new node... 