Professional Documents
Culture Documents
CH 3linked List
CH 3linked List
temp
Inserting a new node in BETWEEn
• void middleinsert()
• { int i,loc,item;
• struct node *newNode, *temp;
• newNode = (struct node *) malloc (sizeof(struct node));
• if(newNode == NULL) { printf("\nOVERFLOW"); }
• else
• { printf("\nEnter element value");
• scanf("%d",&item);
• newNode->data = item;
• printf("\nEnter the location after which you want to insert ");
• scanf("\n%d",&loc);
Inserting a new node in the middle
• temp=head;
• for(i=0;i<loc;i++) {
• temp = temp->next;
• if(temp == NULL)
• { printf("\ncan't insert\n");
• return; } }
• newNode ->next = temp ->next;
• temp ->next = newNode;
• printf("\nNode inserted");
• }
• }
Delete at the front
•temp=head;
head=temp->next;
free(temp);
Delete at the front
• void delete_front()
• { struct node *temp;
• if(head == NULL)
• { printf("\nList is empty\n"); }
• else
• {
• temp = head;
• head = temp->next;
• free(temp);
• printf("\nNode deleted from the beginning ...\n");
• } }
Delete at the back
Delete at the back
• void delete_last()
• { struct node *temp, *temp1;
• if(head == NULL) { printf("\nlist is empty"); }
• else if(head -> next == NULL)
• { head = NULL; free(head);
• printf("\nOnly node of the list deleted ...\n"); }
• else { temp = head;
• while(temp->next != NULL)
• { temp1 = temp;
• temp = temp ->next; }
• temp1->next = NULL; free(temp);
• printf("\nDeleted Node from the last ...\n"); } }
Delete in the middle
Delete in the middle
• void delete_middle()
• { struct node *temp, *temp1;
• int loc,i;
• printf("\n Enter the location of the node after which you want to perform deletion \n");
• scanf("%d",&loc);
• temp=head;
• for(i=0;i<loc;i++)
• { temp1 = temp;
• temp = temp->next;
• if(temp == NULL)
• { printf("\nCan't delete"); return; } }
• temp1 ->next = temp ->next;
Search an element in sll
• void search()
• { struct node *temp;
• int item,i=0,flag;
• temp = head;
• if(temp == NULL) { printf("\nEmpty List\n"); }
• else { printf("\nEnter item which you want to search?\n");
• scanf("%d",&item);
• while (temp!=NULL) { if(temp->data == item) { printf("item found at location %d ",i+1); flag=0; }
• else { flag=1; } i++; temp = temp -> next; }
• if(flag==1)
• { printf("Item not found\n"); }
• } }
Displaying a sll
• void display()
• { struct node *temp;
• temp = head;
• if(temp == NULL)
• { printf("Nothing to print"); }
• else
• { printf("\nprinting values . . . . .\n");
• while (temp!=NULL)
• {
• printf("\n%d",temp->data);
• temp = temp -> next;
• } } }
APPLICATIONS OF
Linked list implementation of
Stack
• Adding a node to the stack (Push operation)
• Create a node first and allocate memory to it.
• If the list is empty, then the item is to be pushed as the start node of
the list. This includes assigning value to the data part of the node and
assign null to the address part of the node.
• If there are some nodes in the list already, then we have to add the
new element in the beginning of the list (to not violate the property
of the stack). For this purpose, assign the address of the starting
element to the address field of the new node and make the new node,
the starting node of the list.
Linked list implementation of Stack
• void push ()
• { int val;
• struct node *newNode =(struct node*)malloc(sizeof(struct node));
• if(newNode == NULL) { printf("not able to push the element"); }
• else { printf("Enter the value"); scanf("%d",&val);
• if(head==NULL) { newNode->val = val;
• newNode -> next = NULL; head=newNode; }
• else
• { newNode->val = val;
• newNode->next = head;
• head=newNode; }
• printf("Item pushed"); }}
Linked list
implementation of stack
• void pop()
• {
• int item;
• struct node *temp;
• if (head == NULL) { printf("Underflow"); }
• else
• { item = head->val;
• temp = head;
• head = head->next;
• free(temp);
• printf("Item popped");
• } }
Linked list implementation of Queue
• Insertion
• newNode -> data = item;
• if(front == NULL) //
• { front = newNode;
• rear = newNode;
• front -> next = NULL;
• rear -> next = NULL; }
• Else
• rear -> next = newNode;
• rear = newNode;
• rear->next = NULL;