Question

In: Computer Science

How to write code for stack with singly linked list using C? Please show examples for...

How to write code for stack with singly linked list using C?

Please show examples for create, free, isempty, push, top, pop functions.

Solutions

Expert Solution

#Program

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*top=NULL;
void display()
{
struct node *p;
if(top==NULL)
printf("stack is empty\n");
else{
p=top;
while(p!=NULL)
{
printf("%d-->",p->data);
p=p->next;
}
printf("NULL\n");
}
}
void create()
{
struct node *temp;
int n,i;
printf("Enter number of values to create: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter value: ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->data);
temp->next=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->next=top;
top=temp;
}
}
display();
}
void push()
{
struct node *temp;
int x;
printf("enter the data: ");
scanf("%d",&x);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x,temp->next=NULL;
if(top==NULL)
top=temp;
else
{
temp->next=top;
top=temp;
}
display();
}
void pop()
{
struct node *temp;
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
top=top->next;
temp->next=NULL;
}
display();
}
void Free()
{
struct node *temp;
if(top==NULL)
printf("stack is empty\n");
else
{
temp=top;
top=top->next;
temp->next=NULL;
free(temp);
}
display();
}
void peek()
{
printf("Top = %d\n",top->data);
}
void isempty()
{
if(top==NULL)
printf("Stack is empty\n");
else
printf("Stack is not empty\n");
}
main()
{
int opt;
while(1){
printf("\n-----------\n");
printf("1.create()\n2.push()\n3.pop()\n4.peek()\n5.isempty()\n6.free()\n7.exit\n");
printf("-----------\n");
printf("Enter your option: ");
scanf("%d",&opt);
switch(opt)
{
case 1: create(); break;
case 2: push(); break;
case 3: pop(); break;
case 4: peek(); break;
case 5: isempty(); break;
case 6: Free(); break;
case 7: exit(0);
default: printf("wrong choice\n");
}
}
}

#if you have any queries please comment


Related Solutions

C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked...
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked list is palindrome or not. Input: a -> b-> NULL a -> b -> a-> NULL s -> a -> g -> a -> r-> NULL r -> a -> d -> a -> r-> NULL Output: not palindrome palindrome not palindrome palindrome Note:- Code should work on MS-Visual Studio 2017 and provide outputs with code
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT