Skip to content

Instantly share code, notes, and snippets.

@sitansusubudhi
Created July 9, 2015 06:27
Show Gist options
  • Save sitansusubudhi/b076c54dcabef33902ab to your computer and use it in GitHub Desktop.
Save sitansusubudhi/b076c54dcabef33902ab to your computer and use it in GitHub Desktop.
#include<stdlib.h>
#include<stdio.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head =NULL;
void Insert(int data){
struct Node* temp1 = (Node*)malloc(sizeof(struct Node));
temp1->data = data;
temp1->next = NULL;
if(head== NULL){
head= temp1;
}
else{
struct Node* temp2 = head;
while((temp2->next)!= NULL){
temp2 = temp2->next;
}
temp2->next = temp1;
}
}
void Print(){
Node* temp = head;
while(temp!= NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void Delete(int n){
struct Node* temp1 = head;
if(n == 1){
head = temp1->next;
free(temp1);
return;
}
int i;
for(i=0;i<n-2;i++)
temp1 = temp1->next;
struct Node* temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
//delete temp2; for new operator
}
int main(){
int n;
Insert(2);
Insert(4);
Insert(6);
Insert(5);
Print();
printf("Enter the position to delete: ");
scanf("%d",&n);
Delete(n);
Print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment