Thread: Deleting from a linked list.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Deleting from a linked list.

    I am trying to delete information from a linked list that i entered. I can get the infor into the linked list but i cannot delete it ---Any assistence or insight would be appreciated.

    Code:
    ///TYPE.h///
    #define MAXNUM  10
    #define MAXNAME 30
    
    typedef struct enode *Enodeptr;
    typedef struct snode *Snodeptr;
    typedef struct cnode *Cnodeptr;
    
    struct studentType {
      char sid[MAXNUM];
      char sname[MAXNAME];
    };
    
    struct courseType {
      char cno[MAXNUM];
      char title[MAXNAME];
      int hours;
    };
    
    struct snode {
      struct studentType student;   /*data stored in the node*/
      Enodeptr firstE;
      Snodeptr next,prior;     /*pointer to the next node and previous */
    };
    
    struct cnode {
      struct courseType course;
      Enodeptr firstE;
      Cnodeptr next,prior;
    };
    
    struct enode {
      Enodeptr sNext, cNext;
      Snodeptr sOwner;
      Cnodeptr cOwner;
      char grade;
    };
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #include "types.h"
    #include "students.h"
    #include "enrolls.h"
    
    Snodeptr sAlloc(void) { /*Working*/
    /*Allocates storage for a snode and returns pointer to the structure */
      return (Snodeptr) malloc(sizeof(struct snode));
    }
    
    void makenullStudents(Snodeptr *students) {     /*Working*/
    /*Creates an empty list of students and returns pointer to dummy node in the variable students */
    (*students) = sAlloc();
    }
    
    int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s) {
    /*returns 0 if student structure x is present in students list
      and 1 otherwise. It also returns a pointer (in s) to the snode where it
      found x; If x is not found it should return a pointer to the snode
      where x should be inserted */
    
      Snodeptr p = students;
      Snodeptr p1 = *s;
    
      while(strcmp(p->next->student.sid, x.sid))
      {
        p = p->next;
      }
    
      return 0;
    }
    
    int insertStudents(struct studentType x, Snodeptr students) {   /*Working*/
    /*inserts the student structure x in the student list; returns 0
     if successful and 1 if failed */
     Snodeptr p = students;
    
     while(p->next != NULL)
     {
       p = p->next;
     }
    
     Snodeptr new = malloc(sizeof(struct snode));
    
     new->student = x;
     p->next = new;
     new->prior = p;
     new->next = NULL;
    
     return 0;
    }
    
    int deleteStudents(struct studentType x, Snodeptr students, Cnodeptr courses) {  /*Working*/
    /* deletes the student structure x in the student list; returns 0
     if successful and 1 if failed */
     Snodeptr p = students;
     Snodeptr p1;
    
     while(strcmp(p->next->student.sid, x.sid))
     {
       p = p->next;
     }
    
     p1 = p->next;
     p->next = p->next->next;
     p->next->prior = p;
    
     return 0;
    }
    
    void printStudents(Snodeptr students) {     /*Working*/
    /* prints the list of students (sid and names) in pretty format with
     -more- like option */
     int count = 0;
     Snodeptr p = students;
     char ch;
    
     while((p->next)!=NULL)
     {
       printf("sid = %s, name = %s   \n", p->next->student.sid, p->next->student.sname);
       p = p->next;
     }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you start with an empty list, how does insertStudents return a new list head?

    How did you arrive at the notion that it was "Working"?

    Show us a test main which calls insert, etc.
    Is there something special about the head node which means you never have to worry about inserting or deleting from the front of the list?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'd start by fixing this so that it doesn't run off the end of the list and crash if the item is not found.
    Quote Originally Posted by kenjiro310 View Post
    Code:
     while(strcmp(p->next->student.sid, x.sid))
     {
       p = p->next;
     }
    Next you'll need to actually call 'free' on something.
    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. Deleting from linked list
    By Golf7 in forum C Programming
    Replies: 1
    Last Post: 06-20-2010, 01:49 PM
  2. Linked List deleting Help
    By StealthRT in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 02:19 AM
  3. deleting for a linked list
    By vidioholic in forum C Programming
    Replies: 15
    Last Post: 09-14-2008, 12:32 PM
  4. Deleting a linked list help...
    By Squeage in forum C Programming
    Replies: 8
    Last Post: 03-13-2008, 06:13 PM
  5. Deleting - Linked List
    By TeeTee in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 07:39 PM