0% found this document useful (0 votes)
33 views12 pages

Dsa CH 3 Program

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Dsa CH 3 Program

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Insertion at the beginning of the Singly linked lists // Example Algorithm,

Step 1. Create a new node and assign the address to any node say ptr.

Step 2. ASSIGN INFO[PTR] = ITEM

Step 3. IF(HEAD = NULL)

ASSIGN NEXT[PTR] = NULL

ELSE

ASSIGN NEXT[PTR] = HEAD

Step 4. ASSIGN HEAD = PTR

Step 5. EXIT
Example SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct node
{
int data;
struct node *link;
} *head;

void create(int);
void display();
void insert_end(int);
void insert_beg(int);
void insert_pos(int, int);
int delete_node(int);
main()
{
int choice, value, choice1, pos;

head=NULL;
while(1)
{
system ("cls");
printf("1. Create linked list:\n");
printf("2. Display linked list: \n");
printf("3. Insert Node in Linked List \n");
printf("4. Delete node \n");
printf("5. Exit");

printf("\nEnter your choice: ");


scanf("%d", &choice);

switch(choice)
{
case 1: printf("Number of nodes:");
scanf("%d",&value);
create(value);
break;

case 2: display();
break;

case 3: system("cls");
printf("1. Insert node at beginning of list \n");
printf("2. Insert node at specific position \n");
printf("3. Insert node at the End of the List \n");
printf("Enter choice:");
scanf("%d", &choice1);
switch ( choice1 )
{
case 1: printf("Enter value for node :");
scanf("%d", &value);
insert_beg(value);
break;

case 2: printf("Enter value for node:");


scanf("%d", &value);

printf("\nInsert position to insert new node:”);


scanf("%d", &pos);

insert_pos( value, pos) ;


break;

case 3: printf("Insert value dor node: ");


scanf("%d", &value);
insert_end(value);
break;

case 4: break;
} break;

case 4: if(head == NULL)


printf("List is Empty\n");
else
{
printf("Enter the number to delete : ");
scanf("%d",&value);

if(delete_node(value))
{
printf("%d deleted successfully\n",value);
getch();
}
else
{
printf("%d not found in the list\n",value);
getch();
}
}
break;

case 5: exit(1);
default: printf("Invalid Entry");
}

}
void create (int value)
{
system("cls");
struct node *temp;
temp = (struct node *) malloc ( sizeof (struct node));

if ( head == NULL)
{
temp->data = value;
temp->link = NULL;
head = temp;
}

printf("List created");
getch();
}
void display()
{
struct node *ptr;
ptr = head;
system("cls");
if (head == NULL)
{
printf("List is empty");
getch();
return;
}
printf("Linked List: \n");

while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr = ptr->link;
}
printf("End_of_List");
getch();
}
void insert_end (int value)
{
struct node *ptr, *tempnode;
ptr = head;
while(1)
{
if (ptr->link != NULL)
ptr = ptr->link;
else
break; // otherwise break the infinite while loop
}
tempnode = (struct node *) malloc ( sizeof (struct node));
tempnode->data = value;
tempnode->link = NULL;
ptr->link = tempnode;
}

void insert_beg(int value)


{
struct node *tempnode;
tempnode = (struct node *) malloc (sizeof ( struct node) );
tempnode->data = value;
tempnode->link = head;
head = tempnode;
}
void insert_pos(int value, int pos)
{
int i;

struct node *tempnode, *ptr;


ptr = head;

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


{
if (ptr == NULL)
{
printf("Invalid position entered");
getch();
return;
}
ptr = ptr->link;
}

if (ptr->link == NULL)
{
insert_end(value);
}
else
{
tempnode = (struct node *) malloc ( sizeof (struct node) );
tempnode->data = value;
tempnode->link = ptr->link;
ptr->link = tempnode;
}
}
int delete_node(int value)
{
struct node *ptr, *prev;
ptr = head;
while (ptr != NULL)
{
if (ptr->data == value)
{
if (ptr == head)
{
head = ptr->link;
free(ptr);
return 1;
}

else
{
prev->link = ptr->link;
free(ptr);
return 1;
}
}
else
{
prev = ptr;
ptr = ptr->link;
}
}
return 0;
}

Write the sample output

Important Questions

 Define a linked list and explain all the operations performed in


linked list with time complexity.
 Define a linked list as an ADT and explain its basic characteristics.
 What are the advantages of using a linked list as an ADT compared
to Array data structures?
 How do linked lists and arrays differ in terms of memory allocation
and storage efficiency?
 How is memory allocated for nodes in a linked list?
 Linked list  Graphical representation
o Singly  Algorithm
o Doubly  C language program – all Operations
 Python code
o Circular
 Advantages and Disadvantages of each list
o Circular Doubly  Real life applications
 Time Complexity

You might also like