insert_and_delete_beginning.c (#59)
* ADD - insert_at_beginning This is a program to add item at the beginning of linked list . Structure Based Implementation . * Changed FIle Named . File name was changed in accordance with the contribution guidelines . * Changed c++ file to c . The file is updated and named was also changed * Fixed Bug . change the type of main function from void to int . Fixed the edge cases for the deletion .pull/75/head
parent
4d57584f7a
commit
b48084de34
|
@ -0,0 +1,71 @@
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
// Structure Definition
|
||||||
|
|
||||||
|
// 1. Data of Integer type.
|
||||||
|
// 2. Pointer to the Next Node .
|
||||||
|
struct node {
|
||||||
|
int data ;
|
||||||
|
struct node *next ;
|
||||||
|
} ;
|
||||||
|
struct node *head = NULL ;
|
||||||
|
struct node *current = NULL ;
|
||||||
|
|
||||||
|
//Function to Print the Linked List
|
||||||
|
void printList(){
|
||||||
|
printf("\nItem in the Linked List are : ");
|
||||||
|
// Intialize the ptr(pointer) with head location .
|
||||||
|
struct node *ptr = head ;
|
||||||
|
// While Loop until we encounter a Node which is NULL .
|
||||||
|
// NULL , signifies we are the end of the list .
|
||||||
|
while(ptr!=NULL){
|
||||||
|
printf("%d ",ptr->data) ; //Print the value of the Node
|
||||||
|
ptr = ptr->next ; //Increment the ptr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Insertion at the Beginning of the Linked List
|
||||||
|
|
||||||
|
void insertBeg(int data){
|
||||||
|
//Creating a link
|
||||||
|
//Malloc is for space allocation .
|
||||||
|
struct node *link = (struct node*) malloc(sizeof(struct node)) ;
|
||||||
|
link->data = data ;
|
||||||
|
//Point the link's pointer to the current head
|
||||||
|
link->next = head ;
|
||||||
|
//Update the Head to the node we want to insert at the beggining
|
||||||
|
head = link ;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Deletion at the Beginning of the Linked List
|
||||||
|
|
||||||
|
struct node* deleteBeg(){
|
||||||
|
//Case of Underflow (Deletion when list is empty)
|
||||||
|
if(head == NULL) {
|
||||||
|
printf("List is Empty , deletion not possible") ;
|
||||||
|
return NULL ;
|
||||||
|
}
|
||||||
|
//Store head location in a temporary Node .
|
||||||
|
struct node *temp = head ;
|
||||||
|
|
||||||
|
//Update the current Header
|
||||||
|
head = head->next ;
|
||||||
|
|
||||||
|
// returning the deleted Link
|
||||||
|
return temp ;
|
||||||
|
|
||||||
|
}
|
||||||
|
int main(){
|
||||||
|
deleteBeg(); //Case of Underflow
|
||||||
|
insertBeg(10); //Insert 10 at Beginning
|
||||||
|
insertBeg(20); //Insert 20 at Beginning
|
||||||
|
insertBeg(30); //Insert 30 at Beginning
|
||||||
|
insertBeg(40); //Insert 40 at Beginning
|
||||||
|
insertBeg(50); //Insert 50 at Beginning
|
||||||
|
insertBeg(60); //Insert 60 at Beginning
|
||||||
|
printList(); // Prints : 60 50 40 40 20 10
|
||||||
|
deleteBeg(); //Delete First Element
|
||||||
|
printList(); // Prints : 50 40 30 20 10
|
||||||
|
return 0 ;
|
||||||
|
}
|
Loading…
Reference in New Issue