- Program to Find the Number of Elements in an Array.
#include <stdio.h> int main(){ int array[] = {4, 78, 23, 92, 106, 81, 1, 55, 99, 0}; int length; length = sizeof(array) / sizeof(array[0]); printf("Size of the given array is: %d\n", length); getchar(); return 0; } /* Output: Size of the given array is: 10 */
- Develop and Implement a menu driven program in C for the following Array operations.
- Creating Array of N Integer elements.
- Display of Array elements with suitable headings.
- Find the total Number of Elements in an Array.
- Inserting an element (ELEM) at a given valid position (POS).
- Deleting an element at a given valid position (POS).
- Search an Element in Given array.
- Exit
#include <stdio.h> #include <stdlib.h> int a[10], pos, elem; int n = 0; void create(); void display(); void siz(); void insert(); void del(); void srch(); void main() { int choice; while (1) { printf("\n************MENU************"); printf("\n1. Create an Array of N Integers"); printf("\n2. Display of Array Elements"); printf("\n3. Get the Total Number of Elements of an array"); printf("\n4. Insert ELEM at a Given POS"); printf("\n5. Delete an Element at a Given POS"); printf("\n6. Search an Element"); printf("\n7. EXIT"); printf("\nEnter Your Choice: "); scanf("%d", & choice); switch (choice) { case 1: create(); break; case 2: display(); break; case 3: siz(); break; case 4: insert(); break; case 5: del(); break; case 6: srch(); break; case 7: exit(1); break; default: printf("\n****Please Enter a Valid Choice****\n\n"); } } } void create() { int i; printf("\nEnter the Size of Array: "); scanf("%d", & n); printf("\nEnter the Elements: "); for (i = 0; i < n; i++) { scanf("%d", & a[i]); } } void siz() { int i, len; if (n == 0) { printf("\n****Array has No Elements(NO SIZE)****\n"); return; } for (i = 0; i < sizeof(n); i++) { len = n; } printf("\n\t The Size of Array is: %d\n", len); } void display() { int i; if (n == 0) { printf("\n****No Elements to Display****\n"); return; } printf("\nArray Elements Are: "); for (i = 0; i < n; i++) { printf("%d\t ", a[i]); } } void insert() { int i; if (n == 5) { printf("\n****Array is Full. Insertion is Not Possible****\n"); return; } do { printf("\nEnter a Valid Position where Element to be Inserted: "); scanf("%d", & pos); } while (pos > n); printf("\nEnter the Value to be Inserted: "); scanf("%d", & elem); for (i = n - 1; i >= pos; i--) { a[i + 1] = a[i]; } a[pos] = elem; n = n + 1; display(); } void del() { int i; if (n == 0) { printf("\n****Array is Empty and No Elements to Delete****\n"); return; } do { printf("\nEnter a Valid Position from where Element to be Deleted: "); scanf("%d", & pos); } while (pos >= n); elem = a[pos]; printf("\nDeleted Element is : %d \n", elem); for (i = pos; i < n - 1; i++) { a[i] = a[i + 1]; } n = n - 1; display(); } void srch(){ int num=0,i, pos; printf("\nEnter the Integer: "); scanf("%d", &num); pos = 0; for(i=0; i<n; i++) { if(a[i]==num) { pos = 1; break; } } if(pos==1){ printf("\n\tElement \"%d\" Found at Position %d\n", num, i+1); }else{ printf("\n\t****Element Not Found****\n"); } }
- Programs for Stack, Queues and Circular Queues using Arrays.
#include<stdio.h> int stack[100], choice, n, top, x, i; void push(void); void pop(void); void display(void); int main() { top = -1; printf("\n Enter the size of STACK[MAX=100]:"); scanf("%d", & n); printf("\n\t STACK OPERATIONS USING ARRAY"); printf("\n\t--------------------------------"); printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT"); do { printf("\n Enter the Choice:"); scanf("%d", & choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: printf("\n\t EXIT POINT "); break; default: printf("\n\t Please Enter a Valid Choice(1/2/3/4)"); } }while (choice != 4); return 0; } void push() { if (top >= n - 1) { printf("\n\tSTACK is over flow"); } else { printf(" Enter a value to be pushed:"); scanf("%d", & x); top++; stack[top] = x; } } void pop() { if (top <= -1) { printf("\n\t Stack is under flow"); } else { printf("\n\t The popped elements is %d", stack[top]); top--; } } void display() { if (top >= 0) { printf("\n The elements in STACK \n"); for (i = top; i >= 0; i--) printf("\n%d", stack[i]); printf("\n Press Next Choice"); } else { printf("\n The STACK is empty"); } } //QUEUES : /* #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"); } } } 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; } } 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; } } void display() { int i; if (front == -1) printf("Queue is empty \n"); else { printf("Queue is : "); for (i = front; i <= rear; i++) printf("%d ", queue_array[i]); printf("\n"); } } */ // CIRCULAR QUEUE USING ARRAYS : /* #include<stdio.h> #include<stdlib.h> #define MAX 10 int cqueue_arr[MAX]; int front = -1; int rear = -1; void display(); void insert(int item); int del(); int peek(); int isEmpty(); int isFull(); int main() { int choice, item; while (1) { printf("\n1.Insert\n"); printf("2.Delete\n"); printf("3.Peek\n"); printf("4.Display\n"); printf("5.Quit\n"); printf("\nEnter your choice: "); scanf("%d", & choice); switch (choice) { case 1: printf("\nInput the element for insertion: "); scanf("%d", & item); insert(item); break; case 2: printf("\nElement deleted is: %d\n", del()); break; case 3: printf("\nElement at the front is: %d\n", peek()); break; case 4: display(); break; case 5: exit(1); default: printf("\nWrong choice\n"); } } return 0; } void insert(int item) { if (isFull()) { printf("\nQueue Overflow\n"); return; } if (front == -1) front = 0; if (rear == MAX - 1) //rear is at last position of queue rear = 0; else rear = rear + 1; cqueue_arr[rear] = item; } int del() { int item; if (isEmpty()) { printf("\nQueue Underflow\n"); exit(1); } item = cqueue_arr[front]; if (front == rear) { front = -1; rear = -1; } else if (front == MAX - 1) front = 0; else front = front + 1; return item; } int isEmpty() { if (front == -1) return 1; else return 0; } int isFull() { if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) return 1; else return 0; } int peek() { if (isEmpty()) { printf("\nQueue Underflow\n"); exit(1); } return cqueue_arr[front]; } void display() { int i; if (isEmpty()) { printf("\nQueue is empty\n"); return; } printf("\nQueue elements: "); i = front; if (front <= rear) { while (i <= rear) printf("%d ", cqueue_arr[i++]); } else { while (i <= MAX - 1) printf("%d ", cqueue_arr[i++]); i = 0; while (i <= rear) printf("%d ", cqueue_arr[i++]); } printf("\n"); } */
- Program to convert an Infix Expression into Postfix Expression and vice-versa.
#include <stdio.h> #include <ctype.h> char stack[100]; int top = -1; void push(char x) { stack[++top] = x; } char pop() { if (top == -1) return -1; else return stack[top--]; } int priority(char x) { if (x == '(') return 0; if (x == '+' || x == '-') return 1; if (x == '*' || x == '/') return 2; return 0; } int main() { char exp[100]; char * e, x; printf("Enter the expression: "); scanf("%s", exp); printf("\n"); e = exp; while ( * e != '\0') { if (isalnum( * e)) printf("%c ", * e); else if ( * e == '(') push( * e); else if ( * e == ')') { while ((x = pop()) != '(') printf("%c ", x); } else { while (priority(stack[top]) >= priority( * e)) printf("%c ", pop()); push( * e); } e++; } while (top != -1) { printf("%c ", pop()); } return 0; }
- Program to implement stack using arrays.
#include <stdio.h> int stack[100], choice, n, top, x, i; void push(void); void pop(void); void display(void); int main() { //clrscr(); top = -1; printf("\n Enter the size of STACK[MAX=100]: "); scanf("%d", & n); printf("\n\t STACK OPERATIONS USING ARRAY"); printf("\n\t--------------------------------"); printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT"); do { printf("\n Enter the Choice: "); scanf("%d", & choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: printf("\n\t**** EXIT ****"); break; default: printf("\n\t Please Enter a Valid Choice(1/2/3/4)"); } } while (choice != 4); return 0; } void push() { if (top >= n - 1) { printf("\n\tSTACK is over flow"); } else { printf(" Enter a value to be pushed: "); scanf("%d", & x); top++; stack[top] = x; } } void pop() { if (top <= -1) { printf("\n\t Stack is under flow"); } else { printf("\n\t The popped elements is %d", stack[top]); top--; } } void display() { if (top >= 0) { printf("\n The elements in STACK: "); for (i = top; i >= 0; i--) printf("\n%d", stack[i]); printf("\n Press Next Choice"); } else { printf("\n The STACK is empty"); } }
- Program to implement stack using linked list.
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node * next; }* top = NULL; void push(int); void pop(); void display(); int main() { int choice, value; printf("\nIMPLEMENTING STACKS USING LINKED LISTS\n"); while (1) { printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); printf("\nEnter your choice : "); scanf("%d", & choice); switch (choice) { case 1: printf("\nEnter the value to insert: "); scanf("%d", & value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\nInvalid Choice\n"); } } } void push(int value) { struct Node * newNode; newNode = (struct Node * ) malloc(sizeof(struct Node)); newNode -> data = value; if (top == NULL) newNode -> next = NULL; else newNode -> next = top; top = newNode; printf("Node is Inserted\n\n"); } void pop() { if (top == NULL) printf("\nEMPTY STACK\n"); else { struct Node * temp = top; printf("\nPopped Element : %d", temp -> data); printf("\n"); top = temp -> next; free(temp); } } void display() { if (top == NULL) printf("\nEMPTY STACK\n"); else { printf("The stack is: "); struct Node * temp = top; while (temp -> next != NULL) { printf("%d--->", temp -> data); temp = temp -> next; } printf("%d--->NULL\n\n", temp -> data); } }
- Program to implement multiple stack in a single array.
// This Program is to Implement two Stacks using a Single Array. #include <stdio.h> #define SIZE 10 int ar[SIZE]; int top1 = -1; int top2 = SIZE; //Functions to push data void push_stack1(int data) { if (top1 < top2 - 1) { ar[++top1] = data; } else { printf("Stack Full! Cannot Push\n"); } } void push_stack2(int data) { if (top1 < top2 - 1) { ar[--top2] = data; } else { printf("Stack Full! Cannot Push\n"); } } //Functions to pop data void pop_stack1() { if (top1 >= 0) { int popped_value = ar[top1--]; printf("%d is being popped from Stack 1\n", popped_value); } else { printf("Stack Empty! Cannot Pop\n"); } } void pop_stack2() { if (top2 < SIZE) { int popped_value = ar[top2++]; printf("%d is being popped from Stack 2\n", popped_value); } else { printf("Stack Empty! Cannot Pop\n"); } } //Functions to Print Stack 1 and Stack 2 void print_stack1() { int i; for (i = top1; i >= 0; --i) { printf("%d ", ar[i]); } printf("\n"); } void print_stack2() { int i; for (i = top2; i < SIZE; ++i) { printf("%d ", ar[i]); } printf("\n"); } int main() { int ar[SIZE]; int i; int num_of_ele; printf("We can push a total of 10 values\n"); //Number of elements pushed in stack 1 is 6 //Number of elements pushed in stack 2 is 4 for (i = 1; i <= 6; ++i) { push_stack1(i); printf("Value Pushed in Stack 1 is %d\n", i); } for (i = 1; i <= 4; ++i) { push_stack2(i); printf("Value Pushed in Stack 2 is %d\n", i); } //Print Both Stacks print_stack1(); print_stack2(); //Pushing on Stack Full printf("Pushing Value in Stack 1 is %d\n", 11); push_stack1(11); //Popping All Elements From Stack 1 num_of_ele = top1 + 1; while (num_of_ele) { pop_stack1(); --num_of_ele; } //Trying to Pop From Empty Stack pop_stack1(); return 0; }
- Program to convert infix notation to postfix notation using stacks.
#include <stdio.h> #include <ctype.h> char stack[100]; int top = -1; void push(char x) { stack[++top] = x; } char pop() { if (top == -1) return -1; else return stack[top--]; } int priority(char x) { if (x == '(') return 0; if (x == '+' || x == '-') return 1; if (x == '*' || x == '/') return 2; return 0; } int main() { char exp[100]; char * e, x; printf("Enter the expression : "); scanf("%s", exp); printf("\n"); e = exp; while ( * e != '\0') { if (isalnum( * e)) printf("%c ", * e); else if ( * e == '(') push( * e); else if ( * e == ')') { while ((x = pop()) != '(') printf("%c ", x); } else { while (priority(stack[top]) >= priority( * e)) printf("%c ", pop()); push( * e); } e++; } while (top != -1) { printf("%c ", pop()); } return 0; }
- Program to implement queue using arrays.
#include <stdio.h> #define n 5 int main() { int queue[n], ch = 1, front = 0, rear = 0, i, j = 1, x = n; printf("\tQueue using Array"); printf("\n\t1.Insertion \n\t2.Deletion \n\t3.Display \n\t4.Exit"); while (ch) { printf("\n\nEnter the Choice: "); scanf("%d", & ch); switch (ch) { case 1: if (rear == x) printf("\n Queue is Full"); else { printf("\n Enter no %d : ", j++); scanf("%d", & queue[rear++]); } break; case 2: if (front == rear) { printf("\n Queue is empty"); } else { printf("\n Deleted Element is: %d", queue[front++]); x++; } break; case 3: printf("\nQueue Elements are: \n "); if (front == rear) printf("\n Queue is Empty"); else { for (i = front; i < rear; i++) { printf("%d", queue[i]); printf("\n"); } break; case 4: exit(0); default: printf("Wrong Choice: please see the options"); } } } return 0; }
- Program to implement queue using pointers.
#include <stdio.h> #include <conio.h> #include <process.h> #define true 1 #define false 0 struct q_point { int ele; struct q_point * n; }; struct q_point * f_ptr = NULL; int e_que(void); void add_ele(int); int rem_ele(void); void show_ele(); void main() { int ele, choice, j; while (1) { //clrscr(); printf("======================================================"); printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n"); printf("=============================================="); printf("\n\t\t MENU\n"); printf("=============================================="); printf("\n\t[1] To insert an element"); printf("\n\t[2] To remove an element"); printf("\n\t[3] To display all the elements"); printf("\n\t[4] Exit"); printf("\n\n\tEnter your choice:"); scanf("%d", & choice); switch (choice) { case 1: printf("\n\tElement to be inserted:"); scanf("%d", & ele); add_ele(ele); getch(); break; case 2: if (!e_que()) { j = rem_ele(); printf("\n\t%d is removed from the queue", j); getch(); } else { printf("\n\tQueue is Empty."); getch(); } break; case 3: show_ele(); getch(); break; case 4: exit(1); break; default: printf("\n\tInvalid choice."); getch(); break; } } } int e_que(void) { if (f_ptr == NULL) return true; return false; } void add_ele(int ele) { struct q_point * queue = (struct q_point * ) malloc(sizeof(struct q_point)); queue -> ele = ele; queue -> n = NULL; if (f_ptr == NULL) f_ptr = queue; else { struct q_point * ptr; ptr = f_ptr; for (ptr = f_ptr; ptr -> n != NULL; ptr = ptr -> n); ptr -> n = queue; } } int rem_ele() { struct q_point * queue = NULL; if (e_que() == false) { int j = f_ptr -> ele; queue = f_ptr; f_ptr = f_ptr -> n; free(queue); return j; } else { printf("\n\tQueue is empty."); return -9999; } } void show_ele() { struct q_point * ptr = NULL; ptr = f_ptr; if (e_que()) { printf("\n\tQUEUE is Empty."); return; } else { printf("\n\tElements present in Queue are:\n\t"); while (ptr != NULL) { printf("%d\t", ptr -> ele); ptr = ptr -> n; } } }
- Program to reverse elements in a queue.
#include <stdio.h> int f = -1, r = -1; int q[50]; void enqueue(int data, int l) { //Enqueue for inserting data if (r == l - 1) { printf("Queue is full"); } else if ((f == -1) && (r == -1)) { f = r = 0; q[r] = data; } else { r++; q[r] = data; } } void print() { //Print function for printing the data int i; for (i = f; i <= r; i++) { printf("\n%d", q[i]); } } void reverse() { //reverse function for reversing the data int i, j, t; for (i = f, j = r; i < j; i++, j--) { t = q[i]; q[i] = q[j]; q[j] = t; } } void main() { int n, i = 0, t; printf("Enter the size of Queue: "); scanf("%d", & n); printf("\nEnter the data for Queue: "); while (i < n) { scanf("%d", & t); enqueue(t, n); i++; } printf("\nQueue which you have entered: "); print(); reverse(); printf("\n\nQueue after reversing: "); print(); }
- Program to implement circular queue using arrays.
#include <stdio.h> # define max 6 int queue[max]; // array declaration int front = -1; int rear = -1; // function to insert an element in a circular queue void enqueue(int element) { if (front == -1 && rear == -1) { // condition to check queue is empty front = 0; rear = 0; queue[rear] = element; } else if ((rear + 1) % max == front) { // condition to check queue is full printf("/nQueue is overflow../n"); } else { rear = (rear + 1) % max; // rear is incremented queue[rear] = element; // assigning a value to the queue at the rear position. } } // function to delete the element from the queue int dequeue() { if ((front == -1) && (rear == -1)) { // condition to check queue is empty printf("\nQueue is underflow..\n"); } else if (front == rear) { printf("\nThe dequeued element is: %d", queue[front]); front = -1; rear = -1; } else { printf("\nThe dequeued element is: %d", queue[front]); front = (front + 1) % max; } } // function to display the elements of a queue void display() { int i = front; if (front == -1 && rear == -1) { printf("\n Queue is empty..\n"); } else { printf("\nElements in a Queue are : "); while (i <= rear) { printf("%d\t", queue[i]); i = (i + 1) % max; } } } int main() { int choice = 1, x; // variables declaration while (choice < 4 && choice != 0) { printf("\n1. Insert an element"); printf("\n2. Delete an element"); printf("\n3. Display the element"); printf("\n4. EXIT"); printf("\n\nEnter your choice: "); scanf("%d", & choice); switch (choice) { case 1: printf("Enter the element which is to be inserted: "); scanf("%d", & x); enqueue(x); break; case 2: dequeue(); break; case 3: display(); case 4: exit(0); break; default: printf("\nInvalid Choice\n"); } } return 0; }
- Program to create add remove & display element from single linked list.
#include <stdio.h> #include <stdlib.h> #include <string.h> struct info { char name[30]; int eno; struct info * next; }; struct info * head = NULL, * temp, * disp; void addrecord(); void deleterecord(); void disrecord(); void main() { int ch; //system("cls"); while (1) { printf("\n 1. To add records\n"); printf("\n 2. To delete a records\n"); printf("\n 3. To view the records\n"); printf("\n 4. To exit\n"); printf("\n Enter your choice: "); scanf("%d", & ch); fflush(stdin); switch (ch) { case 1: addrecord(); break; case 2: deleterecord(); break; case 3: disrecord(); break; case 4: exit(0); } } } void addrecord() { struct info * add; char ans = 'y'; while (ans == 'y') { add = (struct info * ) malloc(sizeof(struct info)); printf("\n Enter the names: "); gets(add -> name); fflush(stdin); printf("\n Enter the enrollment number: "); scanf("%d", & add -> eno); fflush(stdin); if (head == NULL) { head = add; add -> next = NULL; temp = add; } else { temp -> next = add; add -> next = NULL; temp = add; } printf("\n Would you like to enter another name(y\\n): "); ans = getchar(); fflush(stdin); } } void deleterecord() { struct info * delete; int teno, present = 0; if (head == NULL) { printf("\n No records to delete\n"); return; } printf("\n Enter the enrollment number to be deleted \n"); scanf("%d", & teno); fflush(stdin); for (delete = head; delete != NULL; delete = delete -> next) { if (delete -> eno == teno) { if (head -> eno == teno) { delete = head; head = head -> next; free(delete); return; } else { temp -> next = delete -> next; free(delete); return; } } temp = delete; } if (present == 0) printf("\nNo such enrollment number present\n"); } void disrecord() { if (head == NULL) { printf("\n No records to view\n"); return; } for (disp = head; disp != NULL; disp = disp -> next) { printf("\n\n Name : %s", disp -> name); printf("\n\n Number : %d\n", disp -> eno); } }
- Program to create add remove & display element from double linked list.
#include <stdio.h> #include <stdlib.h> struct node { struct node * prev; int info; struct node * next; }; struct node * create_list(struct node * start); void display(struct node * start); struct node * addtoempty(struct node * start, int data); struct node * addatbeg(struct node * start, int data); struct node * addatend(struct node * start, int data); struct node * addafter(struct node * start, int data, int item); struct node * addbefore(struct node * start, int data, int item); struct node * del(struct node * start, int data); int main() { int choice, data, item; struct node * start = NULL; while (1) { printf("\n\n1.Create List\n"); printf("2.Display\n"); printf("3.Add to empty list\n"); printf("4.Add at beginning\n"); printf("5.Add at end\n"); printf("6.Add after\n"); printf("7.Add before\n"); printf("8.Delete\n"); printf("9.Quit\n"); printf("\nEnter your choice : "); scanf("%d", & choice); switch (choice) { case 1: start = create_list(start); break; case 2: display(start); break; case 3: printf("Enter the element to be inserted : "); scanf("%d", & data); start = addtoempty(start, data); break; case 4: printf("Enter the element to be inserted : "); scanf("%d", & data); start = addatbeg(start, data); break; case 5: printf("Enter the element to be inserted : "); scanf("%d", & data); start = addatend(start, data); break; case 6: printf("Enter the element to be inserted : "); scanf("%d", & data); printf("Enter the element after which to insert : "); scanf("%d", & item); start = addafter(start, data, item); break; case 7: printf("Enter the element to be inserted : "); scanf("%d", & data); printf("Enter the element before which to insert : "); scanf("%d", & item); start = addbefore(start, data, item); break; case 8: printf("Enter the element to be deleted : "); scanf("%d", & data); start = del(start, data); break; case 9: exit(1); default: printf("Wrong choice\n"); } } return 0; } struct node * create_list(struct node * start) { int i, n, data; printf("\nEnter the number of nodes : "); scanf("%d", & n); start = NULL; if (n == 0) return start; printf("Enter the element to be inserted : "); scanf("%d", & data); start = addtoempty(start, data); for (i = 2; i <= n; i++) { printf("Enter the element to be inserted : "); scanf("%d", & data); start = addatend(start, data); } return start; } void display(struct node * start) { struct node * p; if (start == NULL) { printf("\nList is empty\n"); return; } p = start; printf("\nList is :"); while (p != NULL) { printf("%d ", p -> info); p = p -> next; } printf("\n"); } struct node * addtoempty(struct node * start, int data) { struct node * tmp; tmp = (struct node * ) malloc(sizeof(struct node)); tmp -> info = data; tmp -> prev = NULL; tmp -> next = NULL; start = tmp; return start; } struct node * addatbeg(struct node * start, int data) { struct node * tmp; tmp = (struct node * ) malloc(sizeof(struct node)); tmp -> info = data; tmp -> prev = NULL; tmp -> next = start; start -> prev = tmp; start = tmp; return start; } struct node * addatend(struct node * start, int data) { struct node * tmp, * p; tmp = (struct node * ) malloc(sizeof(struct node)); tmp -> info = data; p = start; while (p -> next != NULL) p = p -> next; p -> next = tmp; tmp -> next = NULL; tmp -> prev = p; return start; } struct node * addafter(struct node * start, int data, int item) { struct node * tmp, * p; tmp = (struct node * ) malloc(sizeof(struct node)); tmp -> info = data; p = start; while (p != NULL) { if (p -> info == item) { tmp -> prev = p; tmp -> next = p -> next; if (p -> next != NULL) p -> next -> prev = tmp; p -> next = tmp; return start; } p = p -> next; } printf("\n%d not present in the list\n\n", item); return start; } struct node * addbefore(struct node * start, int data, int item) { struct node * tmp, * q; if (start == NULL) { printf("\nList is empty\n"); return start; } if (start -> info == item) { tmp = (struct node * ) malloc(sizeof(struct node)); tmp -> info = data; tmp -> prev = NULL; tmp -> next = start; start -> prev = tmp; start = tmp; return start; } q = start; while (q != NULL) { if (q -> info == item) { tmp = (struct node * ) malloc(sizeof(struct node)); tmp -> info = data; tmp -> prev = q -> prev; tmp -> next = q; q -> prev -> next = tmp; q -> prev = tmp; return start; } q = q -> next; } printf("\n%d not present in the list\n", item); return start; } struct node * del(struct node * start, int data) { struct node * tmp; if (start == NULL) { printf("\nList is empty\n"); return start; } if (start -> next == NULL) if (start -> info == data) { tmp = start; start = NULL; free(tmp); return start; } else { printf("\nElement %d not found\n", data); return start; } if (start -> info == data) { tmp = start; start = start -> next; start -> prev = NULL; free(tmp); return start; } tmp = start -> next; while (tmp -> next != NULL) { if (tmp -> info == data) { tmp -> prev -> next = tmp -> next; tmp -> next -> prev = tmp -> prev; free(tmp); return start; } tmp = tmp -> next; } // Deletion of last node if (tmp -> info == data) { tmp -> prev -> next = NULL; free(tmp); return start; } printf("\nElement %d not found\n", data); return start; }
- Program to count number of nodes in linear linked list.
#include <stdio.h> struct node { int data; struct node * next; }; struct node * head; void insert(int data) { struct node * temp = (struct node * ) malloc(sizeof(struct node)); temp -> data = data; temp -> next = head; head = temp; } void print() { struct node * temp = head; int count = 0; while (temp != NULL) { temp = temp -> next; count++; } printf("\n Total no. of nodes is: %d", count); } void main() { head = NULL; insert(4); // HERE insert(7); // NODES insert(9); // ARE insert(1); // INSERTED print(); } /* Output: Total no. of nodes is: 4 */
- Program to create add remove & display element from circular linked list. May you get some Error
#include <stdio.h> #include <conio.h> void insertAtBeginning(int); void insertAtEnd(int); void insertAtAfter(int, int); void deleteBeginning(); void deleteEnd(); void deleteSpecific(int); void display(); struct Node { int data; struct Node * next; }* head = NULL; void main() { int choice1, choice2, value, location; clrscr(); while (1) { printf("\n*********** MENU *************\n"); printf("1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: "); scanf("%d", & choice1); switch (choice1) { case 1: printf("Enter the value to be inserted: "); scanf("%d", & value); while (1) { printf("\nSelect from the following Inserting options\n"); printf("1. At Beginning\n2. At End\n3. After a Node\n4. Cancel\nEnter your choice: "); scanf("%d", & choice2); switch (choice2) { case 1: insertAtBeginning(value); break; case 2: insertAtEnd(value); break; case 3: printf("Enter the location after which you want to insert: "); scanf("%d", & location); insertAfter(value, location); break; case 4: goto EndSwitch; default: printf("\nPlease select correct Inserting option!!!\n"); } } case 2: while (1) { printf("\nSelect from the following Deleting options\n"); printf("1. At Beginning\n2. At End\n3. Specific Node\n4. Cancel\nEnter your choice: "); scanf("%d", & choice2); switch (choice2) { case 1: deleteBeginning(); break; case 2: deleteEnd(); break; case 3: printf("Enter the Node value to be deleted: "); scanf("%d", & location); deleteSpecic(location); break; case 4: goto EndSwitch; default: printf("\nPlease select correct Deleting option!!!\n"); } } EndSwitch: break; case 3: display(); break; case 4: exit(0); default: printf("\nPlease select correct option!!!"); } } } void insertAtBeginning(int value) { struct Node * newNode; newNode = (struct Node * ) malloc(sizeof(struct Node)); newNode -> data = value; if (head == NULL) { head = newNode; newNode -> next = head; } else { struct Node * temp = head; while (temp -> next != head) temp = temp -> next; newNode -> next = head; head = newNode; temp -> next = head; } printf("\nInsertion success!!!"); } void insertAtEnd(int value) { struct Node * newNode; newNode = (struct Node * ) malloc(sizeof(struct Node)); newNode -> data = value; if (head == NULL) { head = newNode; newNode -> next = head; } else { struct Node * temp = head; while (temp -> next != head) temp = temp -> next; temp -> next = newNode; newNode -> next = head; } printf("\nInsertion success!!!"); } void insertAfter(int value, int location) { struct Node * newNode; newNode = (struct Node * ) malloc(sizeof(struct Node)); newNode -> data = value; if (head == NULL) { head = newNode; newNode -> next = head; } else { struct Node * temp = head; while (temp -> data != location) { if (temp -> next == head) { printf("Given node is not found in the list!!!"); goto EndFunction; } else { temp = temp -> next; } } newNode -> next = temp -> next; temp -> next = newNode; printf("\nInsertion success!!!"); } EndFunction: } void deleteBeginning() { if (head == NULL) printf("List is Empty!!! Deletion not possible!!!"); else { struct Node * temp = head; if (temp -> next == head) { head = NULL; free(temp); } else { head = head -> next; free(temp); } printf("\nDeletion success!!!"); } } void deleteEnd() { if (head == NULL) printf("List is Empty!!! Deletion not possible!!!"); else { struct Node * temp1 = head, temp2; if (temp1 -> next == head) { head = NULL; free(temp1); } else { while (temp1 -> next != head) { temp2 = temp1; temp1 = temp1 -> next; } temp2 -> next = head; free(temp1); } printf("\nDeletion success!!!"); } } void deleteSpecific(int delValue) { if (head == NULL) printf("List is Empty!!! Deletion not possible!!!"); else { struct Node * temp1 = head, temp2; while (temp1 -> data != delValue) { if (temp1 -> next == head) { printf("\nGiven node is not found in the list!!!"); goto FuctionEnd; } else { temp2 = temp1; temp1 = temp1 -> next; } } if (temp1 -> next == head) { head = NULL; free(temp1); } else { if (temp1 == head) { temp2 = head; while (temp2 -> next != head) temp2 = temp2 -> next; head = head -> next; temp2 -> next = head; free(temp1); } else { if (temp1 -> next == head) { temp2 -> next = head; } else { temp2 -> next = temp1 -> next; } free(temp1); } } printf("\nDeletion success!!!"); } FuctionEnd: } void display() { if (head == NULL) printf("\nList is Empty!!!"); else { struct Node * temp = head; printf("\nList elements are: \n"); while (temp -> next != head) { printf("%d ---> ", temp -> data); } printf("%d ---> %d", temp -> data, head -> data); } }
- Write a menu driven program to implement array based stack showing following operations.
- Creating an Empty Stack
- Push()
- Pop()
- Peek()
- Underflow
- Overflow
- Exit
#include<stdio.h> #include<stdlib.h> #define size 50 int stack[size]; int N,i,top; int isEmpty(){ if(top==-1) return 1; else{ return 0; } } int isFull(){ if(top==N-1) return 1; else{ return 0; } } void create_Stack(){ printf("Enter the size of the stack\n"); scanf("%d",&N); top=-1; printf("size of the stack is %d\n",N); } void Push(){ int x; printf("enter data you want to insert in the stack"); scanf("%d",&x); if(isFull()==1) /*if(top==4)*/ printf("\nOVERFLOW CONDITION\n"); else{ top+=1; stack[top]=x; } } void Pop(){ int delvalue; if(isEmpty()==1) /* if(top==-1)*/ printf("\nUNDERFLOW CONDITION\n"); else{ delvalue=stack[top]; top--; } printf("\nthe value deleted from the stack is %d \n",delvalue); } void Peek(){ int topvalue; if(isEmpty()==1) printf("\nUNDERFLOW CONDITION\n"); else{ topvalue=stack[top]; printf("\nthe value at the top of the stack is %d \n",topvalue); } } void Display(){ printf("these are the elements present in the stack\n"); for(int i=top;i>=0;i--){ printf("%d \t",stack[i]); } } void main() { int n; do{ printf("enter your choice of STACK OPERATIONS\n"); printf("1.Creating a an empty stack\n"); printf("2.Push()\n"); printf("3.Pop()\n"); printf("4.Peek()\n"); printf("5.Display contents of the stack\n"); printf("6.Check UNDERFLOW condition\n"); printf("7.Check OVERFLOW condition\n"); printf("0.Exit\n"); scanf("%d",&n); switch(n){ case 1: create_Stack(); break; case 2: Push(); break; case 3: Pop(); break; case 4: Peek(); break; case 5: Display(); break; case 6: isEmpty(); break; case 7: isFull(); break; case 0: exit(0); break; default:break; } }while(n!=0); }
- Write a menu driven program to implement array based queue showing following operations.
- Creating queue stack
- Enqueue()
- Dequeue()
- Underflow
- Overflow
- Exit
#include<stdio.h> #include<stdlib.h> #define size 50 int queue[size]; int front,rear,N; int isFull(){ if(rear==(N-1)) return 1; else return 0; } int isEmpty(){ if(front==-1&&rear==-1) return 1; else return 0; } void create_Queue(){ printf("Enter the size of the queue\n"); scanf("%d",&N); front=-1; rear=-1; printf("size of the queue is %d\n",N); } void Enqueue(int x){ if(isFull()==1) printf("OVERFLOW CONDITION"); else if(front==-1&&rear==-1){ front=rear=0; queue[rear]=x; } else{ rear++; queue[rear]=x; } } void Dequeue(){ if(isEmpty()==1) printf("UNDERFLOW CONDITION"); else if(front==rear){ front=rear=-1; } else{ printf("element deleted from the queue is %d ",queue[front]); front++; } } void Display(){ if(isEmpty()==1) printf("UNDERFLOW CONDITION"); else{ printf("the data in the queue is as follows\n"); for(int i=front;i<rear+1;i++) printf("%d\t",queue[i]); } } void main(){ int n,data; do{ printf("enter your choice of QUEUE OPERATIONS\n"); printf("1.Creating a an empty QUEUE\n"); printf("2.Enqueue()\n"); printf("3.Dequeue()\n"); printf("4.Display contents of the QUEUE\n"); printf("5.Check UNDERFLOW condition\n"); printf("6.Check OVERFLOW condition\n"); printf("0.Exit\n"); scanf("%d",&n); switch(n){ case 1: create_Queue(); break; case 2: printf("enter data you want to insert in the queue"); scanf("%d",&data); Enqueue(data); break; case 3: Dequeue(); break; case 4: Display(); break; case 5: isEmpty(); break; case 6: isFull(); break; case 0: exit(0); break; default:break; } }while(n!=0); }