DS Notes 2
DS Notes 2
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;
};
• 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.
Now, the next node at the left should point to the new node.
start = NULL;
int key, opt, pos;
char ch;
do{
/* 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***********************************");
switch(opt){
} //End of switch
/* 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;
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;
/* 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;
}
if(count == pos){
temp>next = t;
prev>next = temp;
ret = 1;
}
else{
printf("\n Wrong input..");
ret = 0;
}
return ret;
} // End of insert_pos
/* display function */
void display (){
} // 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
***********************************
1. Insert at starting position
2. Insert at last position
***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 3
***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 2
***********************************
1. Insert at starting position
2. Insert at last position
3. Insert at any given position
***********************************
Enter your choice: 3