0

Here is the problem.

The input is 4 kinds of command,

"ADD S C" which means add student S into course C.

"DROP S C" means drop student S into course C.

"PRINTS S" mean print out all the course that student S taken.

"PRINT C" mean print out all the student taken course C.

The input will stop until its reach end of file(EOF).

So, I decided to use a linked list to do this.

first, I define a struct

typedef struct node
{
  int SID;
  int CID;
  struct node* next;
}node;

Then, i make a create_node function.

node* create_node(int IDS, int IDC)
{
  node* copy = (node*)malloc(sizeof(node));
  copy->SID = IDS;
  copy->CID = IDC;
  copy->next = NULL;

return copy;
}

And, i also make a insert node function.

void insert_node(node* a, node* b)
{
  a->next = b;
  b->next = NULL;
}

The problem come out. Because the input would only stop when its reach End Of File. That means there may be "ADD 1 2", "ADD 2, 3" ....... appear so many times. I want to know how to link up two node, because in usually, I will

node* a = create_node(2, 3);
node* b = create_node(7, 7);
insert_node(a, b);

but now, I cannot do this. Can anyone give me an example? Thanks a lot.

zodiac
  • 75
  • 7
  • Maybe you haven't seen this, but [please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Feb 19 '16 at 14:25
  • ...why? lecture note teach me to do this... – zodiac Feb 19 '16 at 14:27
  • That blue text is a link. The reasoning is in the answer that the link is pointing to. Maybe something to hand to your teacher, too. :) – unwind Feb 19 '16 at 14:27
  • When you link `a` to `b`, you don't set `b -> next = NULL;`. Instead, you let `b -> next` link to other nodes, which is the meaning of a "linked list". – WhatsUp Feb 19 '16 at 14:31
  • okay. I delete it. Do you know how to linked to nodes when "ADD" happens more than 2 times? It drive me crazy. – zodiac Feb 19 '16 at 14:32
  • @WhatsUp But b is the end of the nodes, so I link it to NULL. If I don't link it to NULL, then what should I link? – zodiac Feb 19 '16 at 14:33
  • I have added an example in the answer. Hope it helps. – WhatsUp Feb 19 '16 at 14:38

1 Answers1

0

Here is an example of how a "linked list" works.

void insert_node(node * a, node * * head)
{
    a -> next = * head;
    * head = a;
}

void main()
{
    node * list = NULL;        // an empty list
    node a, b, c, d;           // four nodes
    insert_node(& a, & list);  // list = a --> NULL
    insert_node(& b, & list);  // list = b --> a --> NULL
    insert_node(& c, & list);  // list = c --> b --> a --> NULL
    insert_node(& d, & list);  // list = d --> c --> b --> a --> NULL
                               // etc.
}

Here, the function insert_node adds a node to the head of the list.

Usually, if you want to make things more tidy, you can typedef some of these, e.g.:

typedef (node *) pnode
typedef (pnode *) linkedlist
WhatsUp
  • 1,618
  • 11
  • 21
  • Thank you. I think it gonna to be work. I am going to try it. – zodiac Feb 19 '16 at 14:44
  • You are welcome. It's always a bit confusing in the beginning, but once you understand the principle, you may find many different ways to do it. – WhatsUp Feb 19 '16 at 14:47