0% found this document useful (0 votes)
120 views68 pages

Dsuc Assignment

The document contains 8 programs written in C programming language to demonstrate various data structures concepts. The programs cover topics like addition, subtraction and multiplication of matrices; summing the diagonal of a matrix; implementing a stack and queue using arrays and linked lists. Each program contains the code, input/output and expected output. The programs provide hands-on examples of commonly used data structures.

Uploaded by

Sonal Dhomne
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)
120 views68 pages

Dsuc Assignment

The document contains 8 programs written in C programming language to demonstrate various data structures concepts. The programs cover topics like addition, subtraction and multiplication of matrices; summing the diagonal of a matrix; implementing a stack and queue using arrays and linked lists. Each program contains the code, input/output and expected output. The programs provide hands-on examples of commonly used data structures.

Uploaded by

Sonal Dhomne
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/ 68

ASSIGNMENT ON

DATA
STRUCTURES
USING C

NAME : SONAL DHOMNE


COURSE : B.TECH CSE
ENROLLMENT NO. : A80105221063
PROGRAM 1. WRITE A PROGRAM FOR ADDITION OF TWO MATRICES.
#include<stdio.h>

int main()

int i,j,m,n,p,q,arr1[10][10],arr2[10][10],res[10][10];

printf("Enter the order of 1st matrix: ");

scanf("%d %d",&m,&n);

printf("Enter the order of 2nd matrix: ");

scanf("%d %d",&p,&q);

if(m==p&n==q)

printf("Enter the 1st matrix:\n");

for(i=0;i<m;++i)

for(j=0;j<n;++j)

scanf("%d",&arr1[i][j]);

printf("Enter the 2st matrix:\n");

for(i=0;i<p;++i)

for(j=0;j<q;++j)

scanf("%d",&arr2[i][j]);

for(i=0;i<p;++i)

for(j=0;j<q;++j)

res[i][j]=arr1[i][j]+arr2[i][j];

printf("Resultant matrix:\n");

for(i=0;i<p;++i)

{
for(j=0;j<q;++j)

printf("%d ",res[i][j]);

printf("\n");

else

printf("Error! order of matrices does not match.");

return 0;

}
OUTPUT:
PROGRAM 2. WRITE A PROGRAM FOR SUBTRACTION OF TWO MATRICES.
#include<stdio.h>

int main()

int i,j,m,n,p,q,arr1[10][10],arr2[10][10],res[10][10];

//input order of the two matrices

printf("Enter the order of 1st matrix: ");

scanf("%d %d",&m,&n);

printf("Enter the order of 2nd matrix: ");

scanf("%d %d",&p,&q);

if(m==p&n==q)

printf("Enter the 1st matrix:\n");

for(i=0;i<m;++i)

for(j=0;j<n;++j)

scanf("%d",&arr1[i][j]);

printf("Enter the 2st matrix:\n");

for(i=0;i<p;++i)

for(j=0;j<q;++j)

scanf("%d",&arr2[i][j]);

for(i=0;i<p;++i)

for(j=0;j<q;++j)

res[i][j]=arr1[i][j]-arr2[i][j];

printf("Resultant matrix:\n");

for(i=0;i<p;++i)

{
for(j=0;j<q;++j)

printf("%d ",res[i][j]);

printf("\n");

else

printf("Error! order of matrices does not match.");

return 0;

}
OUTPUT:
PROGRAM 3. WRITE A PROGRAM FOR MULTIPLICATION OF TWO MATRICES.
#include<stdio.h>

int main()

int i,j,k,m,n,p,q,arr1[10][10],arr2[10][10],res[10][10];

//input order of the two matrices

printf("Enter the order of 1st matrix: ");

scanf("%d %d",&m,&n);

printf("Enter the order of 2nd matrix: ");

scanf("%d %d",&p,&q);

if(n==p)

printf("Enter the 1st matrix:\n");

for(i=0;i<m;++i)

for(j=0;j<n;++j)

scanf("%d",&arr1[i][j]);

printf("Enter the 2st matrix:\n");

for(i=0;i<p;++i)

for(j=0;j<q;++j)

scanf("%d",&arr2[i][j]);

for(i=0;i<p;++i)

for(j=0;j<q;++j)

res[i][j]=0;

