0% found this document useful (0 votes)
51 views

Practical Stack Queue Linkedlist

The document contains source code for C programs that implement a stack, queue, linked list, and search an element in a linked list. The stack program uses functions like push(), pop(), and display() to add, remove, and view elements. The queue uses functions like insert(), delete(), display() to add, remove and view elements. The linked list program uses malloc to dynamically allocate nodes and stores integer data. The search program traverses the linked list to check if an input key is present and returns the result.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Practical Stack Queue Linkedlist

The document contains source code for C programs that implement a stack, queue, linked list, and search an element in a linked list. The stack program uses functions like push(), pop(), and display() to add, remove, and view elements. The queue uses functions like insert(), delete(), display() to add, remove and view elements. The linked list program uses malloc to dynamically allocate nodes and stores integer data. The search program traverses the linked list to check if an input key is present and returns the result.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <stdio.

h>
#define MAXSIZE 5

struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;

void push(void);
int pop(void);
void display(void);

void main ()
{
int choice;
int option = 1;
s.top = -1;

printf ("STACK OPERATION\n");


while (option)
{
printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");

printf ("Enter your choice\n");


scanf ("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}
}
/* Function to add an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* Function to delete an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}

Queue

#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */

Create Linked List

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

void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;

NODE *head, *first, *temp = 0;


int count = 0;
int choice = 1;
first = 0;

while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);

}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}

A source code of the C program is to search for an element in a linked list. The C
program is successfully compiled and run on a Linux system. The program output is
also shown below.

#include <stdio.h>
#include <stdlib.h>

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

void create(struct node **);


int search(struct node *, int);
void release(struct node **);
void display(struct node *);

int main()
{
struct node *p = NULL;
int key, result;

printf("Enter data into the list\n");


create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
printf("Enter key to search in the list: ");
scanf("%d", &key);
result = search(p, key);
if (result)
{
printf("%d found in the list.\n", key);
}
else
{
printf("%d not found in the list.\n", key);
}
release(&p);

return 0;
}

int search(struct node *head, int key)


{
while (head != NULL)
{
if (head->num == key)
{
return 1;
}
head = head->next;
}

return 0;
}

void create(struct node **head)


{
int c, ch;
struct node *temp, *rear;

do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}

void display(struct node *p)


{
while (p != NULL)
{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}

void release(struct node **head)


{
struct node *temp = *head;
*head = (*head)->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}

You might also like