Skip to content

Commit 7eddec4

Browse files
committed
all
1 parent d0869b7 commit 7eddec4

27 files changed

+4842
-0
lines changed

01insertFirst.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
struct node{
4+
int data;
5+
struct node *next;
6+
};
7+
int insertFirst(struct node **head,int no);
8+
void displayLL(struct node**);
9+
int main(){
10+
struct node *head=NULL;
11+
int itr1,num,no;
12+
printf("Enter number of nodes to be created.\n");
13+
scanf("%d",&num);
14+
for(itr1=1;itr1<=num;itr1++){
15+
printf("Enter data of node %d.\n",itr1);
16+
scanf(" %d",&no);
17+
insertFirst(&head,no);
18+
}
19+
displayLL(&head);
20+
return 0;
21+
}
22+
int insertFirst(struct node **head,int no){
23+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
24+
newNode->data=no;
25+
if(*head==NULL){
26+
*head=newNode;
27+
newNode->next=NULL;
28+
}else{
29+
newNode->next=*head;
30+
*head=newNode;
31+
}
32+
return 0;
33+
}
34+
void displayLL(struct node **head){
35+
struct node *temp=*head;
36+
while(temp->next!=NULL){
37+
printf("|%d|->",temp->data);
38+
temp=temp->next;
39+
}
40+
printf("|%d|\n",temp->data);
41+
}

02insertLast.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
struct node{
4+
int data;
5+
struct node *next;
6+
};
7+
int insertLast(struct node **head,int no);
8+
void displayLL(struct node**);
9+
int main(){
10+
struct node *head=NULL;
11+
int itr1,num,no;
12+
printf("Enter number of nodes to be created.\n");
13+
scanf("%d",&num);
14+
for(itr1=1;itr1<=num;itr1++){
15+
printf("Enter data of node %d.\n",itr1);
16+
scanf(" %d",&no);
17+
insertLast(&head,no);
18+
}
19+
displayLL(&head);
20+
return 0;
21+
}
22+
int insertLast(struct node **head,int no){
23+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
24+
newNode->data=no;
25+
newNode->next=NULL;
26+
if(*head==NULL){
27+
*head=newNode;
28+
newNode->next=NULL;
29+
}else{
30+
struct node *temp=*head;
31+
while(temp->next!=NULL){
32+
temp=temp->next;
33+
}
34+
temp->next=newNode;
35+
}
36+
return 0;
37+
}
38+
void displayLL(struct node **head){
39+
struct node *temp=*head;
40+
while(temp->next!=NULL){
41+
printf("|%d|->",temp->data);
42+
temp=temp->next;
43+
}
44+
printf("|%d|\n",temp->data);
45+
}

03insertAtPos.c

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
struct node{
4+
int data;
5+
struct node *next;
6+
};
7+
int count(struct node **head);
8+
int insertLast(struct node **head,int no);
9+
int insertFirst(struct node **head,int no);
10+
int insertAtPos(struct node **head,int no,int pos);
11+
void displayLL(struct node**);
12+
int main(){
13+
struct node *head=NULL;
14+
int itr1,num,no,pos;
15+
printf("Enter number of nodes to be created.\n");
16+
scanf("%d",&num);
17+
for(itr1=1;itr1<=num;itr1++){
18+
printf("Enter data of node %d.\n",itr1);
19+
scanf(" %d",&no);
20+
insertLast(&head,no);
21+
}
22+
displayLL(&head);
23+
printf("Enter position of newNode\n");
24+
scanf(" %d",&pos);
25+
printf("Enter data of pos node.\n");
26+
scanf(" %d",&no);
27+
insertAtPos(&head,no,pos);
28+
displayLL(&head);
29+
return 0;
30+
}
31+
int count(struct node **head){
32+
int cnt=1;
33+
struct node *temp=*head;
34+
while(temp->next!=NULL){
35+
temp=temp->next;
36+
cnt++;
37+
}
38+
return cnt;
39+
}
40+
int insertLast(struct node **head,int no){
41+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
42+
newNode->data=no;
43+
newNode->next=NULL;
44+
if(*head==NULL){
45+
*head=newNode;
46+
newNode->next=NULL;
47+
}else{
48+
struct node *temp=*head;
49+
while(temp->next!=NULL){
50+
temp=temp->next;
51+
}
52+
temp->next=newNode;
53+
}
54+
return 0;
55+
}
56+
int insertFirst(struct node **head,int no){
57+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
58+
newNode->data=no;
59+
if(*head==NULL){
60+
*head=newNode;
61+
newNode->next=NULL;
62+
}else{
63+
newNode->next=*head;
64+
*head=newNode;
65+
}
66+
return 0;
67+
}
68+
int insertAtPos(struct node **head,int no,int pos){
69+
int cnt=0;
70+
struct node *temp=*head;
71+
cnt=count(head);
72+
if(pos==1){
73+
insertFirst(head,no);
74+
}else if(cnt+1==pos){
75+
insertLast(head,no);
76+
}else{
77+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
78+
while(pos-1>1){
79+
temp=temp->next;
80+
pos--;
81+
}
82+
newNode->data=no;
83+
newNode->next=temp->next;
84+
temp->next=newNode;
85+
}
86+
return 0;
87+
}
88+
void displayLL(struct node **head){
89+
struct node *temp=*head;
90+
while(temp->next!=NULL){
91+
printf("|%d|->",temp->data);
92+
temp=temp->next;
93+
}
94+
printf("|%d|\n",temp->data);
95+
}