for(k=0;k<q;k++)

res[i][j]+=arr1[i][k] * arr2[k][j];
}

printf("Resultant matrix:\n");

for(i=0;i<p;++i)

for(j=0;j<q;++j)

printf("%d ",res[i][j]);

printf("\n");

else

printf("Error! order of matrices does not match.");

return 0;

}
OUTPUT:
PROGRAM 4. WRITE A PROGRAM TO SUM DIAGONAL OF A MATRIX.
#include<stdio.h>

int main()

int i,j,m,arr[10][10],sum=0;

printf("Enter the order of the matrix: ");

scanf("%d",&m);

printf("Enter the matrix:\n");

for(i=0;i<m;++i)

for(j=0;j<m;++j)

scanf("%d",&arr[i][j]);

for(i=0;i<m;++i)

sum+=arr[i][i];

printf("Sum of diagonal of matrix:%d",sum);

return 0;

}
OUTPUT:
PROGRAM 5. WRITE A PROGRAM TO STACK USING ARRAY
IMPLEMENTATION.
#include <limits.h>

#include <stdio.h>

#include <stdlib.h>

struct Stack {

int top;

unsigned capacity;

int* array;

};

struct Stack* createStack(unsigned capacity)

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (int*)malloc(stack->capacity * sizeof(int));

return stack;

// Stack is full when top is equal to the last index

int isFull(struct Stack* stack)

return stack->top == stack->capacity - 1;

// Stack is empty when top is equal to -1

int isEmpty(struct Stack* stack)

return stack->top == -1;

}
// Function to add an item to stack. It increases top by 1

void push(struct Stack* stack, int item)

if (isFull(stack))

return;

stack->array[++stack->top] = item;

printf("%d pushed to stack\n", item);

// Function to remove an item from stack. It decreases top by 1

int pop(struct Stack* stack)

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack->top--];

// Function to return the top from stack without removing it

int peek(struct Stack* stack)

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack->top];

// Driver program to test above functions

int main()

struct Stack* stack = createStack(100);

push(stack, 10);

push(stack, 20);

printf("%d popped from stack\n", pop(stack));

return 0;

}
OUTPUT:
PROGRAM 6. WRITE A PROGRAM TO STACK USING LINKED LIST
IMPLEMETATION.
#include <stdio.h>

#include <stdlib.h>

// Structure to create a node with data and the next pointer

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

// Push() operation on a stack

