473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

linklist problem

51
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function
but i see nothing wrong can you help.Thanx
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iostream>  
  3. #include <string>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <fstream>
  8. using namespace std;
  9. class linklist
  10. {
  11.      private:
  12.  
  13.              struct node
  14.          {
  15.               char data;
  16.               int line;
  17.             node *link;
  18.          }*p;
  19.  
  20.    public:
  21.  
  22.              linklist();
  23.          void append( char num ,int line);
  24.          void add_as_first( char num,int line );
  25.          void addafter( int c, char num,int line );
  26.          void del( char num );
  27.          void display();
  28.          char charat(int x);
  29.          int lineat(int x);
  30.          int count();
  31.          ~linklist();
  32. };
  33.  
  34. linklist::linklist()
  35. {
  36.      p=NULL;
  37. }
  38.  
  39. void linklist::append(char num,int line)
  40. {
  41.      node *q,*t;
  42.  
  43.    if( p == NULL )
  44.    {
  45.         p = new node;
  46.       p->data = num;
  47.       p->line=line;
  48.       p->link = NULL;
  49.    }
  50.    else
  51.    {
  52.         q = p;
  53.       while( q->link != NULL )
  54.            q = q->link;
  55.  
  56.       t = new node;
  57.       t->data = num;
  58.       t->line=line;
  59.       t->link = NULL;
  60.       q->link = t;
  61.    }
  62. }
  63.  
  64. void linklist::add_as_first(char num,int line)
  65. {
  66.      node *q;
  67.  
  68.    q = new node;
  69.    q->data = num;
  70.    q->line=line;
  71.    q->link = p;
  72.    p = q;
  73. }
  74.  
  75. void linklist::addafter( int c, char num,int line)
  76. {
  77.      node *q,*t;
  78.    int i;
  79.    for(i=0,q=p;i<c;i++)
  80.    {
  81.         q = q->link;
  82.       if( q == NULL )
  83.       {
  84.            cout<<"\nThere are less than "<<c<<" elements.";
  85.          return;
  86.       }
  87.    }
  88.  
  89.    t = new node;
  90.    t->data = num;
  91.    t->line=line;
  92.    t->link = q->link;
  93.    q->link = t;
  94. }
  95.  
  96. void linklist::del( char num )
  97. {
  98.      node *q,*r;
  99.    q = p;
  100.    if( q->data == num )
  101.    {
  102.         p = q->link;
  103.       delete q;
  104.       return;
  105.    }
  106.  
  107.    r = q;
  108.    while( q!=NULL )
  109.    {
  110.         if( q->data == num )
  111.       {
  112.            r->link = q->link;
  113.          delete q;
  114.          return;
  115.       }
  116.  
  117.       r = q;
  118.       q = q->link;
  119.    }
  120.    cout<<"\nElement "<<num<<" not Found.";
  121. }
  122.  
  123. void linklist::display()
  124. {
  125.      node *q;
  126.    cout<<endl;
  127.  
  128.    for( q = p ; q != NULL ; q = q->link )
  129.         cout<<endl<<q->data<<'\n'<<q->line;
  130.  
  131. }
  132. int linklist::lineat(int x){
  133.     node *q;
  134.     int num;
  135.     q=p;
  136.     for(int i=0;i<=x;i++){
  137.                   num=q->line;
  138.                   q=q->link; 
  139.                   }
  140.                   return num;
  141.                   }
  142. char linklist::charat(int x){
  143.     node *q;
  144.     char num;
  145.     q=p;
  146.     for(int i=0;i<=x;i++){
  147.                   num=q->data;
  148.                   q=q->link; 
  149.                   }
  150.                   return num;
  151.                   }
  152. int linklist::count()
  153. {
  154.      node *q;
  155.    int c=0;
  156.    for( q=p ; q != NULL ; q = q->link )
  157.         c++;
  158.  
  159.    return c;
  160. }
  161.  
  162. linklist::~linklist()
  163. {
  164.      node *q;
  165.    if( p == NULL )
  166.         return;
  167.  
  168.    while( p != NULL )
  169.    {
  170.         q = p->link;
  171.       delete p;
  172.       p = q;
  173.    }
  174. }
  175. int main(){
  176.     int x;
  177.     linklist l;
  178.     l.append('1',1);
  179.     l.append('2',2);
  180.     l.append('5',10);
  181.     l.del('2');
  182.     l.display();
  183.     cout<<l.lineat(2);
  184.     cin>>x;
  185. }
  186.  
Jan 19 '09 #1
2 2080
weaknessforcats
9,208 Expert Mod 8TB
The loop in linklist::lineat is running to i<=x when it shoudl run to i<x.

The crash is not in your destructor but in linklist::lineat.

A simple debugger session would have found this.
Jan 19 '09 #2
dynamo
51
thanx man.Really appreciate it.
Jan 19 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jova | last post by:
I'm stuck I have to convert an array to a linklist the array is initialized as public Organization{ Crew members = new Member etc....
7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
6
by: jwvai316 | last post by:
I don't really know how to say it so I just say it a nested linklist. How do you make LinkLists inside LinkList? Can anyone help me for this? I think an example program will help me a lot. ...
0
by: prashant0903 | last post by:
how i 'll made the linklist program in java both included insertion & deletion in program
5
by: Asembereng | last post by:
Hi i want to do insertion sort, merge sort and quick sort on my linkList of ints.. do anyone have an idea how to do it?? these are the codes. And i also need to define a lifo fifo class.. ...
2
by: zubia | last post by:
hi how 2 deal with the rptr n lptr in doubly linklist
0
by: smoothkriminal1 | last post by:
the question is u read timetable of 40 students from file n den find da slot where all fourty students dnt hve clash...if any.... may b i ll be able to make clash logic but i m even just nt...
1
by: smoothkriminal1 | last post by:
Write a Code that will pick timetable of 40 students from file using linklist n than find a slot where all the students dont have any class. file can be of any format Student can maximum take 6...
2
Parul Bagadia
by: Parul Bagadia | last post by:
I have written a code for deleting certain value from linklist; it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.