Author Topic: Simple C Program  (Read 2343 times)

bbasujon

  • Administrator
  • VIP Member
  • *****
  • Posts: 1827
  • I want to show my performance at any where
    • View Profile
    • Higher Education
Simple C Program
« on: January 28, 2012, 08:58:40 AM »
This is a simple C program for Stack datastructure using Linked List and user defined structure data type. The function performs “Push” and “Pop” operations in the Stack (LIFO , FILO) in which is defined in the form of a recursive structure. Both Push and Pop operation are performed in a constant time and the size complexity is of Order N , where N is the number of elements in the stack.
Download Stack Program in C using Linked List

The File for Linked List Stack Program can be downloaded from

Program Description

The values are stored in the Linked list as individual items that is capable to hold the next item. So we declare a structure called Node, that contains the value and the address to the next Node. The next Node will be NULL if the node is not connected to the next node.

In the main function , one Node called Root is declared, since it doesn’t contain any element, it should point to NULL. So we call another function called Init and pass the address of the root. This will assign the value of next in root to NULL.

The stack contains two more basic operation called the Push and Pop, that are used to add and remove the values respectively. In Push Operation, we specify the Root node and the value to be added into the stack. Since the stack is implemented in a linked list, we won’t end up in Out of Memory exception, so we don’t need to check for boundary conditions in Push. We create a new node called j and set the next value of j to the present next of Root and make j as root -> next.

root Simple C Program for Stack using Linked List

push Simple C Program for Stack using Linked List

The pop operation is yet again simple,although we check for a condition that if the next node of root is NULL, that means there are no elements in the stack, so we return error saying no element to pop from the stack. We create a temp node only for the reason , that the memory must be made free after pop. We can also do Pop without a temp variable, but we cannot free that memory.
Stack Program using Linked List

#include<stdio.h>
#include<malloc.h>

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

void Init(struct node *root){
root->next=NULL;
}

void Push(struct node *root,int value){
struct node *j =(struct node*)malloc(sizeof(struct node));
j->next=root->next;
j->value=value;
root->next=j;
printf(“Value Pushed is %dn”,value);

}

void Pop(struct node *root){
if(root->next == NULL){
printf(“No Element to Popn”);
}
else {
struct node *temp;
temp=root->next;
root->next=temp->next;
printf(“Value Popped is %dn”,temp->value);
free(temp);
}
}

void main(){
struct node stack;
Init(&stack);
Push(&stack,10);
Push(&stack,60);
Pop(&stack);
Pop(&stack);
}
Acquire the knowledge and share the knowledge so that knowing,learning then sharing - all are the collection