You are on page 1of 23

LINKED LIST

Node creation for sll


• Creating the structure of a node in C program using pointers
• struct node {
• int data;
• struct node *next;
• };
• struct node *head, *newNode;
• A node is dynamically allocated
• newNode = (struct node *)malloc(sizeof(struct node *));
Insert at the beginning
• Creating a new node
• struct Node *newNode;
• newNode = malloc(sizeof(Node));
• If it’s the first node set it to point header
• head=newNode;
• head->next = NULL;
• If already elements are there in the list
• newNode->next = head;
• head=newNode;
INSERT AT THE BEGINNING
• void beginsert() {
• struct node *newNode;
• int item
• newNode = (struct node *) malloc(sizeof(struct node));
• if(newNode == NULL) { printf("\nOVERFLOW");}
• else { printf("\nEnter value\n");
• scanf("%d",&item)
• newNode->data = item;
• newNode->next = head;
• head = newNode;
• printf("\nNode inserted");
• }}
Insert at the end
• void lastinsert() {
• struct node *temp;
• int item;
• newNode = (struct node *) malloc(sizeof(struct node));
• if(newNode == NULL) { printf("\nOVERFLOW"); }
• else { printf("\nEnter value?\n");
• scanf("%d", &item);
• newNode->data = item;
• if(head == NULL) {
• newNode->next = NULL;
• head = newNode;
• printf("\nNode inserted"); }
Insert at the end
• else {
• temp = head;
• while(temp->next != NULL) {
• temp = temp->next;
• }
• temp->next = newNode;
• newNode->next = NULL;
• printf("\nNode inserted");
• }
• }
• }
example

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

• Deleting a node from the stack (POP operation)


• Check for the underflow condition: The underflow
condition occurs when we try to pop from an already
empty stack. The stack will be empty if the head pointer
of the list points to null.
• Adjust the head pointer accordingly: In stack, the
elements are popped only from one end, therefore, the
value stored in the head pointer must be deleted and the
node must be freed. The next node of the head node now
becomes the head node.
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;

You might also like