void push(int value) {

struct Node *newNode;

newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value; // assign value to the node

if (top == NULL) {

newNode->next = NULL;

} else {

newNode->next = top; // Make the node as top

top = newNode; // top always points to the newly created node

printf("Node is Inserted\n\n");

int pop() {

if (top == NULL) {

printf("\nStack Underflow\n");

} else {

struct Node *temp = top;


int temp_data = top->data;

top = top->next;

free(temp);

return temp_data;

void display() {

// Display the elements of the stack

if (top == NULL) {

printf("\nStack Underflow\n");

} else {

printf("The stack is \n");

struct Node *temp = top;

while (temp->next != NULL) {

printf("%d--->", temp->data);

temp = temp->next;

printf("%d--->NULL\n\n", temp->data);

int main() {

int choice, value;

printf("\nImplementation of Stack using Linked List\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:

printf("Popped element is :%d\n", pop());

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nWrong Choice\n");

OUTPUT:
PROGRAM 7. WRITE A PROGRAM TO QUEUE USING ARRAY
IMPLEMENTATION.
#include<stdio.h>

#define SIZE 5

//Basic value initialisation

int queue[SIZE], front = -1, rear = -1;

//Function created to handle enqueue

void enqueue(int item){

if(rear == SIZE-1){

printf("Can't enqueue as the queue is full\n");

else{

//The first element condition

if(front == -1){

front = 0;

rear = rear + 1;

queue[rear] = item;

printf("We have enqueued %d\n",item);

//Function created to handle dequeue

void dequeue(){

if(front == -1){

printf("Can't dequeue as the queue is empty\n");

else{
printf("We have dequeued : %d\n", queue[front]);

front = front + 1;

//Only happens when the last element was dequeued

if(front > rear){

front = -1;

rear = -1;

//function to print the queue

void printQueue(){

if(rear == -1)

printf("\nUnable to display as queue is empty");

else{

int i;

printf("\nThe queue after enqueue & dequeue ops looks like :");

for(i = front; i <= rear; i++)

printf("%d ",queue[i]);

int main()

//enqueue begins here

enqueue(2);

enqueue(4);

enqueue(6);

enqueue(8);
//dequeue beings here

dequeue();

dequeue();

printQueue();

return 0;

OUTPUT:
PROGRAM 8. WRITE A PROGRAM TO QUEUE USING LINKED LIST
IMPLEMENTATION.
#include<stdio.h>

#include<stdlib.h>

// Structure to create a node with data and the next pointer

struct node {

int data;

struct node * next;

};

struct node * front = NULL;

struct node * rear = NULL;

// Enqueue() operation on a queue

void enqueue(int value) {

struct node * ptr;

ptr = (struct node * ) malloc(sizeof(struct node));

ptr -> data = value;

ptr -> next = NULL;

if ((front == NULL) && (rear == NULL)) {

front = rear = ptr;

} else {

rear -> next = ptr;

rear = ptr;

printf("Node is Inserted\n\n");

// Dequeue() operation on a queue

int dequeue() {
if (front == NULL) {

printf("\nUnderflow\n");

return -1;

} else {

struct node * temp = front;

int temp_data = front -> data;

front = front -> next;

free(temp);

return temp_data;

// Display all elements of the queue

void display() {

struct node * temp;

if ((front == NULL) && (rear == NULL)) {

printf("\nQueue is Empty\n");

} else {

printf("The queue is \n");

temp = front;

while (temp) {

printf("%d ", temp -> data);

temp = temp -> next;

printf("\n\n\n");

int main() {

int choice, value;


printf("\nImplementation of Queue using Linked List\n");

while (choice != 4) {

printf("1.Enqueue\n2.Dequeue\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);

enqueue(value);

break;

case 2:

printf("Dequeued element is :%d\n", dequeue());

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nWrong Choice\n");

return 0;

}
OUTPUT:
PROGRAM 9. WRITE A PROGRAM TO CIRCULAR QUEUE USING ARRAY
IMPLEMENTATION.
#include <stdio.h>

#define capacity 6

int queue[capacity];

int front = -1, rear = -1;

// Here we check if the Circular queue is full or not

int checkFull(){

if ((front == rear + 1) || (front == 0 && rear == capacity - 1)){

return 1;

return 0;

// Here we check if the Circular queue is empty or not

int checkEmpty(){

if (front == -1)

return 1;

return 0;

// Addtion in the Circular Queue

void enqueue(int value){

if (checkFull())

printf("Overflow condition\n");

else
{

if (front == -1)

front = 0;

rear = (rear + 1) % capacity;

queue[rear] = value;

printf("%d was enqueued to circular queue\n", value);

// Removal from the Circular Queue

int dequeue() {

int variable;

if (checkEmpty()) {

printf("Underflow condition\n");

return -1;

else

variable = queue[front];

if (front == rear) {

front = rear = -1;

else {

front = (front + 1) % capacity;

printf("%d was dequeued from circular queue\n", variable);

return 1;

}
// Display the queue

void print(){

int i;

if (checkEmpty())

printf("Nothing to dequeue\n");

else

printf("\nThe queue looks like: \n");

for (i = front; i != rear; i = (i + 1) % capacity)

printf("%d ", queue[i]);

printf("%d \n\n", queue[i]);

int main() {

// Not possible as the Circular queue is empty

dequeue();

enqueue(15);

enqueue(20);

enqueue(25);

enqueue(30);

enqueue(35);

print();

dequeue();

dequeue();
print();

enqueue(40);

enqueue(45);

enqueue(50);

enqueue(55);//Overflow condition

print();

return 0;

OUTPUT:
PROGRAM 10. WRITE A PROGRAM TO SINGLY LINKED LIST.
#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head;

void beginsert ();

void lastinsert ();

void randominsert();

void begin_delete();

void last_delete();

void random_delete();

void display();

void search();

void main ()

int choice =0;

while(choice != 9)

printf("\nChoose one option from the following list ...\n");

printf("\n\nMain Menu\n");

printf("\n______________\n");

printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from


Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for an element\
n8.Show\n9.Exit\n");

printf("\nEnter your choice?\n");


scanf("\n%d",&choice);

switch(choice)

case 1:

beginsert();

break;

case 2:

lastinsert();

break;

case 3:

randominsert();

break;

case 4:

begin_delete();

break;

case 5:

last_delete();

break;

case 6:

random_delete();

break;

case 7:

search();

break;

case 8:

display();

break;

case 9:

exit(0);

break;

default:
printf("Please enter valid choice..");

void beginsert()

struct node *ptr;

int item;

ptr = (struct node *) malloc(sizeof(struct node *));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter value\n");

scanf("%d",&item);

ptr->data = item;

ptr->next = head;

head = ptr;

printf("\nNode inserted");

void lastinsert()

struct node *ptr,*temp;

int item;

ptr = (struct node*)malloc(sizeof(struct node));

if(ptr == NULL)

{
printf("\nOVERFLOW");

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

ptr -> next = NULL;

head = ptr;

printf("\nNode inserted");

else

temp = head;

while (temp -> next != NULL)

temp = temp -> next;

temp->next = ptr;

ptr->next = NULL;

printf("\nNode inserted");

void randominsert()

int i,loc,item;

struct node *ptr, *temp;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter element value");

scanf("%d",&item);

ptr->data = item;

printf("\nEnter the location after which you want to insert ");

scanf("\n%d",&loc);

temp=head;

for(i=0;i<loc;i++)

temp = temp->next;

if(temp == NULL)

printf("\ncan't insert\n");

return;

ptr ->next = temp ->next;

temp ->next = ptr;

printf("\nNode inserted");

void begin_delete()

struct node *ptr;

if(head == NULL)
{

printf("\nList is empty\n");

else

ptr = head;

head = ptr->next;

free(ptr);

printf("\nNode deleted from the begining ...\n");

void last_delete()

struct node *ptr,*ptr1;

if(head == NULL)

printf("\nlist is empty");

else if(head -> next == NULL)

head = NULL;

free(head);

printf("\nOnly node of the list deleted ...\n");

else

ptr = head;

while(ptr->next != NULL)

ptr1 = ptr;
ptr = ptr ->next;

ptr1->next = NULL;

free(ptr);

printf("\nDeleted Node from the last ...\n");

void random_delete()

struct node *ptr,*ptr1;

int loc,i;

printf("\n Enter the location of the node after which you want to perform deletion \n");

scanf("%d",&loc);

ptr=head;

for(i=0;i<loc;i++)

ptr1 = ptr;

ptr = ptr->next;

if(ptr == NULL)

printf("\nCan't delete");

return;

ptr1 ->next = ptr ->next;

free(ptr);

printf("\nDeleted node %d ",loc+1);

void search()

{
struct node *ptr;

int item,i=0,flag;

ptr = head;

if(ptr == NULL)

printf("\nEmpty List\n");

else

printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

while (ptr!=NULL)

if(ptr->data == item)

printf("item found at location %d ",i+1);

flag=0;

else

flag=1;

i++;

ptr = ptr -> next;

if(flag==1)

printf("Item not found\n");

}
}

void display()

struct node *ptr;

ptr = head;

if(ptr == NULL)

printf("Nothing to print");

else

printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

printf("\n%d",ptr->data);

ptr = ptr -> next;

OUTPUT:
PROGRAM 11. WRITE A PROGRAM TO DOUBLY LINKED LIST.
#include <stdio.h>

#include <stdlib.h>

// Linked List Node

struct node {

int info;

struct node *prev, *next;

};

struct node* start = NULL;

// Function to traverse the linked list

void traverse()

// List is empty

if (start == NULL) {

printf("\nList is empty\n");

return;

// Else print the Data

struct node* temp;

temp = start;

while (temp != NULL) {

printf("Data = %d\n", temp->info);

temp = temp->next;

// Function to insert at the front

// of the linked list

void insertAtFront()
{

int data;

struct node* temp;

temp = (struct node*)malloc(sizeof(struct node));

printf("\nEnter number to be inserted: ");

scanf("%d", &data);

temp->info = data;

temp->prev = NULL;

// Pointer of temp will be

// assigned to start

temp->next = start;

start = temp;

printf("\nNode inserted.");

// Function to insert at the end of

// the linked list

void insertAtEnd()

int data;

struct node *temp, *trav;

temp = (struct node*)malloc(sizeof(struct node));

temp->prev = NULL;

temp->next = NULL;

printf("\nEnter number to be inserted: ");

scanf("%d", &data);

temp->info = data;

temp->next = NULL;

trav = start;
// If start is NULL

if (start == NULL) {

start = temp;

// Changes Links

else {

while (trav->next != NULL)

trav = trav->next;

temp->prev = trav;

trav->next = temp;

printf("\nNode inserted.");

// Function to insert at any specified

// position in the linked list

void insertAtPosition()

int data, pos, i = 1;

struct node *temp, *newnode;

newnode = malloc(sizeof(struct node));

newnode->next = NULL;

newnode->prev = NULL;

// Enter the position and data

printf("\nEnter position : ");

scanf("%d", &pos);
// If start==NULL,

if (start == NULL) {

start = newnode;

newnode->prev = NULL;

newnode->next = NULL;

// If position==1,

else if (pos == 1) {

// this is author method its correct but we can simply call insertAtfront() function for this
special case

/* newnode->next = start;

newnode->next->prev = newnode;

newnode->prev = NULL;

start = newnode; */

// now this is improved by Jay Ghughriwala on geeksforgeeks

insertAtFront();

// Change links

else {

printf("\nEnter number to be inserted: ");

scanf("%d", &data);

newnode->info = data;

temp = start;

while (i < pos - 1) {

temp = temp->next;

i++;

}
newnode->next = temp->next;

newnode->prev = temp;

temp->next = newnode;

temp->next->prev = newnode;

printf("\nNode inserted.");

// Function to delete from the front

// of the linked list

void deleteFirst()

struct node* temp;

if (start == NULL)

printf("\nList is empty\n");

else {

temp = start;

start = start->next;

if (start != NULL)

start->prev = NULL;

free(temp);

printf("\nNode deleted.");

// Function to delete from the end

// of the linked list

void deleteEnd()

struct node* temp;

if (start == NULL)
printf("\nList is empty\n");

temp = start;

while (temp->next != NULL)

temp = temp->next;

if (start->next == NULL)

start = NULL;

else {

temp->prev->next = NULL;

free(temp);

printf("\nNode deleted.");

// Function to delete from any specified

// position from the linked list

void deletePosition()

int pos, i = 1;

struct node *temp, *position;

temp = start;

// If DLL is empty

if (start == NULL)

printf("\nList is empty\n");

// Otherwise

else {

// Position to be deleted

printf("\nEnter position : ");

scanf("%d", &pos);
// If the position is the first node

if (pos == 1) {

deleteFirst(); // im,proved by Jay Ghughriwala on GeeksforGeeks

if (start != NULL) {

start->prev = NULL;

free(position);

return;

// Traverse till position

while (i < pos - 1) {

temp = temp->next;

i++;

// Change Links

position = temp->next;

if (position->next != NULL)

position->next->prev = temp;

temp->next = position->next;

// Free memory

free(position);

printf("\nNode deleted.");

// Driver Code

int main()

int choice;
while (1) {

printf("\n\n\tMain Menu\n");

printf("\n\t______________\n");

printf("\n\t1 Display\n");

printf("\t2 Insertion at"

" starting\n");

printf("\t3 Insertion at"

" end\n");

printf("\t4 Insertion at "

"any position\n");

printf("\t5 Deletion of "

"first element\n");

printf("\t6 Deletion of "

"last element\n");

printf("\t7 Deletion of "

"element at any position\n");

printf("\t8 Exit\n");

printf("\nEnter Choice :\n");

scanf("%d", &choice);

switch (choice) {

case 1:

traverse();

break;

case 2:

insertAtFront();

break;

case 3:

insertAtEnd();

break;
case 4:

insertAtPosition();

break;

case 5:

deleteFirst();

break;

case 6:

deleteEnd();

break;

case 7:

deletePosition();

break;

case 8:

exit(1);

break;

default:

printf("Incorrect Choice. Try Again \n");

continue;

return 0;

OUTPUT:
PROGRAM 12. WRITE A PROGRAM TO CIRCULAR LINKED 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( )*/


OUTPUT:

You might also like