Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Assignment9text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1.)Malloc takes one argument and calloc takes two. Malloc takes the size in bytes of the data type that we need. Calloc takes that and how many terms there will be in our ilnked list.

2.) Take how many elements there are and multiply it by the size in bytes of that data type.

3.) Stack data is automatically cleared at the end of your program. This is not the case for heap and if you don't free the memory, you could have memory leaks.

4.)To know if the memory is allocated correctly
72 changes: 72 additions & 0 deletions linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <stdlib.h>
#include <stdio.h>

struct node {
int x;
struct node *next;
};

int printlist(struct node *root)
{
struct node *pt;

pt = root;

printf("%d\n", pt->x);

while (pt->next!=0) {

pt = pt->next;

printf("%d\n", pt->x);

}

}

/* void add(struct node *pointer, int data)
{
while (pointer->next!=NULL){

pointer = pointer->next;
}

pointer->next = (struct node *)malloc(sizeof(struct node));

pointer = pointer->next;

pointer->x = data

}

*/
int main()
{
struct node *root, *node1, *node2, *node3, *node4, *node5;

root = (struct node *) malloc(sizeof(struct node));
node1 = (struct node *) malloc(sizeof(struct node));
node2 = (struct node *) malloc(sizeof(struct node));
node3 = (struct node *) malloc(sizeof(struct node));
node4 = (struct node *) malloc(sizeof(struct node));
node5 = (struct node *) malloc(sizeof(struct node));

root->next = node1;
root->x = 5;

node1->next = node2;
node1->x = 7;

node2->next = node3;
node2->x = 11;

node3->next = node4;
node3->x = 2;

node4->next = node5;
node4->x = 17;

node5->next = 0;
node5->x = 9;

}