Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update stack_using_linked_lists.c #620

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
28 changes: 19 additions & 9 deletions data_structures/linked_list/stack_using_linked_lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,51 @@ int main()
}
}

void push(struct node *p)
/**
* push function will add a new node at the head of the list, time complexity O(1).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parameter documentation.
Same in other parts of the code.

* @param p pointer to the top of the stack.
* @returns void.
*/
void push(struct node *p)
{
int item;
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp = (struct node *)malloc(sizeof(struct node));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty whitespace.
Same in other parts of the code.

Suggested change
temp = (struct node *)malloc(sizeof(struct node));
temp = (struct node *)malloc(sizeof(struct node));

printf("enter element to be inserted\n");
scanf("%d", &item);
temp->info = item;

temp->link = top;
top = temp;
top = temp;
Shwetik marked this conversation as resolved.
Show resolved Hide resolved

printf("inserted succesfully\n");
}
void pop(struct node *p)

/**
* pop function deletes the first node from the head, time complexity O(1).
* @param p pointer to the top of the stack.
* @returns void.
*/
void pop(struct node *p)
{
int item;
struct node *temp;

if (top == NULL)
if (top == NULL)
printf("stack is empty\n");
else
{
item = top->info;
temp = top;
top = top->link;
free(temp);
top = top->link;
free(temp);
Shwetik marked this conversation as resolved.
Show resolved Hide resolved
printf("Element popped is%d\n", item);
}
}

void display(struct node *p)
{
if (top == NULL)
if (top == NULL)
Shwetik marked this conversation as resolved.
Show resolved Hide resolved
printf("stack is empty\n");
else
{
Expand All @@ -79,6 +90,5 @@ void display(struct node *p)
printf("%d\n", p->info);
p = p->link;
}
// printf("%d\n",p->info);
}
}