Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #107245

Re: singly linked list -- HCL interview question

From Ike Naar <ike@iceland.freeshell.org>
Newsgroups comp.lang.c
Subject Re: singly linked list -- HCL interview question
Date 2017-03-28 06:51 +0000
Organization A noiseless patient Spider
Message-ID <slrn3vfsodk247.98n.ike@iceland.freeshell.org> (permalink)
References <58d944cc$0$301$14726298@news.sunsite.dk> <aliidclgfna7s21craso25v0r08dvampto@4ax.com>

Show all headers | View raw


On 2017-03-27, Barry Schwarz <schwarzb@dqel.com> wrote:
> On 27 Mar 2017 16:58:52 GMT, arnuld <sunrise@invalid.address> wrote:
>
>> [...]
>>
>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <errno.h>
>>#include <limits.h>
>>
>>struct node {
>>  long nn;
>>  struct node* next;
>>};
>>
>>struct StPtr {
>>  struct node* head;
>>  long tn; /* totoal number of nodes */
>>};
>>
>>
>>struct StPtr* st;
>>
>>void print_diagnostics(void);
>>void printStack(void);
>>void convert_to_long(long*, long*, const char*, const char*);
>>void addNodes(long, long);
>>void removeNode(long, long);
>>struct StPtr* createStack(void);
>>
>>int main(int argc, char* argv[]) {
>>  if(3 != argc) {
>>    print_diagnostics();
>>    return EXIT_FAILURE;
>>  }
>>  else {
>
> This else serves no purpose.  When the previous if expression is true,
> this code cannot be reached due to the return statement.  When the
> expression is false, you want this code to execute unconditionally.
>
>>    st = createStack();
>
> createStack does nothing but call malloc.  Why go through two
> functions when you can call malloc here and eliminate createStack
> completely?

It might be a better idea to keep createStack() and let it not only
allocate the memory, but also initialize the members,
setting 'head' to NULL and 'tn' to 0.
('head' is now initialized in main(), 'tn' is never initialized,
leading to undefined behaviour in the functions that increment or
decrement it).

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

singly linked list -- HCL interview question arnuld <sunrise@invalid.address> - 2017-03-27 16:58 +0000
  Re: singly linked list -- HCL interview question Barry Schwarz <schwarzb@dqel.com> - 2017-03-27 11:08 -0700
    Re: singly linked list -- HCL interview question Chad <cdalten@gmail.com> - 2017-03-27 12:02 -0700
    Re: singly linked list -- HCL interview question arnuld <sunrise@invalid.address> - 2017-03-28 03:18 +0000
      Re: singly linked list -- HCL interview question Barry Schwarz <schwarzb@dqel.com> - 2017-03-27 22:59 -0700
        Re: singly linked list -- HCL interview question arnuld <sunrise@invalid.address> - 2017-03-29 04:28 +0000
      Re: singly linked list -- HCL interview question scott@slp53.sl.home (Scott Lurndal) - 2017-03-28 12:56 +0000
    Re: singly linked list -- HCL interview question Ike Naar <ike@iceland.freeshell.org> - 2017-03-28 06:51 +0000
      Re: singly linked list -- HCL interview question arnuld <sunrise@invalid.address> - 2017-03-29 04:31 +0000
        Re: singly linked list -- HCL interview question Keith Thompson <kst-u@mib.org> - 2017-03-29 08:46 -0700

csiph-web