Thread: Linked Lists problem

  1. #16
    Registered User
    Join Date
    May 2008
    Posts
    61
    alright, so heres my coding that ive come up with so far including the insert.. tell me where im completely wrong, since i am sure i am.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct SLL {
       int val;
       struct SLL * next;
    };
    
    typedef struct SLL element;
    
    int main() {
       element * curr, * head;
       int i;
    
       head = NULL;
    
       for(i=1;i<=10;i++) {
          curr = (element *)malloc(sizeof(element));
          curr->val = i;
          curr->next  = head;
          head = curr;
       }
    
       curr = head;
    
       while(curr) {
          printf("%d\n", curr->val);
          curr = curr->next ;
       }
    }
    
    
    void SLLinsert(shortint theID)
    {
        liData *newItem = new liData;
        newItem->num = theID;
        liData *prev = NULL;
        liData *temp = NULL;
        for(temp = ptrHead; temp->ptrNext != NULL; temp = temp->ptrHead)
        {
              // is new node's priority less than current node pointer's priority?
              if(newItem->num <  temp->num)
              {
                  if(prev == NULL)
                  {
                        // add new node at the head
                        newItem->ptrNext = temp;
                        ptrHead = newItem;
                        break;
                  }
                  else
                  {
                        // add new node somewhere in the middle of the list
                        newItem->ptrNext = temp;
                        prev->ptrNext = newItem;
                        break;
                   }
                   // save current node address
                   prev = temp;
        }
         // if it gets to here then all the nodes in the list have the same
         // priority as the new node.  So i'm just adding the new node to the 
         // head of the list.
         newItem->ptrNext = ptrHead;    
        ptrHead = newItem;
    }
    yes no?

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    61
    anyone see issues in this code? im still too new at this..

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Without running it, it looks to me like your "tie scenario" (at the bottom) will end up replacing the head every time, since the if else loop breaks in either case. I think you want an if...else if...else instead.

    No, I'm wrong. You're just missing a } in the outer if...but even if that were there, this would mean you don't can't deal with when the priority is higher. It looks like lower priorities go first? I also have a hard time believing your "for" loop iterates at all...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    61
    I see the missing } you speak of, and that can be fixed easily. but why don't you believe my "for" loop interates at all? am i missing something blatantly obvious?

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by needhelpbad View Post
    I see the missing } you speak of, and that can be fixed easily. but why don't you believe my "for" loop interates at all? am i missing something blatantly obvious?
    Maybe, I'm actually no expert. But what is a liData?
    Code:
    for(temp = ptrHead; temp->ptrNext != NULL; temp = temp->ptrHead)
    Since temp is a *liData, I don't know what ->ptrHead is supposed to be, but something seems suspicous here...

    edit: Whoa! I just noticed I'm on page 2, sorry, maybe I'll look back and see if I can find this liData...
    edit#2: I still don't see it anywhere.
    Last edited by MK27; 12-07-2008 at 10:18 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    liData *newItem = new liData;
    this is not C
    for(temp = ptrHead; temp->ptrNext != NULL; temp = temp->ptrHead)
    Shouldn't the red part be ptrNext ?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    May 2008
    Posts
    61
    to be honest, i never really understood what my professor was doing with liData either. But I have no idea what else I would use in place of what its doing, so I supposed I would just use it blindly

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by needhelpbad View Post
    to be honest, i never really understood what my professor was doing with liData either. But I have no idea what else I would use in place of what its doing, so I supposed I would just use it blindly
    Are you saying you do not know what struct liData even looks like!!??? I don't.

    The point was that you MUST know what this struct is, or your whole program has exactly zero chance of ever compiling. But you didn't include it in any of your posts, so someone else looking at this will be stuck throwing up their hands.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #24
    Registered User
    Join Date
    May 2008
    Posts
    61
    i looked through my notes back and front and couldnt find anything about his struct that he created, seems he just referred to it without really showing it...

    so i thought id try and do a fresh insert with pre and post declarations just to get the idea of it down.. tell me what u think..

    Code:
    /* SLLinsert: inserts a new entry into the list
     * Pre conditions: The list created, not full, x is a valid list entry, p is a valid position
     * Post conditions: x has successfully been inserted into position p in list
     */
    
    void SLLinsert(Position p, ListEntry x, List *list) {
         ListNode *newNode, *current;
         
         if (p < 0 || p > list->count)
              Error("Attempt to insert in a position not in the list.");
         else {
              newNode = MakeListNode(x);
              if (p == 0) {
                   newNode->next = list->head;
                   list->head = newNode;
                   }
              else {
                   SetPosition(p-1, list, &current);
                   newNode->next = current->next;
                   current->next = newNode;
                   }
              list->count++;
              }
    }

  10. #25
    Registered User
    Join Date
    May 2008
    Posts
    61
    does that make more sense?

  11. #26
    Registered User
    Join Date
    May 2008
    Posts
    61
    while waiting for assistance from you actually knowledgeable people.. :P

    I decided I would try and make a flat example of how I believe the deletion would work, tell me if this also looks correct or if im missing something obvious

    Code:
                                        // If the node is not the starting or the last one
    void delete(int n) {      //deletes the nth position node of the linked list
         node *p;                 //node - type pointer to traverse the linked list
         int i = 1;
         while(i < n-1) {
              p = p->next;      //points to next node
              ++i;
         }
                                        //Now p is at the node previous to the node to be deleted
         node *q;                 //declares another pointer
         q = p->next;           //q is next to q
         p->next = q->next;
         free(q);
    }
    thanks

  12. #27
    Registered User
    Join Date
    May 2008
    Posts
    61
    do the last two codings look concievable to anyone?

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your delete function will cheerfully walk off the end of the list, should you pass some very large number in for n.

  14. #29
    Registered User
    Join Date
    May 2008
    Posts
    61
    where is the issue that would cause that to happen in the code?

    im not recognizing where n being large would have an issue..

  15. #30
    Registered User
    Join Date
    May 2008
    Posts
    61
    aside from what ive said above..

    how would i throw in the exceptions for empty and repeat nodes?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  2. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  3. Linked lists
    By sballew in forum C Programming
    Replies: 6
    Last Post: 10-24-2001, 08:52 PM
  4. I need some help on my linked lists app
    By valar_king in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:36 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM