Like Tree3Likes
  • 1 Post By Elkvis
  • 2 Post By iMalc

linked list

This is a discussion on linked list within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int main() { typedef struct list { int info; struct list *link; }node; ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    linked list

    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
        typedef struct list
        {
            int info;
            struct list *link;
        }node;
        int a;
        char res;
        node *start,*ptr,*ptr1,*ne,*ne1;
        do{
        
        cout<<"Enter 1 to create the list, 2 to insert in the begining, 3 to insert at the end, 4 to insert at a particular location";
        cin>>a;
        switch(a)
        {
        case 1:    
        cout<<"Enter the number of nodes in the linked list"<<endl;
        int n;
        cin>>n;
        ptr=(node*)malloc(sizeof(node));
        start=ptr;
        cout<<"Enter the info";
        cin>>start->info;
        ptr1=start;
        for(int i=1;i<=(n-1);i++){
                ptr=(node*)malloc(sizeof(node));
                ptr1->link=ptr;
                cout<<"Enter the info";
                cin>>ptr->info;
                ptr->link=NULL;
                ptr1=ptr;
                }
                ptr=start;
        while(ptr!=NULL)
        {
            cout<<ptr->info <<" --> ";
            ptr=ptr->link;
    }
    cout<<"NULL"<<endl;
    
    
    cout<<"Do You Want to repeat?";
    cin>>res;
    break;
    case 2:
        ne=(node*)malloc(sizeof(node));
        ne->link=start;
        start=ne;
        cout<<"Enter the info";
        cin>>ne->info;
            ptr=start;
        while(ptr!=NULL)
        {
            cout<<ptr->info <<" --> ";
            ptr=ptr->link;
        }
        cout<<"NULL"<<endl;
    cout<<"Do You Want to repeat?"<<endl;
    cin>>res;
        break;
        case 3:
        ne=(node*)malloc(sizeof(node));
         int p=1;
         ptr=start;
         while(p!=(n))
         { 
           ptr=ptr->link;
           p++;
         
         }
        ptr->link=ne;
        ne->link=NULL;
        cout<<"Enter the info";
        cin>>ne->info;
            ptr=start;
        while(ptr!=NULL)
        {
            cout<<ptr->info <<" --> ";
            ptr=ptr->link;
        }
        cout<<"NULL"<<endl;
        cout<<"Do You Want to repeat?"<<endl;
        cin>>res;
        break;
        
        case 4 :
            int pos;
            cout<<"Enter the position";
            cin>>pos;
              ne1=(node*)malloc(sizeof(node));
              int f=1;
             
              ptr=start;
              while(f!=(pos))
         { 
           ptr=ptr->link;
           f++;
         
         }
    ne1->link=ptr->link;
    ptr->link=ne1;
    ptr=start;
        while(ptr!=NULL)
        {
            cout<<ptr->info <<" --> ";
            ptr=ptr->link;
        }
        cout<<"NULL"<<endl;
    cout<<"Do You Want to repeat?"<<endl;
    cin>>res;
        break;
            
        }
        }while(res=='y');
        getch();
        return 0;
    }

    D:\Programming\link_new.cpp In function 'int main()':
    90 7 D:\Programming\link_new.cpp [Error] jump to case label [-fpermissive]
    67 7 D:\Programming\link_new.cpp [Error] crosses initialization of 'int p'

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Start by indenting your code properly.

    Note that if you want to declare a variable within a switch, you generally should create a local scope using a block (i.e., insert a set of braces). This is what the "jump to case label (...) crosses initialization of 'int p'" error message is trying to tell you. However, in such a case, maybe you should declare a function instead and call it from within the switch.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,266
    why are you using malloc to create nodes? you should be using new.

    also this:

    Code:
    typedef struct list
    {
        int info;
        struct list *link;
    }node;
    can be simplified to:

    Code:
    struct node
    {
        int info;
        node *link;
    };
    you seem to be doing a lot of things the "C" way. where did you get these habits from?
    Elysia likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    In fact, using malloc instead of new can cause invisible errors in your program, so don't do it. Use new/delete if you must, but even better is to avoid it altogether.
    Also, C++ has a linked list built in called std::list. Unless you mean to build a linked list as an exercise, it would be best to use that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    20
    Your code looks ugly !

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by ddutch View Post
    Your code looks ugly !
    That may be so, but saying that and nothing more comes across only as a putdown, when in fact you could have provided constructive advice about how to improve it.

    Start as Laserlight suggests.
    Elkvis and Salem like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21