Thursday 16 March 2017

C program to implement stack using linked list

The linked list data structure is the best way to implement a variable-sized stack. The stack can grow in size till there is enough space in the memory to hold the data. The dynamic stack implementation is given in the following C program.

//Stack using Linked list
#include<stdio.h>
#include<conio.h>
struct node
{
struct node *next;
int data;
}*head,*temp;

void push()
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter the data\t");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=newnode;
head->next=NULL;
}
else
{
newnode->next=head;
head=newnode;
  }
}
void pop()
{
if(head==NULL)
{
printf("Empty stack\n");
}
else
{
printf("The popped element is ");
temp=head;
head=head->next;
printf("%d",temp->data);
free(temp);
}
}
void display()
{
if(head==NULL)
{
printf("Empty stack\n");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
}
main()
{
int ch;
void push();
void pop();
void display();

clrscr();
  do
  {
  printf("\n1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("Enter the choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:exit(0);
}
}while(1);
}


/*
OUTPUT:
________
1.Push
2.Pop
3.Display
Enter the choice        1
Enter the data  1

1.Push
2.Pop
3.Display
Enter the choice        1
Enter the data  2

1.Push
2.Pop
3.Display
Enter the choice        3
2->1->

1.Push
2.Pop
3.Display
Enter the choice        2
The popped element is 2

1.Push
2.Pop
3.Display
Enter the choice        4

*/

Sunday 12 March 2017

C program to implement stack using arrays

Stack is a data structure to which the elements are inserted and deleted in the Last In First Out (LIFO). Stack has many applications like arithmetic expression evaluation, subroutine handling, interrupt handling, recursion call implementation etc.. Stack can be implemented statically using arrays or a dynamic stack implementation using Linked list. The following program shows how to implement stack operations using array.

//Program to implement stack using array
#include<stdio.h>
#include<conio.h>
#define size 5

int stack[size],top=-1;

main()
{
 int ch;
 int a;
 void push(int,int *);
 int pop(int *);
 void display();
 clrscr();

 do
 {
  printf("\n1.push\t");
  printf("2.pop\t");
  printf("3.display\t");
  printf("Enter the choice\t");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1: printf("\nEnter the number to be pushed: ");
  scanf("%d",&a);
  push(a,stack);
  break;
   case 2: pop(stack);
  break;
   case 3: display();
  break;
   default: exit(0);
   }
  }while(ch<4);
 getch();
}
void push(int a,int *stack)
{
 if(top>=size)
  {
   printf("Stack overflow\n");
  }
  else
  {
   top++;
   stack[top]=a;
  }
}
int pop(int *stack)
 {
  int temp;
  if(top==-1)
  {
   printf("\nStack Empty\n");
  }
  else
  {
   printf("The popped element is\t");
   {
    temp=stack[top];
    top--;
    printf("%d\n",temp);
    return(temp);
   }
  }
 }
 void display()
 {
  int i;
  if(top==-1)
  {
   printf("Stack underflow\n");
  }
  else
  {
   printf("\nThe contents of the stack are \t");
   for(i=0;i<=top;i++)
   {
    printf("%d\t",stack[i]);
   }
   printf("\n");
  }
 }

/*
OUTPUT
-------

Enter the number to be pushed: 1

1.push  2.pop   3.display       Enter the choice        1

Enter the number to be pushed: 2

1.push  2.pop   3.display       Enter the choice        1

Enter the number to be pushed: 3

1.push  2.pop   3.display       Enter the choice        1

Enter the number to be pushed: 4

1.push  2.pop   3.display       Enter the choice        1

Enter the number to be pushed: 5

1.push  2.pop   3.display       Enter the choice        3

The contents of the stack are   1       2       3       4       5

1.push  2.pop   3.display       Enter the choice        1

Enter the number to be pushed: 6

1.push  2.pop   3.display       Enter the choice        1

Enter the number to be pushed: 7
Stack overflow

1.push  2.pop   3.display       Enter the choice        3

The contents of the stack are   1       2       3       4       5       6


1.push  2.pop   3.display       Enter the choice        2
The popped element is   6

1.push  2.pop   3.display       Enter the choice        2
The popped element is   5

1.push  2.pop   3.display       Enter the choice        2
The popped element is   4

1.push  2.pop   3.display       Enter the choice        4
*/

Friday 10 March 2017

C program for creating text file

Files are created and manipulated to store permanent data needed for a program. In C language, text files can be created and processed using file I/O functions. In the following program, we see how to create a new text file and open the created file and display the contents of the file on the screen.

//Program to create a text file
#include<stdio.h>
#include<conio.h>
main()
{
  FILE *f1;
  char c;
  clrscr();
  f1=fopen("sample.txt","w");
  printf("Enter text. Press ctrl+z to stop input\n");
  printf("----------------------------------------------\n");
 
  while ((c=getchar())!=EOF)
  fputc(c,f1);
  fclose(f1);

  printf("The content of the file\n");
  printf("--------------------------\n");
  f1=fopen("sample.txt","r");
  while((c=getc(f1))!=EOF)
  putchar(c);
  fclose(f1);
  getch();
}


/*
OUTPUT
-------
Enter text. Press ctrl+z to stop input
---------------------------------------------
This program creates a text file.                                              
The input is read from the keyboard.                                           
To stop getting input press ctrl+z.                                            
                                                                              
The content of the file                                                           
--------------------------                                                     
This program creates a text file.                                              
The input is read from the keyboard.                                           
To stop getting input press ctrl+z.
*/