TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Delete at Nth Position in Singly Linked List
Kaushik S
Nov 28
2015
Code
2.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef
struct
node{
int
data;
struct
node* next;
}node;
struct
node* head;
void
insert(
int
x)
{
struct
node* temp=NULL;
temp=(node*)malloc(
sizeof
(
struct
node));
temp->data=x;
if
(head==NULL)
{
temp->next=NULL;
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
void
print()
{
struct
node* temp=head;
while
(temp!=NULL)
{
printf(
"%d"
,temp->data);
printf(
"%s"
,
":"
);
temp=temp->next;
}
}
void
DeleteAtNthPos(
int
x)
{
struct
node* temp=head;
if
(head==NULL)
{
printf(
"Sorry no data to delete"
);
}
else
if
(temp->data==x)
{
head=temp->next;
free(temp);
}
else
{
struct
node* temp2=NULL;
temp2=temp->next;
while
(temp->next->data!=x)
{
temp=temp->next;
temp2=temp->next;
}
temp->next=temp2->next;
free(temp2);
}
printf(
" <>deleted items<>"
);
print();
}
void
main()
{
insert(5);
insert(10);
insert(15);
insert(20);
print();
DeleteAtNthPos(20);
getch();
}
C
Datastructures