0% found this document useful (0 votes)
28 views

Linked List Programs Assignment

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Linked List Programs Assignment

Uploaded by

Komal Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

LINKED LIST PROGRAMS ASSIGNMENT 4 & 5

a) Implement a list library (singlylist.h) for a singly linked list with the above
six operations. Write a menu driven driver program to call the operations.

#include <stdio.h>

#include <stdlib.h>

void create();

void insert_at_beg();

void insert_at_end();

void insert_after_pos();

void del();

void search();

void display();

struct node

int info;

struct node *link;

}*start=NULL;

int data,item,n1,pos,i,m;

int main()

int n;

setbuf(stdout, NULL);
printf("\n****Linked List*****\n");

printf("\n1.Create\n2.Insert at Beginning\n3.Insert at End\n4.Insert

After Position\n5.Delete\n6.Search\n7.Display\n8.Exit\n");

while(1)

printf("\nEnter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at

End 4.Insert after Pos. 5.Delete 6.Search 7.Display 8.Exit)\n");

scanf("%d",&n);

switch(n)

case 1:

create();

break;

case 2:

insert_at_beg();

break;

case 3:

insert_at_end();

break;

case 4:

insert_after_pos();

break;

case 5:
del();

break;

case 6:

search();

break;

case 7:

display();

break;

case 8:

exit(0);

default:

printf("\nWrong Choice !!\n");

return 0;

void create()

struct node *q, *tmp;

printf("Enter element :\n");

scanf("%d",&data);

tmp=malloc(sizeof(struct node));

tmp->info=data;
tmp->link=NULL;

if(start==NULL)

start=tmp;

else

{ q=start;

while(q->link!=NULL)

q=q->link;

q->link=tmp;

void insert_at_beg()

struct node *tmp;

printf("\nEnter the element to be inserted :\n");

scanf("%d",&data);

tmp=malloc(sizeof(struct node));

tmp->info=data;

tmp->link=start;

start=tmp;

display();

void insert_at_end()

{
struct node *tmp,*q;

printf("\nEnter the element to be inserted :\n");

scanf("%d",&data);

tmp=malloc(sizeof(struct node));

tmp->info=data;

tmp->link=NULL;

if(start==NULL)

start=tmp;

else

q=start;

while(q->link!=NULL)

q=q->link;

q->link=tmp;

display();

void insert_after_pos()

display();

struct node *q,*tmp;

int index;

tmp=malloc(sizeof(struct node));
printf("\nEnter the element to be inserted :\n");

scanf("%d",&data);

tmp->info=data;

tmp->link=NULL;

if(start==NULL)

start=tmp;

else

printf("Enter index after which element to be inserted :\n");

scanf("%d",&index);

q=start;

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

q = q->link;

if(q==NULL)

printf("There are less elements\n");

return;

tmp->link = q->link;
q->link = tmp;

display();

void del()

struct node *q,*tmp;

printf("Enter the element to be deleted :\n");

scanf("%d",&data);

if(start->info==data) //deletion of first node

tmp=start;

start=start->link;

free(tmp);

display();

return;

q=start;

while(q->link->link!=NULL) //deletion middle node

if(q->link->info==data)

tmp=q->link;
q->link=tmp->link;

free(tmp);

display();

return;

q=q->link;

if(q->link->info==data) //deletion of last node

tmp=q->link;

q->link=NULL;

free(tmp);

display();

return;

printf("\nElement not found \n");

void search()

struct node *tmp;

int i=0;

printf("\nEnter the element to be searched :");

scanf("%d",&item);
tmp=start;

while(tmp!=NULL)

if(tmp->info==item)

printf("Element found at index: %d\n",i);

return;

tmp=tmp->link;

i++;

if(tmp->link==NULL)

printf("Element not found \n");

void display()

struct node *q;

if(start==NULL)

printf("List is empty!!\n");

else

printf("**** Elements in Linked List ****\n");

q=start;
while(q!=NULL)

printf("%d\t",q->info);

q=q->link;

b) Implement a list library (singlylist.h) for a singly linked list. Create a linked
list, reverse it and display reversed linked list.

#include <stdio.h>

#include <stdlib.h>

struct node

int num; //Data of the node

struct node *nextptr; //Address of the node

}*stnode;

void createNodeList(int n); //function to create the list

void reverseDispList(); //function to convert the list in reverse

void displayList(); //function to display the list

int main()

{
int n;

printf("\n\n Linked List : Create a singly linked list and print it in


reverse order :\n");

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

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

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list are : \n");

displayList();

reverseDispList();

printf("\n The list in reverse 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) //check whether the stnode is NULL and if so no memory


allocation
{

printf(" Memory can not be allocated.");

else

// reads data for the node through keyboard

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL; //Links the address field to NULL

tmp = stnode;

//Creates n nodes and adds to linked list

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

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

if(fnNode == NULL) //check whether the fnnode is NULL and if so no


memory allocation

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);


scanf(" %d", &num);

fnNode->num = num; // links the num field of fnNode with num

fnNode->nextptr = NULL; // links the address field of fnNode with


NULL

tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode

tmp = tmp->nextptr;

void reverseDispList()

struct node *prevNode, *curNode;

if(stnode != NULL)

prevNode = stnode;

curNode = stnode->nextptr;

stnode = stnode->nextptr;

prevNode->nextptr = NULL; //convert the first node as last

while(stnode != NULL)
{

stnode = stnode->nextptr;

curNode->nextptr = prevNode;

prevNode = curNode;

curNode = stnode;

stnode = prevNode; //convert the last node as head

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); // prints the data of current node

tmp = tmp->nextptr; // advances the position of current node

a) Implement a list library (doublylist.h) for a doubly linked list with the
above four operations. Write a menu driven driver program to call the
operationsappend, insert, delete specific node, delete at position, search,
display.

#include <stdio.h>

#include <stdlib.h>

// Node Structure of the linked list

struct node {

int data;

struct node *prev, *next;

};

struct node* start = NULL;

// Function to traverse and print the linked list

void traverse(){

// List is empty

// just return

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

return;

// Else print the Node's Data

struct node* temp;

temp = start;

while (temp != NULL) {

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

temp = temp->next;

// function to insert node 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->data = data;

temp->prev = NULL;
// Pointer of temp will be assigned to start

temp->next = start;

start = temp;

// 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->data = 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;

// Function to insert at any given 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);

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

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

temp = start;

// If start==NULL,

if (start == NULL) {

start = newnode;

newnode->prev = NULL;

newnode->next = NULL;

// If position==1,

else if (pos == 1) {

newnode->next = start;

newnode->next->prev = newnode;

newnode->prev = NULL;

start = newnode;

// Change links

else {

while (i < pos - 1) {

temp = temp->next;

i++;
}

newnode->next = temp->next;

newnode->prev = temp;

temp->next = newnode;

temp->next->prev = newnode;

// 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);

// 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);

// function to delete from any given

// 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) {

position = start;

start = start->next;

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);

int main(){

int choice;

while (1) {

printf("\n\t1 To see list\n");

printf("\t2 For insertion at"

" starting\n");

printf("\t3 For insertion at"

" end\n");
printf("\t4 For insertion at "

"any position\n");

printf("\t5 For deletion of "

"first element\n");

printf("\t6 For deletion of "

"last element\n");

printf("\t7 For deletion of "

"element at any position\n");

printf("\t8 To 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;

}
OR

#include <stdio.h>

#include <stdlib.h>

void create();

void insert_at_beg();

void insert_at_end();

void insert_after_pos();

void del();

void search();

void display();

struct node {

int info;

struct node* next;

struct node* prev;

}*start=NULL;

int data,item,n1,pos,i,m;

int main()

int n;

setbuf(stdout, NULL);

printf("\n****Doubly Linked List*****\n");

printf("\n1.Create\n2.Insert at Beginning\n3.Insert at

End\n4.Insert After Position\n5.Delete\n6.Display\n7.Exit\n");


while(1)

printf("\nEnter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End

4.Insert after Pos. 5.Delete 6.Display 7.Exit)\n");

scanf("%d",&n);

switch(n)

case 1:

create();

break;

case 2:

insert_at_beg();

break;

case 3:

insert_at_end();

break;

case 4:

insert_after_pos();

break;

case 5:

del();

break;

case 6:
display();

break;

case 7:

exit(0);

default:

printf("\nWrong Choice !!\n");

return 0;

void create()

int data;

struct node *tmp;

printf("\nEnter the first element to be inserted :\n");

scanf("%d",&data);

tmp=malloc(sizeof(struct node));

tmp->info=data;

tmp->prev=NULL;

tmp->next=NULL;

if(start == NULL)

start = tmp;

display();
}

void insert_at_beg()

int data;

struct node *tmp;

printf("\nEnter the element to be inserted :\n");

scanf("%d",&data);

tmp=malloc(sizeof(struct node));

tmp->info=data;

tmp->prev=NULL;

tmp->next=NULL;

if(start == NULL)

start = tmp;

else

start->prev = tmp;

tmp->next = start;

start = tmp;

display();

void insert_at_end()

{
int data;

struct node *q,*tmp;

printf("\nEnter the element to be inserted :\n");

scanf("%d",&data);

tmp=malloc(sizeof(struct node));

tmp->info=data;

tmp->prev=NULL;

tmp->next=NULL;

if(start == NULL)

start = tmp;

else

q=start;

while(q->next != NULL)

q = q->next; // Go To last Node

q->next = tmp;

tmp->prev = q;

display();

void insert_after_pos()

int data;
struct node *q,*tmp;

tmp=malloc(sizeof(struct node));

printf("\nEnter the element to be inserted :\n");

scanf("%d",&data);

tmp->info=data;

tmp->prev=NULL;

tmp->next=NULL;

if(start==NULL)

start=tmp;

else

printf("Enter index after which element to be inserted :\n");

scanf("%d",&pos);

q=start;

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

q = q->next;

tmp->next = q->next;

if(q->next!=NULL)

{
q->next->prev=tmp;

q->next = tmp;

tmp->prev=q;

display();

void del()

struct node *tmp,*q,*prev;

printf("Enter the element to be deleted :\n");

scanf("%d",&data);

if(start->info==data) //deletion of first node

tmp=start;

if(tmp->next!=NULL)

start->next->prev=NULL;

start=start->next;

free(tmp);

display();

return;
}

q=start;

while(q->next->next!=NULL) //deletion of middle node

if(q->next->info==data)

prev=q->next->prev;

tmp=q->next;

q->next=tmp->next;

q->next->prev=prev;

free(tmp);

display();

return;

q=q->next;

if(q->next->info==data) //deletion at end

tmp=q->next;

q->next=NULL;

free(tmp);

display();

return;
}

printf("\nElement not found \n");

void display()

{ struct node *q;

if(start==NULL)

printf("List is empty!!\n");

else

printf("**** Elements in Doubly Linked List ****\n");

q=start;

while(q!=NULL)

printf("%d\t",q->info);

q=q->next;

b) Implement a list library (doublylist.h) for a doubly linked list. Create a


linked list, reverse it and display reversed 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 displayDlListRev();

int main()

int n;

stnode = NULL;

ennode = NULL;

printf("\n\n Doubly Linked List : Create and display a doubly linked list in
reverse order :\n");

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

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

scanf("%d", &n);

DlListcreation(n);

displayDlListRev();
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;

// putting data for rest of the nodes

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; // new node is linking with the


previous node

fnNode->nextptr = NULL;

ennode->nextptr = fnNode; // previous node is linking with the


new node

ennode = fnNode; // assign new node as last node

else

printf(" Memory can not be allocated.");

break;

else

{
printf(" Memory can not be allocated.");

void displayDlListRev()

struct node * tmp;

int n = 0;

if(ennode == NULL)

printf(" No data found in the List yet.");

else

tmp = ennode;

printf("\n Data in reverse order are :\n");

while(tmp != NULL)

printf(" Data in node %d : %d\n", n+1, tmp->num);

n++;

tmp = tmp->preptr; // current pointer set with previous node


}

You might also like