Assignment 7
Assignment 7
Department: CST
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:
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:
OUTPUT:
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:
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:
OUTPUT:
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:
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;
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");
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:
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");
OUTPUT: