0% found this document useful (0 votes)
12 views33 pages

Data Structure Assignment

The document provides C code implementations for various linked list operations, including single linked lists, doubly linked lists, and circular linked lists. It covers insertion and deletion of nodes at the beginning, end, and specific positions, as well as counting and displaying nodes. Additionally, it includes search functionality for finding nodes within the lists.

Uploaded by

higigiy447
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)
12 views33 pages

Data Structure Assignment

The document provides C code implementations for various linked list operations, including single linked lists, doubly linked lists, and circular linked lists. It covers insertion and deletion of nodes at the beginning, end, and specific positions, as well as counting and displaying nodes. Additionally, it includes search functionality for finding nodes within the lists.

Uploaded by

higigiy447
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/ 33

SINGLE LINKED LIST:

• Insert a Node at Beginning, at Ending and at a


given Position
#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node* next;

};

void Linked_list(struct node*ptr)

while(ptr!=NULL)

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

ptr=ptr->next;

void insertB(struct node*head,struct node*newnode)

newnode->next=head;

head=newnode;

Linked_list(head);

void insertE(struct node*head,struct node*newnode)

struct node*ptr=NULL;

ptr=head;

while(ptr->next!=NULL)
{

ptr=ptr->next;

ptr->next=newnode;

newnode->next=NULL;

Linked_list(head);

void insertP(struct node*head,struct node*newnode,int n)

int p,i;

printf("enter the position to be inserted");

scanf("%d",&p);

struct node*ptr=NULL;

ptr=head;

if(n<p)

printf("invalid input");

else

for(i=1;i<p-1;i++)

ptr=ptr->next;

newnode->next=ptr->next;

ptr->next=newnode;

Linked_list(head);

int main()

struct node*head,*ptr,*newnode;

head=0;
int n,i,k;

printf("enter the number of elements in linked list");

scanf("%d",&n);

printf("enter the data");

for(i=1;i<=n;i++)

newnode=malloc(sizeof(struct node));

scanf("%d",&newnode->data);

newnode->next=NULL;

if(head==0)

head=ptr=newnode;

else

ptr->next=newnode;

ptr=newnode;

Linked_list(head);

printf("\n");

printf("enter the value to be inserted");

newnode=malloc(sizeof(struct node));

scanf("%d",&newnode->data);

printf("enter the number for the insertion 1-beginning, 2-end, 3-position");

scanf("%d",&k);

if(k==1)

insertB(head,newnode);

else if(k==2)

insertE(head,newnode);

else if(k==3)
insertP(head,newnode,n);

AT BEGINNING:

AT END:

AT GIVEN POSITION :
• Delete a Node at Beginning, at Ending and at a given
Position
#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

void Linked_list(struct node*ptr)

while(ptr!=0)

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

ptr=ptr->next;

void deleteB(struct node*head)

{
struct node*ptr=NULL;

ptr=head;

head=head->next;

free(ptr);

printf("\n");

Linked_list(head);

void deleteE(struct node*head)

struct node*ptr=head;

struct node*q=head->next;

while(q->next!=NULL)

ptr=ptr->next;

q=q->next;

ptr->next=NULL;

free(q);

printf("\n");

Linked_list(head);

void deleteP(struct node*head,int n)

int p,i;

struct node*ptr=head;

printf("enter the position to be deleted");

scanf("%d",&p);

if(n<p)

printf("invalid");

else

{
for(i=1;i<p-1;i++)

ptr=ptr->next;

struct node*q=ptr->next;

ptr->next=q->next;

free(q);

printf("\n");

Linked_list(head);

int main()

struct node*head,*ptr,*newnode;

head=0;

int n,i,k;

printf("enter the number of elements in the linked list");

scanf("%d",&n);

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

newnode=malloc(sizeof(struct node));

scanf("%d",&newnode->data);

newnode->next=NULL;

if(head==0)

head=ptr=newnode;

else

ptr->next=newnode;

ptr=newnode;
}

Linked_list(head);

printf("\n");

printf("enter the value for deletion at 1-begining,2-ending,3- postion\n");

scanf("%d",&k);

if(k==1)

deleteB(head);

else if(k==2)

deleteE(head);

else if(k==3)

deleteP(head,n);

else

printf("invalid number");

AT BEGINNING:
AT END:

AT GIVEN NODE:
• Search, Count the Number of Nodes and Display
#include <stdio.h>

//Represent a node of singly linked list

struct node{

int data;

struct node *next;

};

//Represent the head and tail of the singly linked list

struct node *head, *tail = NULL;

//addNode() will add a new node to the list

void addNode(int data) {

//Create a new node

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

newNode->data = data;

newNode->next = NULL;

//Checks if the list is empty

if(head == NULL) {

//If list is empty, both head and tail will point to new node

head = newNode;

tail = newNode;

else {

//newNode will be added after tail such that tail's next will point to newNode

tail->next = newNode;

//newNode will become new tail of the list

tail = newNode;

}
}

//countNodes() will count the nodes present in the list

int countNodes() {

int count = 0;

//Node current will point to head

struct node *current = head;

while(current != NULL) {

//Increment the count by 1 for each node

count++;

current = current->next;

return count;

//display() will display all the nodes present in the list

void display() {

//Node current will point to head

struct node *current = head;

if(head == NULL) {

printf("List is empty\n");

return;

printf("Nodes of singly linked list: \n");

while(current != NULL) {

//Prints each node by incrementing pointer

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

current = current->next;

}
printf("\n");

int main()

//Add nodes to the list

addNode(1);

addNode(2);

addNode(3);

addNode(4);

display();

//Counts the nodes present in the given list

printf("Count of nodes present in the list: %d", countNodes());

return 0;

OUTPUT:

Design a program to create a doubly linked list for the


following operations
• Insert a Node at Beginning, at Ending and at a given Position
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node*next;
struct node*prev;
};

void Dlinked_list(struct node*ptr,struct node*qtr)


{
while(ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n");
while(qtr!=NULL)
{
printf("\n%d",qtr->data);
qtr=qtr->prev;
}
}

void insertionB(struct node*head,struct node*newnode)


{
struct node*p=head;
newnode->prev=NULL;
newnode->next=head;
head->prev=newnode;
head=newnode;
printf("\n");
while(p->next!=NULL)
{
p=p->next;
}
Dlinked_list(head,p);
}

void insertionE(struct node*head,struct node*newnode)


{
struct node*p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
newnode->prev=p;
newnode->next=NULL;
Dlinked_list(head,newnode);
}

void insertionP(struct node*head,struct node*newnode,int n)


{
int p,i;
struct node*ptr=head;
struct node*r=head;
struct node*q=NULL;
printf("enter the postion where the node should be inserted");
scanf("%d",&p);
if(p>n)
printf("invalid");
else
{
for(i=1;i<p-1;i++)
{
ptr=ptr->next;
}
newnode->next=ptr->next;
q=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
q->prev=newnode;
while(r->next!=NULL)
{
r=r->next;
}
Dlinked_list(head,r);
}
}

int main()
{
struct node*head,*ptr,*newnode,*newnode2;
int n,i,k;
head=0;
printf("enter the number nodes to be inserted");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(head==0)
head=ptr=newnode;
else
{
ptr->next=newnode;
newnode->prev=ptr;
ptr=newnode;
}
}
Dlinked_list(head,newnode);
printf("\nenter the number to be inserted");
newnode2=malloc(sizeof(struct node));
scanf("%d",&newnode2->data);
printf("enter number for insertion 1-begining,2-ending,3-position");
scanf("%d",&k);
if(k==1)
insertionB(head,newnode2);
if(k==2)
insertionE(head,newnode2);
if(k==3)
insertionP(head,newnode2,n);
else
printf(" ");
}
OUTPUT:
AT BEGINNING:

AT END:
AT GIVEN NODE:

• Delete a Node at Beginning, at Ending and at a given


Position
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
struct node*prev;
};
void Dlinked_list(struct node*ptr,struct node*qtr)
{
while(ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n");
while(qtr!=NULL)
{
printf("\n%d",qtr->data);
qtr=qtr->prev;
}
printf("\n");
}
void deleteB(struct node*head)
{
struct node*p=head;
head=head->next;
head->prev=NULL;
free(p);
struct node*q=head;
while(q->next!=NULL)
{
q=q->next;
}
Dlinked_list(head,q);
}

void deleteE(struct node*head)


{
struct node*p=head;
struct node*q=head->next;
while(q->next!=NULL)
{
p=p->next;
q=q->next;
}
p->next=NULL;
free(q);
Dlinked_list(head,p);
}
void deleteP(struct node*head,int n)
{
int p,i;
printf("enter the postion to be deleted");
scanf("%d",&p);
struct node*ptr=head;
struct node*z=head;
if(p>n)
printf("invalid");
else
{
for(i=1;i<p-1;i++)
{
ptr=ptr->next;
}
struct node*q=ptr->next;
ptr->next=q->next;
struct node*r=q->next;
r->prev=ptr;
free(q);
while(z->next!=NULL)
{
z=z->next;
}
Dlinked_list(head,z);
}
}
int main()
{
struct node*head,*ptr,*newnode;
int n,i,k;
head=0;
printf("enter the number nodes to be inserted");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(head==0)
head=ptr=newnode;
else
{
ptr->next=newnode;
newnode->prev=ptr;
ptr=newnode;
}
}
Dlinked_list(head,newnode);
printf("enter the node deletion 1 for begining,2 for ending,3 for position");
scanf("%d",&k);
if(k==1)
deleteB(head);
if(k==2)
deleteE(head);
if(k==3)
deleteP(head,n);
else
printf(".");
}

OUTPUT:
AT BEGINNING:
AT END:

AT GIVEN NODE:
• Search, Count the Number of Nodes and Display
#include<stdio.h>
#include<stdlib.h>
void create();
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL,*temp,*newnode;
void create()
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
if(head==NULL)
{
newnode->next=0;
newnode->prev=0;
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
}
void display()
{
int r=0;
temp=head;
while(temp!=0)
{
r++;
printf("%d\n",temp->data);
temp=temp->next;
}
printf("Total no. of nodes:%d\n",r);
}
int main()
{
int n,h,c,k=0;
printf("Enter no. of nodes :\n");
scanf("%d",&n);
printf("Enter the elements of node:\n");
for(int i=0;i<n;i++)
create();
printf("The double linked list :\n");
display();
printf("The element to be searched:\n");
scanf("%d",&c);

temp=head;
while(temp!=0)
{
k+=1;
if (temp->data==c)
{
printf("Element found at position:%d",k);
break;
}
temp=temp->next;
}
if(temp==0)
printf("Element is not found");
}

OUTPUT:
Create a Circular singly linked list for adding and
deleting a Node.
ADDING:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node*next;
int data;
};
void Clinked_list(struct node*head)
{
struct node*ptr=head;
while(ptr->next!=head)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n%d",ptr->data);
}

void CinsertB(struct node*head,struct node*newnode)


{
struct node*ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
newnode->next=head;
head=newnode;
ptr->next=head;
Clinked_list(head);
}
void CinsertE(struct node*head,struct node*newnode)
{
struct node*ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
ptr->next=newnode;
newnode->next=head;
Clinked_list(head);
}
int main()
{
struct node*head,*ptr,*newnode;
head=0;
int n,i,k;
printf("enter the value to be inserted");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==0)
head=ptr=newnode;
else
{
ptr->next=newnode;
ptr=newnode;
}
ptr->next=head;
}
Clinked_list(head);
printf("\n");
printf("enter the newnode to be inserted");
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
printf("enter the number for insertion 1-beginning,2-end");
scanf("%d",&k);
if(k==1)
CinsertB(head,newnode);
if(k==2)
CinsertE(head,newnode);
else
printf(" ");
}

OUTPUT:
DELETING:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node*next;
int data;
};
void Clinked_list(struct node*head)
{
struct node*ptr=head;
while(ptr->next!=head)
{
printf("\n%d",ptr->data);
ptr=ptr->next;
}
printf("\n%d",ptr->data);
}
void CdeleteB(struct node*head)
{
struct node*ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
struct node*p=head;
head=head->next;
ptr->next=head;
free(p);
Clinked_list(head);
}
void CdeleteE(struct node*head)
{
struct node*p=head;
struct node*q=head->next;
while(q->next!=head)
{
q=q->next;
p=p->next;
}
p->next=head;
free(q);
Clinked_list(head);
}

int main()
{
struct node*head,*ptr,*newnode;
head=0;
int n,i,k;
printf("enter the value to be inserted");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==0)
head=ptr=newnode;
else
{
ptr->next=newnode;
ptr=newnode;
}
ptr->next=head;
}
Clinked_list(head);
printf("\n");
printf("enter the element to be deleted at 1-beginning,2-ending");
scanf("%d",&k);
if(k==1)
CdeleteB(head);
if(k==2)
CdeleteE(head);
else
{
printf("invalid");
}
}

OUTPUT:

You might also like