Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node_t{
- int x;
- struct node_t *next;
- } node_t;
- node_t *list_create(int x,node_t *next) {
- node_t *new_node = (node_t *)malloc(sizeof(node_t));
- if (new_node == NULL) {
- return (node_t*)1;
- }
- new_node->x=x;
- new_node->next=next;
- return new_node;
- }
- void list_append(node_t *head,int x) {
- node_t *cursor = head;
- while (cursor->next != NULL) {
- cursor=cursor->next;
- }
- cursor->next=list_create(x,NULL);
- return;
- }
- void list_destroy(node_t *head) {
- node_t *cursor = head->next, *temp = NULL;
- free(head);
- if (cursor != NULL) {
- while (cursor->next != NULL) {
- temp=cursor->next;
- free(cursor);
- cursor=temp;
- }
- free(cursor);
- }
- return;
- }
- int main() {
- node_t *head = list_create(0,NULL), *cursor = NULL;
- for (int i=1;i<32;i++) {
- list_append(head,i);
- }
- cursor=head;
- if (cursor->next == NULL) printf("%d",cursor->x);
- while (cursor->next != NULL) {
- printf("%d\n",cursor->x);
- cursor=cursor->next;
- }
- printf("%d\n",cursor->x);
- list_destroy(head);
- }
Add Comment
Please, Sign In to add comment