0% found this document useful (0 votes)
4 views10 pages

DS Notes 2

The document provides detailed notes on linear data structures, specifically linked lists, including their types (simple, doubly, and circular linked lists) and basic operations such as insertion, deletion, display, and search. It includes algorithms for inserting nodes at various positions within a linked list and a C program demonstrating these operations. The notes are intended for B.Tech. CSE 4th semester students and are authored by Shipra Swati from PSCET, Bihar.

Uploaded by

Ghana Uni Help
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)
4 views10 pages

DS Notes 2

The document provides detailed notes on linear data structures, specifically linked lists, including their types (simple, doubly, and circular linked lists) and basic operations such as insertion, deletion, display, and search. It includes algorithms for inserting nodes at various positions within a linked list and a C program demonstrating these operations. The notes are intended for B.Tech. CSE 4th semester students and are authored by Shipra Swati from PSCET, Bihar.

Uploaded by

Ghana Uni Help
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/ 10

DS Notes Class: B.Tech.

CSE 4th Sem

Unit III
[Linear Data Structures and their Linked Representation: Linked linear list, circularly linked linear
list, Doubly linked list, linked stack, linked queue and their operations and applications.]

Linked List: Linked list is a linear dynamic data structure containing collection of homogeneous
nodes, which are connected together via links.

As per the above diagram, following are the important points to be considered:
• Linked List contains a link element (pointer) called head.
• Each node carries a data field(s) and a link field (pointer) called next.
• Each node is linked with its next link using its next field.
• Last node carries a link as null to mark the end of the list.
In C this type of node can be defined using a structure, such as:

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

Types of Linked List:


• Simple Linked List − Item navigation is forward only.

• Doubly Linked List − Items can be navigated forward and backward.

• Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.

Basic Operation:
• Insertion − Adds an element at the beginning of the list.

• Deletion − Deletes an element at the beginning of the list.

• Display − Displays the complete list.

• Search − Searches an element using the given key.

• Delete − Deletes an element using the given key.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Simple Linked List


1. Insertion Opertation: Consists of following steps
1. First, create a node using the same structure
2. find the location where it has to be inserted (first, last or after n th elements).
Example:
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode).

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

Then point B.next to C.

NewNode.next −> RightNode;

Now, the next node at the left should point to the new node.

LeftNode.next −> NewNode;

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

The new list should look like this −

Algorithm for inserting a node at 1st position:


Declare node *shead = NULL
Step 1. ALLOCATE MEMORY FOR newnode
Step 2. newnode -> ADDRESS = NULL
Step 3. newnode -> DATA = element
Step 4. IF shead == NULL
Step 5. shead = newnode
Step 6. END OF IF
Step 7. ELSE
Step 8. newnode -> ADDRESS = shead
Step 9. shead = newnode
Step 10. END OF ELSE

Algorithm for inserting a node at last position:


Declare node *shead = NULL and node *temp = NULL
Step 1. ALLOCATE MEMORY FOR newnode
Step 2. newnode -> ADDRESS = NULL
Step 3. newnode -> DATA = element
Step 4. IF shead == NULL
Step 5. shead = newnode
Step 6. END OF IF
Step 7. ELSE
Step 8. temp = shead
Step 9. WHILE temp -> ADDRESS ! = NULL
Step 10. temp = temp -> ADDRESS
Step 11. END OF WHILE
Step 12. temp -> ADDRESS = newnode
Step 13. END OF ELSE

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Algorithm for inserting a node at a given position:


Declare node *shead = NULL, node *temp = NULL, node *prev = NULL
Declare pos, the place where user wants to insert new node.
Step 1. count = 1
Step 2. ALLOCATE MEMORY FOR newnode
Step 3. newnode -> DATA = element
Step 4. newnode -> ADDRESS = NULL
Step 5. IF pos <=0 OR (pos > 1 AND shead=NULL)
Step 6. PRINT “Wrong input for position”
Step 7. END OF IF
Step 8. ELSE
Step 9. IF pos == 1
Step 10. newnode -> ADDRESS = shead
Step 11. shead = newnode
Step 12. END OF IF
Step 13. ELSE
Step 14. temp = shead
Step 15. WHILE count < pos AND (temp ->ADDRESS) != NULL
Step 16. prev = temp
Step 17. temp = temp ->ADDRESS
Step 18. count = count+1
Step 19. END OF WHILE
Step 20. IF count == pos
Step 21. newnode -> ADDRESS = temp
Step 22. prev ->ADDRESS = newnode
Step 23. END OF IF
Step 24. ELSE
Step 25. PRINT “Wrong input for position”
Step 26. END OF ELSE
Step 27. END OF ELSE
Step 28. END OF ELSE

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

C Program for Linked list insertion:


