0% found this document useful (0 votes)
2 views27 pages

Lab Manual MCA DSC (Practical 1-11)

The document outlines practical programming exercises involving data structures in C, including sorting algorithms (Selection Sort, Bubble Sort, Merge Sort, Quick Sort, Heap Sort) and searching algorithms (Linear and Binary Search). It also covers the implementation of a Stack and a Linked List with various operations such as insertion, deletion, and traversal. Each section includes code snippets and explanations for creating and manipulating these data structures.

Uploaded by

dsss919138
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views27 pages

Lab Manual MCA DSC (Practical 1-11)

The document outlines practical programming exercises involving data structures in C, including sorting algorithms (Selection Sort, Bubble Sort, Merge Sort, Quick Sort, Heap Sort) and searching algorithms (Linear and Binary Search). It also covers the implementation of a Stack and a Linked List with various operations such as insertion, deletion, and traversal. Each section includes code snippets and explanations for creating and manipulating these data structures.

Uploaded by

dsss919138
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

PRACTICAL 1

Create a Structure with following Data Members:

1. Integer Array

2. Size of the Array

Sort the Array using various Sorting algorithms such as (i) Selection Sort (ii) Bubble Sort (iii)
Two-way Merge Sort (iv) Quick Sort (v) Heap Sort. And store the sorted Array in a text file

SELECTION SORT

#include<stdio.h>
#include<conio.h>
void selection(int arr[], int n)
{
int i, j, small,temp;
for (i = 0; i< n-1; i++) // One by one move boundary of unsorted
subarr
{
small = i; //minimum element in unsorted array
for (j = i+1; j < n; j++){
if (arr[j] <arr[small]){
small = j;
}
}
// Swap the minimum element with the first element
temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
}
void printArr(int a[], int n) /* function to print the array */
{
int i;
for (i = 0; i< n; i++){
printf("%d ", a[i]);
}
}
int main() {
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
selection(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
BUBBLE SORT

#include<stdio.h>
#define MAX_SIZE 100
int main()
{
int array[MAX_SIZE],i,j,k,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&array[i]);
}
printf("Unsorted Elemets :\n");
for(i=0; i<n; i++)
printf("%d ", array[i]);
printf("\n");

/*Bubble sort logic begins here !*/


for(i=0; i<n-1; i++)
{
printf("PASS %d - : \n\n",i+1);
xchanges=0;
for(j=0; j<n-1-i; j++)
{
printf("Elements are in phase - %d : ",i+1);
for(k=0; k<n; k++)
{
printf("%d ",array[k]);
if(k==n-i-1 && k!=n-1)
printf("___");
}
printf("\n");
printf("Comparing %d and %d ", array[j], array[j+1]);
if( array[j] > array[j+1] )
{
printf("Since %d > %d, Exchange (Swap)
them\n",array[j],array[j+1]);
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
xchanges++;
}
else
printf("Since %d < %d, No Need to exchange\n",array[j],
array[j+1]);
}
if(xchanges==0) /*If list is sorted*/
{
printf("No exchanges in this pass, so the list is sorted\n\n");
break;
}
printf("After Pass %d elements are : \n",i+1);
for(k=0; k<n; k++)
{
printf("%d ", array[k]);
if(k==n-i-2)
printf("___");
}
printf("\n");
}
printf("Sorted list is :\n");
for (i = 0; i< n; i++)
printf("%d ", array[i]);
printf("\n\n");
}

MERGE SORT

#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high)
{
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++)
{
if(a[l1] <= a[l2]) b[i] = a[l1++];
else b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i<= high; i++)
a[i] = b[i];
}
void sort(int low, int high)
{
int mid;
if(low < high) { mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
}
else {
return;
}
}
int main()
{
int i;
printf("List before sorting\n");
for(i = 0; i<= max; i++)
printf("%d ", a[i]);
sort(0, max);

printf("\nList after sorting\n");


for(i = 0; i<= max; i++)
printf("%d ", a[i]);
}

QUICK SORT

#include <stdio.h>
#include <stdbool.h>
#define MAX 7
int intArray[MAX] = {4,6,3,2,1,9,7};
void printline(int count)
{
int i;
for(i = 0;i < count-1;i++) {
printf("=");
}
printf("=\n");
}
void display() {
int i; printf("[");
// navigate through all items
for(i = 0;i <MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]\n");
}

void swap(int num1, int num2) {


int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}
int partition(int left, int right, int pivot) {
int leftPointer = left -1;
int rightPointer = right;
while(true) {
while(intArray[++leftPointer] < pivot) {
//do nothing
}
while(rightPointer> 0 &&intArray[--rightPointer] > pivot) {
//do nothing
}
if(leftPointer>= rightPointer) {
break;
}
else {
printf(" item swapped :%d,%d\n",
intArray[leftPointer],intArray[rightPointer]);
swap(leftPointer,rightPointer);
}
}
printf(" pivot swapped :%d,%d\n",
intArray[leftPointer],intArray[right]);
swap(leftPointer,right);
printf("Updated Array: ");
display();
return leftPointer;
}
void quickSort(int left, int right) {
if(right-left <= 0) {
return;
}
else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}
}
int main() {
printf("Input Array: ");
display();
printline(50);
quickSort(0,MAX-1);
printf("Output Array: ");
display();
printline(50);
}

