Skip to content

Commit 1422f14

Browse files
authored
Add files via upload
1 parent 35d6601 commit 1422f14

File tree

4 files changed

+297
-0
lines changed

4 files changed

+297
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
struct Node
5+
{
6+
int data;
7+
struct Node* next;
8+
}*first=NULL;
9+
void Create(int A[], int n)
10+
{
11+
int i;
12+
struct Node* last, * t;
13+
first = (struct Node*)malloc(sizeof(struct Node));
14+
first->data = A[0];
15+
first->next = NULL;
16+
last = first;
17+
18+
for (i = 1; i < n; i++)
19+
{
20+
t = (struct Node*)malloc(sizeof(struct Node));
21+
t->data = A[i];
22+
t->next = NULL;
23+
last->next = t;
24+
last = t;
25+
}
26+
}
27+
void Display(struct Node* p)
28+
{
29+
while (p != NULL)
30+
{
31+
printf("->%d", p->data);
32+
p = p->next;
33+
}
34+
}
35+
void RDisplay(struct Node* p)
36+
{
37+
if (p!=NULL)
38+
{
39+
printf("->%d", p->data);
40+
RDisplay(p->next);
41+
}
42+
}
43+
44+
// Remove duplicate from ll ; if nodes data are same del that ; from sorted linkedlist
45+
void Removeduplicate(struct Node* p) // taking pointer to 1st node
46+
{
47+
// inside fxn we want one more pointer
48+
struct Node* q = p->next; // which will before p // q is ahead and p is following
49+
while (q != NULL)
50+
{
51+
if (p->data != q->data) // if data is not matching // means no duplicate
52+
{
53+
p = q; // move ahead
54+
q = q->next;
55+
}
56+
else // if there is duplicate value
57+
{
58+
p->next = q->next; // p next points on q next //q will be logically remove from ll
59+
free(q); // delete q
60+
q = p->next; // and move q to next
61+
}
62+
}
63+
}
64+
65+
66+
67+
68+
int main()
69+
{
70+
int A[] = {10,10,10,10,20,20,20,30,40,50 };
71+
Create(A, sizeof (A)/sizeof (int ));
72+
Removeduplicate(first);
73+
Display(first);
74+
printf("\n\n");
75+
76+
return 0;
77+
78+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct Node
4+
{
5+
int data;
6+
struct Node* next;
7+
}*first=NULL;
8+
void Create(int A[], int n)
9+
{
10+
int i;
11+
struct Node* last, * t;
12+
first = (struct Node*)malloc(sizeof(struct Node));
13+
first->data = A[0];
14+
first->next = NULL;
15+
last = first;
16+
17+
for (i = 1; i < n; i++)
18+
{
19+
t = (struct Node*)malloc(sizeof(struct Node));
20+
t->data = A[i];
21+
t->next = NULL;
22+
last->next = t;
23+
last = t;
24+
}
25+
}
26+
void Display(struct Node* p)
27+
{
28+
while (p != NULL)
29+
{
30+
printf("->%d", p->data);
31+
p = p->next;
32+
}
33+
}
34+
int count(struct Node* p)
35+
{
36+
int c = 0;
37+
while (p != NULL)
38+
{
39+
c++;
40+
p = p->next;
41+
}
42+
return c;
43+
}
44+
45+
// reversing ll using array
46+
void Reverse1(struct Node* p) // pointer to 1st node
47+
{
48+
int* A,i=0; // for storing reversed linkedlist we need an array & and its size is not known so we will find the length of ll // for that we created an array dynamically
49+
struct Node* q=p; // for traversing we will take one more pointer that will help in traversing
50+
A = (int*)malloc(sizeof(int) * count(p)); // create an array size is length of ll // size of int type // mul by length of ll // array is created
51+
while (q != NULL)
52+
{
53+
A[i] = q->data; // everytime copy data from q to array
54+
q= q->next; // and move q to next
55+
i++; // increment i
56+
57+
}
58+
// once it reach end of linkedlist all element copied in array
59+
q = p;// q sud again strat from 1st pointer i.e p
60+
i--; // i shoud go back
61+
while (q != NULL)
62+
{
63+
q->data = A[i]; // we will copy in reverse manner
64+
q = q->next; // move q to next
65+
i--;
66+
67+
}
68+
69+
}
70+
71+
72+
int main()
73+
{
74+
int A[] = { 10,20,30,40,50 };
75+
Create(A, sizeof (A)/sizeof (int ));
76+
Reverse1(first);
77+
Display(first);
78+
printf("\n\n");
79+
80+
return 0;
81+
82+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
struct Node
5+
{
6+
int data;
7+
struct Node* next;
8+
}*first=NULL;
9+
void Create(int A[], int n)
10+
{
11+
int i;
12+
struct Node* last, * t;
13+
first = (struct Node*)malloc(sizeof(struct Node));
14+
first->data = A[0];
15+
first->next = NULL;
16+
last = first;
17+
18+
for (i = 1; i < n; i++)
19+
{
20+
t = (struct Node*)malloc(sizeof(struct Node));
21+
t->data = A[i];
22+
t->next = NULL;
23+
last->next = t;
24+
last = t;
25+
}
26+
}
27+
void Display(struct Node* p)
28+
{
29+
while (p != NULL)
30+
{
31+
printf("->%d", p->data);
32+
p = p->next;
33+
}
34+
}
35+
void RDisplay(struct Node* p)
36+
{
37+
if (p!=NULL)
38+
{
39+
printf("->%d", p->data);
40+
RDisplay(p->next);
41+
}
42+
}
43+
// using sliding pointer
44+
void Reverse2(struct Node* p) // 1st node pointer
45+
{
46+
struct Node* q = NULL, *r = NULL; // we requre three pointer p,q,r
47+
p = first;
48+
while (p != NULL)
49+
{/*
50+
sliding pointer */
51+
r = q; // r sud come upon q
52+
q = p;// q sud come upon p
53+
p = p->next;// p sud move to next node
54+
q->next = r;// q next sud points on
55+
}
56+
first = q; // once we came out of while loop // 1st ointer sud point on q
57+
58+
}
59+
60+
61+
int main()
62+
{
63+
int A[] = { 10,20,30,40,50 };
64+
Create(A, sizeof (A)/sizeof (int ));
65+
Reverse2(first);
66+
Display(first);
67+
printf("\n\n");
68+
69+
return 0;
70+
71+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct Node
4+
{
5+
int data;
6+
struct Node* next;
7+
}*first=NULL;
8+
void Create(int A[], int n)
9+
{
10+
int i;
11+
struct Node* last, * t;
12+
first = (struct Node*)malloc(sizeof(struct Node));
13+
first->data = A[0];
14+
first->next = NULL;
15+
last = first;
16+
17+
for (i = 1; i < n; i++)
18+
{
19+
t = (struct Node*)malloc(sizeof(struct Node));
20+
t->data = A[i];
21+
t->next = NULL;
22+
last->next = t;
23+
last = t;
24+
}
25+
}
26+
void Display(struct Node* p)
27+
{
28+
while (p != NULL)
29+
{
30+
printf("->%d", p->data);
31+
p = p->next;
32+
}
33+
}
34+
void RDisplay(struct Node* p)
35+
{
36+
if (p!=NULL)
37+
{
38+
printf("->%d", p->data);
39+
RDisplay(p->next);
40+
}
41+
}
42+
43+
44+
//reversing using Recursion
45+
void Rreverse(struct Node* q, struct Node* p) // taking two pointer p and q ; p will always ahead and q will follow that;
46+
{
47+
if (p != NULL)
48+
{
49+
Rreverse(p, p->next); // passing two pointer
50+
p->next = q;// while recturnig it will make p next as q
51+
}
52+
else
53+
first = q; // while p i null make first as q ;
54+
}
55+
56+
int main()
57+
{
58+
int A[] = { 10,20,30,40,50 };
59+
Create(A, sizeof (A)/sizeof (int ));
60+
Rreverse(NULL,first);
61+
Display(first);
62+
printf("\n\n");
63+
64+
return 0;
65+
66+
}

0 commit comments

Comments
 (0)