04deleteFirst.c

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
struct node{
4+
int data;
5+
struct node *next;
6+
};
7+
int count(struct node **head);
8+
int insertLast(struct node **head,int no);
9+
int insertFirst(struct node **head,int no);
10+
int deleteFirst(struct node **head);
11+
int insertAtPos(struct node **head,int no,int pos);
12+
void displayLL(struct node**);
13+
int main(){
14+
struct node *head=NULL;
15+
int itr1,num,no,pos;
16+
printf("Enter number of nodes to be created.\n");
17+
scanf("%d",&num);
18+
for(itr1=1;itr1<=num;itr1++){
19+
printf("Enter data of node %d.\n",itr1);
20+
scanf(" %d",&no);
21+
insertLast(&head,no);
22+
}
23+
displayLL(&head);
24+
printf("Enter position of newNode\n");
25+
scanf(" %d",&pos);
26+
printf("Enter data of pos node.\n");
27+
scanf(" %d",&no);
28+
insertAtPos(&head,no,pos);
29+
displayLL(&head);
30+
deleteFirst(&head);
31+
displayLL(&head);
32+
return 0;
33+
}
34+
int deleteFirst(struct node **head){
35+
struct node *temp=*head;
36+
if(*head==NULL){
37+
printf("nothing to delete.\n");
38+
}else if((*head)->next==NULL){
39+
free(temp);
40+
*head=NULL;
41+
}else{
42+
*head=temp->next;
43+
free(temp);
44+
}
45+
return 0;
46+
}
47+
int count(struct node **head){
48+
int cnt=1;
49+
struct node *temp=*head;
50+
while(temp->next!=NULL){
51+
temp=temp->next;
52+
cnt++;
53+
}
54+
return cnt;
55+
}
56+
int insertLast(struct node **head,int no){
57+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
58+
newNode->data=no;
59+
newNode->next=NULL;
60+
if(*head==NULL){
61+
*head=newNode;
62+
newNode->next=NULL;
63+
}else{
64+
struct node *temp=*head;
65+
while(temp->next!=NULL){
66+
temp=temp->next;
67+
}
68+
temp->next=newNode;
69+
}
70+
return 0;
71+
}
72+
int insertFirst(struct node **head,int no){
73+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
74+
newNode->data=no;
75+
if(*head==NULL){
76+
*head=newNode;
77+
newNode->next=NULL;
78+
}else{
79+
newNode->next=*head;
80+
*head=newNode;
81+
}
82+
return 0;
83+
}
84+
int insertAtPos(struct node **head,int no,int pos){
85+
int cnt=0;
86+
struct node *temp=*head;
87+
cnt=count(head);
88+
if(pos==1){
89+
insertFirst(head,no);
90+
}else if(cnt+1==pos){
91+
insertLast(head,no);
92+
}else{
93+
struct node *newNode=(struct node*)malloc(sizeof(struct node));
94+
while(pos-1>1){
95+
temp=temp->next;
96+
pos--;
97+
}
98+
newNode->data=no;
99+
newNode->next=temp->next;
100+
temp->next=newNode;
101+
}
102+
return 0;
103+
}
104+
void displayLL(struct node **head){
105+
struct node *temp=*head;
106+
while(temp->next!=NULL){
107+
printf("|%d|->",temp->data);
108+
temp=temp->next;
109+
}
110+
printf("|%d|\n",temp->data);
111+
}

0 commit comments

Comments
 (0)