0% found this document useful (0 votes)
125 views41 pages

Assignment 7

The document contains 5 code snippets showing how to perform various operations on singly linked lists in C such as: 1. Creating and displaying a singly linked list. 2. Inserting a node at the beginning of a linked list. 3. Deleting a node from the beginning of a linked list. The code snippets demonstrate how to implement basic linked list operations like insertion, deletion and traversal through the use of pointers and memory allocation functions in C. Each snippet is accompanied by sample input/output to test the functionality.
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)
125 views41 pages

Assignment 7

The document contains 5 code snippets showing how to perform various operations on singly linked lists in C such as: 1. Creating and displaying a singly linked list. 2. Inserting a node at the beginning of a linked list. 3. Deleting a node from the beginning of a linked list. The code snippets demonstrate how to implement basic linked list operations like insertion, deletion and traversal through the use of pointers and memory allocation functions in C. Each snippet is accompanied by sample input/output to test the functionality.
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/ 41

PROGRAMMING FOR PROBLEM SOLVING LAB

Subject Code: ESC-295

UNIVERSITY OF ENGINEERING & MANAGEMENT KOLKATA


NEWTOWN, KOLKATA – 160

Name: RAJARSHI BANERJEE

Enrollment No: 12019009022178

Registration No: 304201900900812

Department: CST

1st Year, Section - F

Roll No: 67
1. WAP in C to create and display a singly 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;
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);
}

OUTPUT:

2. WAP in C to insert a node at beginning of a Singly linked-list:


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void insertNodeAtBeginning(int data);
void displayList();
int main()
{
int n, data;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter data to insert at beginning of the list: ");
scanf("%d", &data);
insertNodeAtBeginning(data);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);

head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtBeginning(int data)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = head;
head = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next; }
}
}

