-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSLLwithHead-InsertItemAtSpecifiedPosition.c
More file actions
66 lines (58 loc) · 1.69 KB
/
CSLLwithHead-InsertItemAtSpecifiedPosition.c
File metadata and controls
66 lines (58 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{ NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL){
printf("out of memory\n");
exit(0);}
return x;
}
void freenode(NODE x)
{
free(x);
}
NODE insert_spec_pos(NODE head,int pos,int item){ int c=1;NODE temp,prev,cur; temp=getnode(); temp->info=item;temp->link=NULL; if(head->link==head && pos==1){ head->link=temp;temp->link=head;return head;} cur=head->link; prev=head; while(c!=pos && cur!=head){ cur=cur->link; prev=prev->link; c++;} if(cur==head){ printf("Insertion at position %d not possible\n",pos);return head;}
if(c==pos){ prev->link=temp;temp->link=cur;return head;}}
void display(NODE head)
{
NODE temp=head;
if (head->link==head){ printf("List is empty\n"); return;}
printf("contents of List:\n");
temp=head->link;
while(temp!=head){
printf("%d ",temp->info);
temp=temp->link;}
printf("\n");
}
int main()
{
int choice,item,pos;
NODE head;
head=getnode();
head->link=head;
while(1)
{
printf("Enter 1 to insert item\nEnter 2 to display\nEnter any other key to exit\n");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter item to be inserted\n");
scanf("%d",&item);
printf("Enter position where %d should be inserted\n",item);
scanf("%d",&pos);
head=insert_spec_pos(head,pos,item);
break;
case 2: display(head);
break;
default: exit(0);
}
}
}