/* Program of node insertion in Linked List*/
#include<stdlib.h>

/* Creating node structure for linked list */


struct node{
int data;
struct node *next;
};
struct node *start, *temp;
void main(){

start = NULL;
int key, opt, pos;
char ch;

do{

printf("\nEnter the element you want to insert:\t ");


scanf("%d", &key);

/* displaying menu */
printf("\n***********************************");
printf("\n1. Insert at starting position ");
printf("\n2. Insert at last position ");
printf("\n3. Insert at any given position");
printf("\n***********************************");

printf("\n Enter your choice:\t");


scanf("%d", &opt);

switch(opt){

case 1: /* insert at starting */


insert_first(key);
printf("\n After adding in the starting,");
break;

case 2: /* insert at last */


insert_last(key);
printf("\n After adding in the end,");
break;

case 3: /* insert at a given position */


printf("\nEnter the position, where you want to
enter the elements..");
scanf("%d", &pos);
int ret = insert_pos(key, pos);
if(ret == 1)

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

printf("After adding %d position,",pos);


break;

default: /* No choice matched */


printf("\nWrong choice..");

} //End of switch

/* printing the list */


display();

printf("\nDo you want more (Y / N ):\t");


getchar();
scanf("%c", &ch);
}while(ch=='y'); // End of do­while

/* Insert at starting */
void insert_first(int key){
/* new node is created */
temp = (struct node*) malloc(sizeof(struct node));
temp­>data = key;
temp­>next = NULL;

/* check if the list is empty */


if (start == NULL) {
printf("\nfirst time insertion..");
start = temp;
return;
}

temp­>next = start;
start = temp;
return;
} // End of insert_first()

/* Insert at last */
void insert_last (int key){
/* new node is created */
temp = (struct node*) malloc(sizeof(struct node));
temp­>data = key;
temp­>next = NULL;

/* check if the list is empty */


if (start == NULL) {
printf("\nfirst time insertion..");
start = temp;
return;

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

struct node *t = start;


/* Loop through the list till the last element */
while(t­>next != NULL){
t = t­>next;
}
t­>next = temp;
return;
} // End of insert_last

/* inserting at given position */


int insert_pos(int key, int pos){

int count = 1, ret = 0;


struct node *prev;
/* new node is created */
temp = (struct node*) malloc(sizeof(struct node));
temp­>data = key;
temp­>next = NULL;

/* Wrong choice */
if(pos <=0 || (pos > 1 && start==NULL)){
printf("\nWrong input for position..\n");
ret = 0;
}

/* pos =1 */
if(pos == 1){
temp­>next = start;
start = temp;
ret = 1;
}

struct node *t = start;


while ((count < pos) && ((t­>next) != NULL)){
prev = t;
t = t­>next;
count = count + 1;
}

if(count == pos){
temp­>next = t;
prev­>next = temp;
ret = 1;
}
else{
printf("\n Wrong input..");
ret = 0;

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

}
return ret;
} // End of insert_pos

/* display function */
void display (){

printf(" List details:\n data \t\t Current Address \t Next


Address");
printf(" \n
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­\n
");
temp = start;
while(temp­>next != NULL){
printf(" %d \t\t %u \t\t %u \n", temp­>data, temp, temp­>next);
temp=temp­>next;
}
printf(" %d \t\t %u \t\t %u \n", temp­>data, temp, temp­>next);

} // End of display

Output:
Enter the element you want to insert: 4

***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 2

first time insertion..


After adding in the end, List details:
data Current Address Next Address
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­
4 135499784 0

Do you want more (Y / N ): y

Enter the element you want to insert: 6

***********************************
1. Insert at starting position
2. Insert at last position

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

3. Insert at any given position


***********************************
Enter your choice: 1

After adding in the starting, List details:


data Current Address Next Address
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­
6 135499800 135499784
4 135499784 0

Do you want more (Y / N ): y

Enter the element you want to insert: 8

***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 3

Enter the position, where you want to enter the elements..2


After adding 2 position, List details:
data Current Address Next Address
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­
6 135499800 135499816
8 135499816 135499784
4 135499784 0

Do you want more (Y / N ): y

Enter the element you want to insert: 2

***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 2

After adding in the end, List details:


data Current Address Next Address
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­
6 135499800 135499816
8 135499816 135499784
4 135499784 135499832
2 135499832 0

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Do you want more (Y / N ): y

Enter the element you want to insert: 5

***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 3

Enter the position, where you want to enter the elements..4


After adding 4 position, List details:
data Current Address Next Address
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
­
6 135499800 135499816
8 135499816 135499784
4 135499784 135499848
5 135499848 135499832
2 135499832 0

Do you want more (Y / N ): n

Provided By: Shipra Swati, PSCET, Bihar

You might also like