DataStructure Linked List
Zhe Jiang University
Design
Target: Find the sum and the product of 2 polynomials.
Data Structure
1 | typedef struct PolyNode *Polynomial; |
Structure of the program
1 | Polynomial P1, P2, PP, PS; |
Some New Ideas
-
If you want to pass in a variable and let the function change it, you have to pass in a more ‘fundamental’ thing - that is - a pointer to this variable. If passing the value itself, there will be a ‘shell’ to preent it from being changed.
-
(linked list) Getting the node just before the target node can be sometimes more flexible. It is easier to delete a node.
1
2
3t = Rear->link;
Rear->link = t->link;
free(t); -
Creating a linked list: in writing a connecting function, in order to avoid if-clauses to find whether it is the first node of a list, you can just create am empty node at the head of a linked list and in the end delete it.
1
2
3
4
5
6P->link = NULL;
Rear = P;
while(N--){
scanf("%d %d", &c, &e);
Attach(c, e, &Rear);
} -
The usage of scanf is easier than expected. The computer will stop getting input every time it meets a space or an enter. The way to get input can simply be like the example below.
1
2
3
4
5
6
7
8scanf("%d", &N);
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while(N--){
scanf("%d %d", &c, &e);
Attach(c, e, &Rear);
}REMARK: The pause enter or space will be dumped by the computer instead of being inputed.