#include <stdio.h>
#include <stdlib.h>
struct tag
{
int data;
struct tag *link;
};
typedef struct tag * NODE;
void push_node();
void pop_node();
void display();
int main()
{
int ch, element, dnum;
NODE top = NULL;
do
{
printf("\n1.) PUSH");
printf("\n2.) POP");
printf("\n3.) DISPLAY");
printf("\n4.) EXIT");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter element to push: ");
scanf("%d", &element);
push_node(&top, element);
break;
case 2: pop_node(&top);
break;
case 3: display(top);
break;
case 4: break;
default: printf("\nWrong choice!! Enter choice again.");
}
}while(ch!=4);
}
void push_node(NODE *top, int ele)
{
NODE newnode, temp;
temp = *top;
newnode = (NODE)malloc(sizeof(struct tag));
if(newnode == NULL)
{
printf("\nInsufficient memory");
return;
}
newnode->data = ele;
if(*top == NULL)
{
newnode->link = NULL;
*top = newnode;
}
else
{
newnode->link = *top;
*top = newnode;
}
}
void pop_node(NODE *top)
{
NODE c;
if(*top == NULL)
{
printf("\nStack is empty");
return;
}
c=*top;
if(c->link == NULL)
{
printf("\nPopped node: %d", c->data);
free(c);
*top = NULL;
return;
}
*top = c->link;
printf("\nPopped node: %d", c->data);
free(c);
}
void display(NODE top)
{
NODE temp;
temp = top;
if(top == NULL)
{
printf("\nStack is empty");
return;
}
printf("\nStack is: \n");
while(temp != NULL)
{
printf("\n%d", temp->data);
temp = temp->link;
}
}
Hello everybody!! Here on this blog I'll be posting C as well as C++ programs. Programs will be easier to understand as they will be implemented in a easier method and I'll b providing the necessary comments wherever required in short. Happy Programming!!!

Custom Search
Showing posts with label singly linked list. Show all posts
Showing posts with label singly linked list. Show all posts
Friday, November 4, 2011
Program to implement dynamically allocated Stack using linked list.
Labels:
dynamic memory allocation,
free.,
malloc,
singly linked list,
stack
Saturday, October 15, 2011
Singly linked list (in C++) to insert and delete node at front.
//Singly Linked list in C++...
//Inserting and deleting a node from front.
#include <iostream>
#include <cstdlib>
using namespace std;
class NODE
{
public:
int data;
NODE* link;
};
class LIST
{
NODE* first;
public:
LIST();
~LIST();
void ins_front(int);
void del_front();
void display();
};
LIST::LIST() //Constructor
{
first = NULL;
}
LIST::~LIST() //Destructor
{
cout<<"\nDestroyed\n";
}
//Function to insert node at front
void LIST::ins_front(int inum)
{
NODE* newnode;
newnode = (NODE*)malloc(sizeof(NODE)); //Allocating memory to newnode
newnode->data= inum;
(newnode->link) = first;
first = newnode;
}
//Function to delete front node
void LIST::del_front()
{
NODE* temp;
if(first == NULL)
{
cout<<"\nSingly Linked list is empty";
return;
}
if(first->link == NULL) //to check if only one node is present
{
temp = first;
first = NULL;
cout<<"\nDeleted node: "<<temp->data;
free(temp);
}
else //If more than one nodes are present
{
temp = first;
first = first->link;
cout<<"\nDeleted node: "<<temp->data;
free(temp);
}
}
void LIST::display()
{
NODE* temp;
if(first == NULL)
{
cout<<"\nSingly Linked list is empty";
return;
}
temp = first;
cout<<"\nSingly linked list is:\n";
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->link;
}
}
int main()
{
LIST obj; //Creating object of the class LIST
int choice, inum;
do
{
cout<<"\n\nSingly linked list operations: "; //Menu for operations on linked list
cout<<"\n1.) Insert node at front";
cout<<"\n2.) Delete front node";
cout<<"\n3.) Display";
cout<<"\n4.) Exit";
cout<<"\n\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<"\nEnter element to insert at the front: ";
cin>>inum;
obj.ins_front(inum);
break;
case 2: obj.del_front();
break;
case 3: obj.display();
break;
case 4: break;
default: cout<<"\nInvalid choice!! Enter choice again.";
}
}while(choice != 4);
}
Subscribe to:
Posts (Atom)

Custom Search