473,785 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linklist problem

51 New Member
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 2106
weaknessforcats
9,208 Recognized Expert Moderator Expert
The loop in linklist::linea t is running to i<=x when it shoudl run to i<x.

The crash is not in your destructor but in linklist::linea t.

A simple debugger session would have found this.
Jan 19 '09 #2
dynamo
51 New Member
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
3011
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
3140
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. #include<stdio.h> #include<stdlib.h> struct node { int data;
6
2179
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. thank you.
0
1655
by: prashant0903 | last post by:
how i 'll made the linklist program in java both included insertion & deletion in program
5
2046
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.. class UTGLLElement { public int myData; // data item
2
2095
by: zubia | last post by:
hi how 2 deal with the rptr n lptr in doubly linklist
0
1344
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 getting how to pick timetable data from file using linklist file can b of any format there shud b six days n 6 lecture a day(maximum) plz help me for coding this problem
1
2388
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 lectures a day consist of 90mins each. I have not tried this problem yet becuase i dont know how to get data from file using linklist. Dnt give me a code for getting data from file but atleast give me idea how to get data from file so that i become...
2
1925
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; down here am posting my code; please somebody who can point it out; let me know fast; whats wrong in it... //Delete a number next to the given number void afterdelete() { struct linklist*temp=NULL; int no=0;
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.