添加细节链接结构

时间:2017-06-01 16:25:01

标签: c

我写了这段代码:(上面的结构和下面的函数)

typedef struct course {
    char *title;
    int  number;
    struct slist *students;
} course;

typedef struct slist {
    student      *info;
    struct slist *next;
} slist;

typedef struct clist {
    course       *info;
    struct clist *next;
} clist;

结构是:

MERGE WITESTCO.dbo.[WIBOMD] AS target  
USING ( SELECT [STOCK NO]    
              , u.rev
              , bomEntry  = row_number() over (order by u.ordinal)
              , u.Partid
              , Qty       ='1'
              , cmnt      = 'TEST'
              , srcLoc    = 'TEST'
              , dType     = '0'
              , lead      = '0'
              , lineNbr   = row_number() over (order by u.ordinal)
        FROM  [inserted]
              CROSS APPLY ( VALUES  ([bomRev],1,[BOM-WHEEL PN])
                                  , ([bomRev],2,[BOM - RIM])
                                  , ([bomRev],3,[BOM - SECONDARY DISC PN])
                                  , ([bomRev],4,[BOM - FIN DISC PN])
                                  , ([bomRev],5, [BOM - FLAT FIN DISC PN])
                                  , ([bomRev],6,[WHL BOM PART 1 PN])
                                  , ([bomRev],7,[WHL BOM PART 2 PN])
                                  , ([bomRev],8,[WHL BOM PART 3 PN])
                                  , ([bomRev],9,[WHL BOM PART 4 PN])
                                  , ([bomRev],10,[WHL BOM PART 5 PN])
                                  , ([bomRev],11,[COLOR-PN]) ) u (rev, ordinal, partId )
        WHERE   NULLIF(u.partId,'') is NOT NULL 
          AND   NOT EXISTS ( SELECT  * 
                             FROM    WITESTCO.dbo.[WIBOMD] 
                             WHERE   WITESTCO.dbo.[WIBOMD].[bomItem] = inserted.[STOCK NO] 
                               AND   WITESTCO.dbo.[WIBOMD].[bomRev]  = inserted.[RevControl] ) 
        ) AS source ( [STOCK NO], rev, bomEntry, Partid, Qty, cmnt, srcLoc, dType, lead, lineNbr )
    ON  ( target.bomItem  = source.[STOCK NO] 
          target.bomRev   = source.Rev 
          target.bomEntry = source.bomEntry )  
WHEN MATCHED THEN   
    UPDATE  [bomItem]  =  [STOCK NO] 
          , [bomRev]   =  u.rev
          , [bomEntry] =  bomEntry  
          , [partId]   =  Partid
          , [qty]      =  Qty       
          , [cmnt]     =  cmnt      
          , [srcLoc]   =  srcLoc    
          , [dType]    =  dType     
          , [lead]     =  lead      
          , [lineNbr]  =  lineNbr   
WHEN NOT MATCHED THEN  
    INSERT ([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt], [srcLoc], [dType], [lead], [lineNbr])  
    VALUES ([STOCK NO], rev,     bomEntry,   Partid,   Qty,   cmnt,   srcLoc,   dType,   lead,   lineNbr  )

第一个功能是将学生添加到学生列表中。 它添加了学生但是如果我试图放另一个学生,名称正在替换第一个名称(只有名称而不是数字)。

我无法在代码中发现错误,非常感谢您的帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

为名称分配内存并从name参数中复制它:

list * add_student(slist * students, char * name, int id) {
  slist * temp, * adder;
  if (!(adder = (slist * ) malloc(sizeof(slist)))) exit(1);
  student * data;
  if (!(data = (student * ) malloc(sizeof(student)))) exit(1);
  if (!(data->name = (char * ) malloc(sizeof(char) * (strlen(name)+1)))) exit(1);
  strcpy(data->name, name);
  data->id = id;
  data->courses = NULL;
  adder->info = data;
  adder->next = NULL;
  if (!students) {
    return adder;
  }
  for (temp = students; temp - > next; temp = temp - > next);
  temp - > next = adder;
  return students;
}