OUTPUT:
3. WAP in C to insert a node at end of a Singly linked-list:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void insertNodeAtEnd(int data);
void displayList();
int main()
{
int n, data;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nEnter data to insert at end of the list: ");
scanf("%d", &data);
insertNodeAtEnd(data);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link the data field with data
head->next = NULL; // Link the address field to NULL
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtEnd(int data)
{
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; // Link the data part
newNode->next = NULL;
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode; // Link address part
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

OUTPUT:

4. WAP in C to insert a node at the middle of a Singly linked-list:


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void insertNodeAtMiddle(int data, int position);
void displayList();
int main()
{
int n, data, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("nEnter data to insert at middle of the list: ");
scanf("%d", &data);
printf("Enter the position to insert new node: " );
scanf("%d", &position);
insertNodeAtMiddle(data, position);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtMiddle(int data, int position)
{
int i;
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = NULL;
temp = head;
for(i=2; i<=position-1; i++)
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
newNode->next = temp->next;
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n");
}
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

OUTPUT:

5. WAP in C to delete a node from the beginning of a Singly


linked-list:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
OUTPUT:

6. WAP in C to delete a node from the end of a Singly linked-list:


#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void LastNodeDeletion();
void displayList();
int main()
{
int n,num,pos;
printf("\n\n Linked List : Delete the last node of Singly Linked List :\n");
printf("---------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
LastNodeDeletion();
printf("\n The new list after deletion the last node are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;

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


if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL; //Links the address field to NULL
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void LastNodeDeletion()
{
struct node *toDelLast, *preNode;
if(stnode == NULL)
{
printf(" There is no element in the list.");
}
else
{
toDelLast = stnode;
preNode = stnode;
while(toDelLast->nextptr != NULL)
{
preNode = toDelLast;
toDelLast = toDelLast->nextptr;
}
if(toDelLast == stnode)
{
stnode = NULL;
}
else
{
preNode->nextptr = NULL;
}
free(toDelLast);
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the empty list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
OUTPUT:

7. WAP in C to delete a node from the middle of a Singly linked


list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;

void createNodeList(int n);


void MiddleNodeDeletion(int pos);
void displayList();

int main()
{
int n,num,pos;
printf("\n\n Linked List : Delete a node from the middle of Singly Linked List. :\n");
printf("-------------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input the position of node to delete : ");
scanf("%d", &pos);
if(pos<=1 || pos>=n)
{
printf("\n Deletion can not be possible from that position.\n ");
}
if(pos>1 && pos<n)
{
printf("\n Deletion completed successfully.\n ");
MiddleNodeDeletion(pos);
}
printf("\n The new list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL; //Links the address field to NULL
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL) {
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void MiddleNodeDeletion(int pos)
{
int i;
struct node *toDelMid, *preNode;
if(stnode == NULL)
{
printf(" There are no nodes in the List.");
}
else
{
toDelMid = stnode;
preNode = stnode;
for(i=2; i<=pos; i++)
{
preNode = toDelMid;
toDelMid = toDelMid->nextptr;
if(toDelMid == NULL)
break;
}
if(toDelMid != NULL)
{
if(toDelMid == stnode)
stnode = stnode->nextptr;
preNode->nextptr = toDelMid->nextptr;
toDelMid->nextptr = NULL;
free(toDelMid);
}
else
{
printf(" Deletion can not be possible from that position.");
}
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}

OUTPUT:

8. WAP in C to create and display a Doubly linked-list.


#include<stdio.h>
#include<stdlib.h>
struct node {
int num;
struct node *preptr;
struct node *nextptr;
}*stnode,*ennode;
void DlListcreation(int n);
void displayDlList();
int main()
{
int n;
stnode=NULL;
ennode=NULL;
printf("\n\n Doubly Linked List : Create and display a doubly linked list :\n");
printf("-------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d",&n);
DlListcreation(n);
displayDlList();
return 0;
}
void DlListcreation(int n)
{
int i,num;
struct node *fnNode;
if(n >=1)
{
stnode=(struct node *)malloc(sizeof(struct node));
if(stnode!=NULL)
{
printf(" Input data for node 1 : ");
scanf("%d",&num);
stnode->num=num;
stnode->preptr=NULL;
stnode->nextptr=NULL;
ennode=stnode;
for(i=2; i<=n; i++)
{
fnNode=(struct node *)malloc(sizeof(struct node));
if(fnNode!=NULL)
{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnNode->num=num;
fnNode->preptr=ennode;
fnNode->nextptr=NULL;
ennode->nextptr=fnNode;
ennode=fnNode;
}
else
{
printf(" Memory can not be allocated.");
break;
}
}
}
else
{
printf(" Memory can not be allocated.");
}
}
}
void displayDlList()
{
struct node *tmp;
int n =1;
if(stnode==NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp=stnode;
printf("\n\n Data entered on the list are :\n");
while(tmp!=NULL)
{
printf(" node %d : %d\n", n,tmp->num);
n++;
tmp=tmp->nextptr;
}
}
}

OUTPUT:
9. WAP in C to insert a node in the middle of a Doubly linked-list.
#include<stdio.h>
#include<stdlib.h>
struct node {
int num;
struct node *preptr;
struct node *nextptr;
}*stnode,*ennode;
void DlListcreation(int n);
void DlLinsertNodeAtMiddle(int num,int pos);
void displayDlList(int a);
int main()
{
int n,num1,a,insPlc;
stnode=NULL;
ennode=NULL;
printf("\n\n Doubly Linked List : Insert new node at the middle in a doubly linked list :\n");
printf("----------------------------------------------------------------------------------\n");
printf(" Input the number of nodes (3 or more ): ");
scanf("%d",&n);
DlListcreation(n);
a=1;
displayDlList(a);
printf(" Input the position ( 2 to %d ) to insert a new node : ",n-1);
scanf("%d",&insPlc);
if(insPlc<=1||insPlc>=n)
{
printf("\n Invalid position. Try again.\n ");
}
if(insPlc>1&&insPlc<n)
{
printf(" Input data for the position %d : ",insPlc);
scanf("%d",&num1);
DlLinsertNodeAtMiddle(num1,insPlc);
a=2;
displayDlList(a);
}
return 0;
}
void DlListcreation(int n)
{
int i,num;
struct node *fnNode;
if(n >=1)
{
stnode=(struct node *)malloc(sizeof(struct node));
if(stnode!=NULL)
{
printf(" Input data for node 1 : ");// assigning data in the first node
scanf("%d",&num);
stnode->num=num;
stnode->preptr=NULL;
stnode->nextptr=NULL;
ennode=stnode;
for(i=2; i<=n; i++)
{
fnNode=(struct node *)malloc(sizeof(struct node));
if(fnNode!=NULL)
{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnNode->num=num;
fnNode->preptr=ennode;
fnNode->nextptr=NULL;
ennode->nextptr=fnNode;
ennode=fnNode;
}
else
{
printf(" Memory can not be allocated.");
break;
}
}
}
else
{
printf(" Memory can not be allocated.");
}
}
}
void DlLinsertNodeAtMiddle(int num,int pos)
{
int i;
struct node *newnode,*tmp;
if(ennode==NULL)
{
printf(" No data found in the list!\n");
}
else
{
tmp=stnode;
i=1;
while(i<pos-1&&tmp!=NULL)
{
tmp=tmp->nextptr;
i++;
}
if(tmp!=NULL)
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->num=num;
newnode->nextptr=tmp->nextptr;
newnode->preptr=tmp;
if(tmp->nextptr!=NULL)
{
tmp->nextptr->preptr=newnode;
}
tmp->nextptr=newnode;
else
{
printf(" The position you entered, is invalid.\n");
}
}
}
void displayDlList(int m)
{
struct node *tmp;
int n =1;
if(stnode==NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp=stnode;
if(m==1)
{
printf("\n Data entered in the list are :\n");
}
else
{
printf("\n After insertion the new list are :\n");
}
while(tmp!=NULL)
{
printf(" node %d : %d\n", n,tmp->num);
n++;
tmp=tmp->nextptr;
}
}
}

OUTPUT:

10. WAP in C to delete a node from the tail of a Doubly linked-list.


#include<stdio.h>
#include<stdlib.h>
struct node {
int num;
struct node *preptr;
struct node *nextptr;
}*stnode,*ennode;
void DlListcreation(int n);
void DlListDeleteLastNode();
void displayDlList(int a);
int main()
{
int n,num1,a,insPlc;
stnode=NULL;
ennode=NULL;
printf("\n\n Doubly Linked List : Delete node from the last of a doubly linked list :\n");
printf("-----------------------------------------------------------------------------\n");
printf(" Input the number of nodes (3 or more ): ");
scanf("%d",&n);
DlListcreation(n);
a=1;
displayDlList(a);
DlListDeleteLastNode();
a=2;
displayDlList(a);
return 0;
}
void DlListcreation(int n)
{
int i,num;
struct node *fnNode;
if(n >=1)
{
stnode=(struct node *)malloc(sizeof(struct node));
if(stnode!=NULL)
{
printf(" Input data for node 1 : ");
scanf("%d",&num);
stnode->num=num;
stnode->preptr=NULL;
stnode->nextptr=NULL;
ennode=stnode;
for(i=2; i<=n; i++)
{
fnNode=(struct node *)malloc(sizeof(struct node));
if(fnNode!=NULL)
{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnNode->num=num;
fnNode->preptr=ennode;
node->nextptr=NULL;
ennode->nextptr=fnNode;
ennode=fnNode;
}
else
{
printf(" Memory can not be allocated.");
break;
}
}
}
else
{
printf(" Memory can not be allocated.");
}
}
}
void DlListDeleteLastNode()
{
struct node *NodeToDel;
if(ennode==NULL)
{
printf(" Delete is not possible. No data in the list.\n");
}
else
{
NodeToDel=ennode;
ennode=ennode->preptr;
ennode->nextptr=NULL;
free(NodeToDel);
}
}
void displayDlList(int m)
{
struct node *tmp;
int n =1;
if(stnode==NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp=stnode;
if(m==1)
{
printf("\n Data entered in the list are :\n");
}
else
{
printf("\n After deletion the new list are :\n");
}
while(tmp!=NULL)
{
printf(" node %d : %d\n", n,tmp->num);
n++;
tmp=tmp->nextptr;
}
}

OUTPUT:

11. WAP in C to reverse a singly linked list


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void reverseList();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to reverse the order of singly linked list\n");
scanf("%d", &choice);
if(choice == 1)
{
reverseList();
}
printf("\nData in the list\n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
if(n <= 0)
{
printf("List size must be greater than zero.\n");
return;
}
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);

head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void reverseList()
{
struct node *prevNode, *curNode;
if(head != NULL)
{
prevNode = head;
curNode = head->next;
head = head->next;
prevNode->next = NULL; // Make first node as last node
while(head != NULL)
{
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
}
head = prevNode;
printf("SUCCESSFULLY REVERSED LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

OUTPUT:

12. WAP in C to search an element in a Singly linked list.


#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;
}
}

OUTPUT:
13. WAP in C to delete an element from a circular linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode;

struct node *tail,*p,*q,*store;


void ClListcreation(int n);
void ClListDeleteMiddle(int pos);
void displayClList(int a);

int main()
{
int n,num1,a,pos;
stnode = NULL;
printf("\n\n Circular Linked List : Delete node from the middle of a circular linked list
:\n");
printf("-----------------------------------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);
ClListcreation(n);
a=1;
displayClList(a);
printf("\n Input the position to delete the node : ");
scanf("%d",&pos);
ClListDeleteMiddle(pos);
a=2;
displayClList(a);
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;
}
preptr->nextptr = stnode;
}
}
void ClListDeleteMiddle(int pos)
{
int delNode,k=1;
delNode=pos;
p=stnode;
while(k!=delNode)
{
q=p;
p=p->nextptr;
k++;
}
q->nextptr=p->nextptr;
printf("\n The deleted node is : %d",p->num);
free(p);
}
void displayClList(int m)
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
if (m==1)
{
printf("\n Data entered in the list are :\n");
}
else
{
printf("\n After deletion the new list are :\n");
}
do {
printf(" Data %d = %d\n", n, tmp->num);

tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}

OUTPUT:

14. WAP in C to reverse a circular linked-list


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
void insert(int data) {
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data = data;
link->next = NULL;
if(head==NULL) {
head = link;
head->next = link;
return;
}
current = head;
while(current->next != head)
current = current->next;
current->next = link;
link->next = head;
}
void reverse_print(struct node *list) {
if(list->next == head) {
printf(" %d =>",list->data);
return;
}
reverse_print(list->next);
printf(" %d =>",list->data);
}
int main() {
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
reverse_print(head);
printf(" [head]\n");
return 0;
}

OUTPUT:
15. WAP in C to search an element in a circular linked list:
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode,*ennode;
void ClListcreation(int n);
int FindElement(int FindElem, int n);
void displayClList();
int main()
{
int n,m;
int i,FindElem,FindPlc;
stnode = NULL;
ennode = NULL;
printf("\n\n Circular Linked List : Search an element in a circular linked list :\n");
printf("-------------------------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);
m=n;
ClListcreation(n);
displayClList();
printf(" Input the element you want to find : ");
scanf("%d", &FindElem);
FindPlc=FindElement(FindElem,m);
if(FindPlc<n)
printf(" Element found at node %d \n\n",FindPlc);
else
printf(" This element does not exists in linked list.\n\n");
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL;
preptr->nextptr = newnode;
preptr = newnode;
}
preptr->nextptr = stnode;
}
}
int FindElement(int FindElem, int a)
{
int ctr=1;
ennode=stnode;
while(ennode->nextptr!=NULL)
{
if(ennode->num==FindElem)
break;
else
ctr++;
ennode=ennode->nextptr;
if (ctr==a+1)
break;
}
return ctr;
}
void displayClList()
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
printf("\n\n Data entered in the list are :\n");
do {
printf(" Data %d = %d\n", n, tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}

OUTPUT:

You might also like