Linked lists

This is a discussion on Linked lists within the C++ Programming forums, part of the General Programming Boards category; I need some help, ive been trying for a few days to make a linked list, but it keeps on ...

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    Linked lists

    I need some help, ive been trying for a few days to make a linked list, but it keeps on crashing, or not working at all. its kind of wierd though, I can do opengl, but cant make a linked list :P

    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    
    
    
    class PATIENT
    {
    
    public:
    	char Name[10];
    	char Gender;
    	int  Age;
    	char Insurance;
    	char Insurance_Name[24];
    
    	PATIENT* pNext;
    };
    
    PATIENT* head;
    void AddPatient(PATIENT* pND)
    {
    
    	pND->pNext = NULL;
    
    	PATIENT* head;
    
    	if (head=0)
    	{
    		head = pND;
    		return ;
    	}
    
    	PATIENT* current = head;
    
    	while (current->pNext)
    	{
    		current = current->pNext;
    	}
    
    	current->pNext = pND;
    }
    
    
    void PrintList(PATIENT* head)
    {
    	while(head != NULL)
    	{
    		cout << head;
    	}
    }
    int main()
    {
        PATIENT* pND;
    
    	
    
    		cout <<" enter patient name : ";
    		cin >> pND->Name;
    		cout<<"\n\n";
    
    		AddPatient(pND);
    whats the matter with it, and can someone give a link to a site that has a good tutorial on linked lists, as all the books I have barely skim the topic.
    Last edited by EvBladeRunnervE; 03-31-2003 at 06:51 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The code is somewhat mixed up, but I have some general hints:
    seperate data and linked list code. Make a data class and a linked list class. Don't mix it. If you have pointers, make sure you actually have an object assigned with new.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    PATIENT* pND;
    cin >> pND->Name;
    You have a pointer to a patient - true, but it doesn't point at a patient. You need to allocate memory for it.
    Also, in your Addpatient you might want to traverse to the end of the list before inserting it. I see some attempt at that but generally your insertion function is pretty screwed.
    Code:
    pND->pNext = NULL;
    Why nullify it? You'll cut the list doing so, making the tail lost forever (don't count on windows handling that memory leak for ya ).

    Code:
    PATIENT* head;
    if (head=0)
    My poor eyes...
    First, you never allocate memory for it, second = is the assignment operator, not a comparator. Third, I'd use NULL instead of 0, but that's my taste (I know others who would argue the opposite, but let's hope they never find this thread ).
    Last edited by Magos; 03-31-2003 at 09:17 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    yes, I knopw, rather crappy attempt, but its only my first. Do you know where I could find an indepth tutorial to linked lists?

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You have a local variable named head that "shadows" the global varable named head . With a singlely linked list life is much easyer if you "push" elements on the head of your list, so the list will be in reverse order.

    Code:
    patent *head = 0;
    
    void push(patent *p) {
        if(p) {
            p->pNext = head;     
            head = p;
       } else {
             std::cerr << "NULL patent \n";
       }    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

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