Live Webinar: Prompt Engineering: Skill Everyone Must Learn Today
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Delete at Nth Position in Singly Linked List
WhatsApp
Kaushik S
Nov 28
2015
2.7
k
0
0
#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
Up Next
Delete at Nth Position in Singly Linked List