HEAP SORT

#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j]; number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("How many element are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}

PRACTICAL 2

Create a Structure with following Data Members:

1. Integer Array

2. Size of the Array

Search an element in Array using Linear (Sequential) Search and Binary Search, and
Display result in file. For Sequential Search, assume that input array is Unordered and for
Binary Search assume that input array is Ordered and develop programs accordingly.
Sequential Search
Binary Search

#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL)
return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
printf("Inorder traversal: ");
inorder(root);
printf("\nAfter deleting 10\n");
root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}

PRACTICAL 3

Create a “Stack” data structure with following Data members:


1. Integer Array and Stack Pointer (Top of Stack: Is it same as the Size of the
Array)
Perform the following operations on stack using user-defined functions: Push , Pop ,
Isempty , Isfull , Peep
Create a file which stores all values of Array through Stack. Has it reversed the order of the
elements of the Array? Why?

#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5 //Maximum number of elements that can be stored
int top=-1,stack[MAX];
void push();
void pop();
void display();

void main() {
int ch;
while(1){ //infinite loop, will end when choice will be 4
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong Choice!!");
}
}
}
void push(){
int val;
if(top==MAX-1){
printf("\nStack is full!!");
}
else{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop() {
if(top==-1) {
printf("\nStack is empty!!");
}
else {
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1){
printf("\nStack is empty!!");
}
else{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
OUTPUT
PRACTICAL 4

Create a “Linked List” structure with the following data members: A Data, A link to the next

node

Perform the following operations on stack using user-defined functions:

1. Insert a value X at the first place

2. Insert a value X at the end of the list

3. Insert a value X at the place so that it preserves the ordering of the terms in the
increasing order.
(i) Delete an element whose address is given by X and
(ii) (ii) Copy a linked linear list
Create a file which stores all values of list.

#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;
int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();

while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n---- Insert Menu ----");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at
specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_beg();
break;
case 2:
insert_end();
break;
case 3:
insert_pos();
break;
case 4:
exit(0);
default:
printf("Wrong Choice!!");
}
break;
case 2:
display();
break;
case 3:
printf("\n---- Delete Menu ----");
printf("\n1.Delete from beginning\n2.Delete from end\
n3.Delete from specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
delete_beg();
break;
case 2:
delete_end();
break;
case 3:
delete_pos();
break;
case 4:
exit(0);
default:
printf("Wrong Choice!!");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice!!");
}
}
return 0;
}
void insert_beg()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
if(start==NULL) //If list is empty
{
t->next=NULL;
start=t;
}
else{
t->next=start;
start=t;
}
}
void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
if(start==NULL){ //If list is empty
start=t;
}
else{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}
int insert_pos()
{
int pos,i,num;
if(start==NULL){
printf("List is empty!!");
return 0;
}
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t->next=q->next;
q->next=t;
return 0;
}
void display() {
if(start==NULL){
printf("List is empty!!");
}
else{
q=start;
printf("The linked list is:\n");
while(q!=NULL){
printf("%d->",q->data);
q=q->next;
}
}
}
void delete_beg() {
if(start==NULL){
printf("The list is empty!!");
}
else{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}
void delete_end() {
if(start==NULL){
printf("The list is empty!!");
}
else{
q=start;
while(q->next->next!=NULL)
q=q->next;
t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}
int delete_pos()
{
int pos,i;
if(start==NULL){
printf("List is empty!!");
return 0;
}
printf("Enter position to delete:");
scanf("%d",&pos);
q=start;
for(i=1;i<pos-1;i++){
if(q->next==NULL){
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);
return 0;
}

PRACTICAL 5

Write a program to convert an infix arithmetic expression (parenthesize / unparenthesized)


into postfix notation.

#include<stdio.h>
#define max 100
int top=-1, a[max];
void push(char x)
{
a[++top]=x;
}
char pop() {
if(top==-1)
return -1;
else
return a[top--];
}
int prcd(char c) {
if(c=='(')
return 0;
else if(c=='+'||c=='-')
return 1;
else if(c=='*'||c=='/')
return 2;
}
int infixtopostfix(char infix[max],char postfix[max])
{
char temp,x;
int i=0,j=0;

while(infix[i]!='\0')
{
temp=infix[i];
if(isalnum(temp)){
postfix[j++]=temp;
}
else if(temp=='(')
push(temp);
else if(temp==')')
{
while((x=pop())!='('){
postfix[j++]=x;
}
}
else{
while(prcd(a[top])>=prcd(temp)){
postfix[j++]=pop();
}
push(temp);
}
i++;
}
while(top!= -1)
postfix[j++]=pop();
postfix[j]='\0';
}
void main() {
char infix[max],postfix[max];
printf("Enter the infix expression\n");
gets(infix);
printf("The infix expression is %s\n",infix);
infixtopostfix(infix, postfix);
printf("The postfix expression is %s\n",postfix);
}
PRACTICAL7

Create a structure with the following Data members:

1. Integer Array
2. 2. Size of the Array

Search an element in a given list using Binary Search by recursion. And Display result in a
file.

#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL)
return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
printf("Inorder traversal: ");
inorder(root);
printf("\nAfter deleting 10\n");
root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}

PRACTICAL 8

Create a “Queue” structure with following Data members:

1. Integer Array

2. Size of the Array

Perform the following operations on Simple queue using user-defined functions:


1. Insert an element 2. Remove an element 3. Display 4. Isfull 5. Isempty Create a file which
stores all values of Array

#include<stdio.h>
#define n 7
int main() {
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\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;
}

OUTPUT

PRACTICAL 10

Create a “Circular Queue” structure with following Data members:


1. Integer Array

2. Size of the Array

Perform the following operations on Circular queue using user-defined functions: 1. Insert
an element 2. Remove an element 3. Display 4. Isfull 5. Isempty Create a file which stores
all values of Array

#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
// check if the queue is full
int isFull() {
if ((front == (rear + 1) % SIZE) || (front == 0 && rear == SIZE - 1))
return 1;
return 0;
}
// check if the queue is empty
int isEmpty() {
if (front == -1)
return 1;
return 0;
}
// adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
}
else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
// display the queue
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
int main() {
// fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4); enQueue(5);
// fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// fails to enqueue because front == rear + 1
enQueue(8);
return 0;
}
PRACTICAL 11

Create a “Circular Queue” user-defined structure with the following data members:
1. A Data
2. A link to the next node
Perform the following operations on Circular queue using userdefined functions: 1. Insert an
element 2. Remove an element 3. Display 4. Isfull 5. IsemptyCreate a file which stores all
values of list.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *create_list(struct node *last);
void display(struct node *last);
struct node *addtoempty(struct node *last,int data);
struct node *addatbeg(struct node *last,int data);
struct node *addatend(struct node *last,int data);
struct node *addafter(struct node *last,int data,int item);
struct node *del(struct node *last,int data);

int main( )
{
int choice,data,item;
struct node*last=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.Delete\n");
printf("8.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
last=create_list(last);
break;
case 2:
display(last);
break;
case 3:
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
last=addtoempty(last,data);
break;
case 4:
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
last=addatbeg(last,data);
break;
case 5:
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
last=addatend(last,data);
break;
case 6:
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
printf("\nEnter the element after which to insert : ");
scanf("%d",&item);
last=addafter(last,data,item);
break;
case 7:
printf("\nEnter the element to be deleted : ");
scanf("%d",&data);
last=del(last,data);
break;
case 8:
exit(1);
default:
printf("\nWrong choice\n");
}/*End of switch*/
}/*End of while*/
return 0;
}/*End of main( )*/
struct node *create_list(struct node *last)
{
int i,n,data;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
last=NULL;
if(n==0)
return last;
printf("Enter the element to be inserted : ");
scanf("%d",&data);
last=addtoempty(last,data);
for(i=2;i<=n;i++){
printf("Enter the element to be inserted : ");
scanf("%d",&data);
last=addatend(last,data);
}
return last;
}/*End of create_list()*/
struct node *addtoempty(struct node *last,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
last=tmp;
last->link=last;
return last;
}/*End of addtoempty( )*/
struct node *addatbeg(struct node *last,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
return last;
}/*End of addatbeg( )*/
struct node *addatend(struct node *last,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
last=tmp;
return last;
}/*End of addatend( )*/
struct node *addafter(struct node *last,int data,int item)
{
struct node *tmp,*p;
p=last->link;
do
{
if(p->info==item){
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=p->link;
p->link=tmp;
if(p==last)
last=tmp;
return last;
}
p=p->link;
}
while(p!=last->link);
printf("%d not present in the list\n",item);
return last;
}/*End of addafter()*/
struct node *del(struct node *last,int data)
{
struct node *tmp,*p;
if(last==NULL){
printf("List is empty\n");
return last;
}
/*Deletion of only node*/
if(last->link==last && last->info==data){
tmp=last;
last=NULL;
free(tmp);
return last;
}
/*Deletion of first node*/
if(last->link->info==data){
tmp=last->link;
last->link=tmp->link;
free(tmp);
return last;
}
/*Deletion in between*/
p=last->link;
while(p->link!=last)
{
if(p->link->info==data){
tmp=p->link;
p->link=tmp->link;
free(tmp);
return last;
}
p=p->link;
}
/*Deletion of last node*/
if(last->info==data){
tmp=last;
p->link=last->link;
last=p;
free(tmp);
return last;
}
printf("\nElement %d not found\n",data);
return last;
}/*End of del( )*/

void display(struct node *last)


{
struct node *p;
if(last==NULL){
printf("\nList is empty\n");
return;
}
p=last->link;
do{
printf("%d ",p->info);
p=p->link;
}
while(p!=last->link);
printf("\n");
}/*End of display( )*/

You might also like