Question LAB 2: ##
1. Insert new node
2. Insert node at beginning
3. Insert node at any position………
Exercise 2:
Create Linked List
Creating linked list is a more than one step activity. First, create a node using structure and find the location where it has to be inserted. Then input the data and store it in the allocated memory space. Use the pointer part of the node to point another node and insert the node at the end of the previously inserted node.
Exercise 3:
Insert New Node at beginning
After completing exercise 1 you have a newly created linked list. Then take a new node and add it in the beginning of the linked list.
Exercise 4:
Insert New Node at any position
Take a position number to insert a new node. Then take a new node and add it in the specified position number.
Exercise 5:
Delete Node from last position
Delete the last node from the linked list and make the pointer part NULL of the previous node of the last node
Exercise 6:
Delete Node from beginning
Delete the first node.
Exercise 7:
Delete Node from any position
Take a position number to delete a node from that position number.
Exercise 8:
Reverse Linked list
You have to reverse the linked list in following format.
Exercise 9:
Remove Duplicate data from Linked list
You have to remove all duplicate entry from your linked list. For this you have to create a list of data containing duplicate data and after executing, your program will provide a list, by removing all duplicate data.
Example:
Input: 1 2 1 1 2 3
Output: 1 2 3
Solution:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_at_beginning();
void insert_at_end();
void insert_at_any_position();
void delete_at_beginning();
void delete_at_end();
void delete_at_any_position();
struct node{
int val;
struct node *next, *prev;
};
struct node *curr, *tail=NULL, *head=NULL;
int main(){
for(;;){
printf("\n.............MENU..............\n");
printf("...............................\n");
printf("1. Crate\n");
printf("2. Display\n");
printf("3. Insert at the beginning\n");
printf("4. Insert at the end\n");
printf("5. Insert at the any position\n");
printf("6. delete at the beginning\n");
printf("7. delete at the end\n");
printf("8. delete at any position\n");
printf("7. Exit\n");
printf("...............................\n");
printf("Enter number: ");
int x;
scanf("%d", &x);
switch(x){
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_at_beginning();
break;
case 4:
insert_at_end();
break;
case 5:
insert_at_any_position();
break;
case 6:
delete_at_beginning();
break;
case 7:
delete_at_end();
break;
case 8:
delete_at_any_position();
break;
}
}
}
void create(){
int i, n;
printf("node number: ");
scanf("%d", &n);
printf("value ");
for(i = 0; i < n; i++){
curr = (struct node *) malloc(sizeof(struct node));
scanf("%d", &(curr -> val));
curr -> next = NULL;
curr -> prev = NULL;
if(head == NULL){
head = curr;
tail = curr;
} else{
struct node *temp;
temp = tail;
temp->next = curr;
curr->prev= temp;
tail = curr;
}
}
}
void display(){
struct node *temp = head;
while(temp != NULL){
printf("%d ", temp -> val);
temp = temp -> next;
}
}
insert_at_beginning(){
curr = (struct node *) malloc(sizeof(struct node));
printf("value ");
scanf("%d", &curr -> val);
curr -> next = NULL;
curr ->prev = NULL;
if(head == NULL){
head = curr;
tail = curr;
} else{
struct node *temp;
temp = head;
head = curr;
curr->next = temp;
temp->prev = curr;
}
}
insert_at_end(){
struct node *temp;
curr = (struct node *) malloc(sizeof(struct node));
printf("value ");
scanf("%d", &curr -> val);
curr->next = NULL;
curr->prev = NULL;
if(head == NULL){
head = curr;
tail = curr;
} else{
temp = head;
while(temp-> next!=NULL){
temp = temp -> next;
}
temp -> next = curr;
curr->prev = temp;
tail = curr;
}
}
insert_at_any_position(){
int posi, i;
struct node *temp, *temp1;
curr = (struct node *) malloc(sizeof(struct node));
printf("where ");
scanf("%d", &posi);
printf("value ",posi);
scanf("%d", &curr -> val);
curr -> next = NULL;
curr-> prev = NULL;
if(head == NULL){
head = curr;
tail = curr;
} else{
temp = head;
for(i = 1; i <= posi-1; i++){
temp1 = temp;
temp = temp->next;
}
temp1 -> next = curr;
curr -> prev = temp1;
curr ->next = temp;
temp->prev = curr;
}
}
void delete_at_beginning(){
struct node *temp;
temp = head;
head = head->next;
head->prev = NULL;
free(temp);
}
void delete_at_end(){
struct node *temp;
temp = tail;
tail = tail->prev;
tail->next = NULL;
free(temp);
}
void delete_at_any_position(){
int posi, i, c = 0;
struct node *temp, *temp1, *temp2;
printf("Enter node position which one you want to remove: ");
scanf("%d", &posi);
if(head != NULL){
temp = head;
for(i = 1; i <= posi-1; i++){
temp1 = temp;
temp = temp->next;
c++;
}
if(c!=0){
temp2 = temp->next;
temp1 -> next = temp2;
temp2->prev = temp1;
free(temp);
}
}
}