Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by elvis on 19.1.14.
- //
- #include "include/list_public.h"
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int data;
- struct node * next;
- }node;
- node * createLinkedList(int n);
- void displayList(node * head);
- void freeList(struct node* head);
- int main( ) {
- struct linked_list list;
- int n = 0;
- node * HEAD = NULL;
- printf("\nHow many nodes:");
- scanf("%d",&n);
- HEAD = createLinkedList(n);
- displayList(HEAD);
- //char *pStr = (char*) malloc(512); //memory leaks fix? nope this is another memory leak wtf?
- freeList(HEAD);
- return 0;
- }
- node * createLinkedList(int n)
- {
- //int i = 0;
- node * head = NULL;
- node * temp = NULL;
- node * current = head;//node * current = NULL;
- for (int i = 0; i < n; i++) {
- temp = (node*)malloc(sizeof(node));
- printf("\nEnter the data for node number: %d", i + 1); //printf("\nEnter the data for node number: ", i + 1);
- scanf("%d",&temp->data);//number you store in nodes &(temp->data)
- temp->next = NULL;
- if (head == NULL)
- {
- head = temp;//no **
- current = head->next;
- temp = NULL;
- printf("\n IF loop");
- }
- else
- {
- current = temp;
- current = current->next;
- temp = NULL;
- }
- printf("\n ending loop");
- free(temp);
- }
- return head;
- }
- void displayList(node * head)
- {
- node * current = head;
- while(current !=NULL)
- {
- printf("\t%d->",current->data);
- current = current->next;
- }
- }
- void freeList(struct node* head)
- {
- struct node* tmp;
- while (head != NULL)
- {
- tmp = head;
- head = head->next;
- free(tmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement