Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node{
- int data;
- struct node * next;
- } node_t;
- typedef struct linkedlist{
- int size;
- struct node * list;
- } linkedlist_t;
- //& means as a part of the parameter indicates pass by reference
- void initiliaze(linkedlist_t* list){
- list->size = 0; //No elements in the list
- list->list = NULL; //list is empty
- }
- void addnode(linkedlist_t* currentlist, int newdata){
- //Create a pointer to a new nde and allocate space on the heap for the new node
- //Java: node new = new node();
- node_t* newnode = (node_t*)malloc(sizeof(node_t));
- newnode->next = currentlist->list;
- newnode->data = newdata;
- currentlist->list = newnode;
- currentlist-> size++;
- }
- void print(linkedlist_t* currentlist){
- node_t* temp = currentlist -> list;
- while(temp != NULL){
- printf("%d ", temp -> data);
- temp = temp-> next;
- }
- }
- int main(void){
- linkedlist_t mylist;
- initiliaze(&mylist); //this will hand myList into the initialize function instead of creating a copy
- addnode(&mylist, 1);
- addnode(&mylist, 55);
- addnode(&mylist, 42);
- print(&mylist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement