Computer Concept & Programming In C - Unit IV - 16

Q.29 What are differences in Stack and List? Explain how an element can be deleted from a Stack and List.                                                                 (AKTU. 2009-10)
Ans. A stack is a LIFO i.e Last In- First out data structure and its basic operations are PUSH and POP. Only an element can be inserted at the Top Of Stack (TOS) also called PUSHED onto the stack. Only an element can be removed from the Top Of Stack (TOS) also called POPED from the stack. The algorithm to delete an element from the list as well as all other operations on the list can be understood from the following program to implement the Linked List. The program to implement stack is given above.
/* Program to create singly linked list.*/
/* We make use of parameter passing*/

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

/*structure of single linked list*/
struct node{
  int info;
  struct node *next;
};
/*head of a list initialised to NULL*/
struct node *head = NULL;

/* To allocate memory for a node*/
struct node* create_node(void);

/* loop control*/
int flag = 0;

/*Elementry linked list functions are :*/
/* 1)Insertion at the begining */
 void insert_at_begining(struct node**, int);

/* 2)Insertion after i-th node*/
 void insert_after_i_node(struct node**, int, int);
/* 3)Insertion of a node at the last*/
 void insert_at_last(struct node**, int);

/* 4)Insertion after the specified node*/
 void insert_after(struct node**, int, int);

/* 5)Deletion after i-th node  */
 int delete_after_i_node(struct node**, int);

/* 6)Deletion of specied node  */
 void delete_node(struct node**, int);

/* 7)Deletion of the start node*/
 int delete_at_start(struct node**);

/* 8)Deletion of the last node */
 int delete_at_last(struct node**);

/* 9)Search a node and return its position*/
 int search_node(struct node**, int);

/* 10)Traverse the list*/
 void traverse_list(struct node**);

/* 11)Main - Menu to access all the above functions*/
void main_menu(void);

void main()
{
 clrscr();
 printf("\n\t\tIMPLEMENTATION OF SINGLY LINKED LIST ");
 printf("\n\t\tCreated by ABHINAV SHALABH KULSHRESTHA");
 printf("\n\n");
 while(1)
 {
   main_menu();
   if(flag == 1)
     break;
  }
}

void main_menu(void)
{
  int ch;
  int position, value, value1, return_value;
  char c;
  printf("\nMAIN-MENU");
  printf("\nPress 0. To insert node at beginning.\n");
  printf("\nPress 1. To insert node after i-th position.\n");
  printf("\nPress 2. To insert node at last position.\n");
  printf("\nPress 3. To insert node after specified node.\n");
  printf("\nPress 4. To delete node after i-th position.\n");
  printf("\nPress 5. To delete node specified.\n");
  printf("\nPress 6. To delete node at the beginning.\n");
  printf("\nPress 7. To delete node at the last.\n");
  printf("\nPress 8. To search a node.\n");
  printf("\nPress 9. To traverse the list.\n");
  printf("\nPress any other key to exit.\n");
  printf("\nEnter your choice.\n");
  c = NULL;
  c = getche();
  ch = c - '0';
  switch(ch)
  {
    case 0: value = NULL;
   printf("\nInsert at beginning");
   printf("\nEnter the number to be inserted at the beginning");
   scanf("%d", &value);
   insert_at_begining(&head, value);
   traverse_list(&head);
   break;

    case 1: position = NULL;
   value = NULL;