Skip to content

Commit fab5c13

Browse files
authored
Add files via upload
1 parent 90f481e commit fab5c13

24 files changed

+1549
-0
lines changed

1. Basic stack push po empty top.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define MAX_SIZE 5
5+
6+
int array[MAX_SIZE];
7+
int topN = -1;
8+
9+
void push(int x){
10+
if (topN == MAX_SIZE - 1){
11+
printf("Stack Overflow! Warning\n");
12+
}
13+
else{
14+
topN++;
15+
array[topN] = x;
16+
printf("Number pushed onto the stack is: %d\n", x);
17+
}
18+
}
19+
20+
int pop(){
21+
if (topN == -1){
22+
printf("Stack Underflow! Warning\n");
23+
return -1;
24+
}
25+
else{
26+
int poppedN = array[topN];
27+
topN--;
28+
return poppedN;
29+
}
30+
}
31+
32+
int top(){
33+
return array[topN];
34+
}
35+
36+
int isEmpty(){
37+
if(topN==-1){
38+
return 1;
39+
}
40+
else{
41+
return 0;
42+
}
43+
}
44+
45+
int main(){
46+
47+
push(10);
48+
push(20);
49+
push(30);
50+
push(40);
51+
push(50);
52+
53+
54+
printf("Top element: %d\n", top());
55+
56+
printf("%d popped out of the stack\n", pop());
57+
printf("%d popped out of the stack\n", pop());
58+
59+
printf("New top element: %d\n", top());
60+
61+
printf("Is the stack empty? %s\n", isEmpty() ? "Yes" : "No");
62+
63+
return 0;
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node{
5+
int data;
6+
struct node *link;
7+
};
8+
9+
void insert_Atthe_end(struct node *head){
10+
11+
struct node *ptr, *temp;
12+
ptr = head;
13+
14+
temp = malloc(sizeof(struct node));
15+
temp -> data = 99;
16+
temp -> link = NULL;
17+
18+
while(ptr -> link != NULL){
19+
ptr = ptr -> link;
20+
}
21+
22+
ptr -> link = temp;
23+
24+
}
25+
26+
int main(){
27+
28+
struct node *head = malloc(sizeof(struct node));
29+
head -> data = 45;
30+
head -> link = NULL;
31+
32+
struct node *second = malloc(sizeof(struct node));
33+
second -> data = 60;
34+
second -> link = NULL;
35+
head -> link = second;
36+
37+
second = malloc(sizeof(struct node));
38+
second -> data = 77;
39+
second -> link = NULL;
40+
41+
head -> link -> link = second;
42+
43+
insert_Atthe_end(head);
44+
45+
struct node *current = head;
46+
while (current != NULL) {
47+
printf("Data: %d\n", current->data);
48+
current = current->link;
49+
}
50+
51+
return 0;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node{
5+
int data;
6+
struct node *link;
7+
};
8+
9+
struct node *insertAt_theEnd(struct node *ptr, int data){
10+
struct node *temp = malloc(sizeof(struct node));
11+
12+
temp -> data = data;
13+
temp -> link = NULL;
14+
15+
ptr -> link = temp;
16+
return temp;
17+
18+
}
19+
20+
int main(){
21+
22+
struct node *head = malloc(sizeof(struct node));
23+
head -> data = 45;
24+
head -> link = NULL;
25+
26+
struct node *ptr = head;
27+
ptr = insertAt_theEnd(ptr,60);
28+
ptr = insertAt_theEnd(ptr,77);
29+
ptr = insertAt_theEnd(ptr,98);
30+
31+
ptr = head;
32+
33+
printf("Data: ");
34+
35+
while(ptr != NULL){
36+
37+
printf("%d ", ptr -> data);
38+
ptr = ptr -> link;
39+
40+
}
41+
42+
return 0;
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node{
5+
int data;
6+
struct node *link;
7+
};
8+
9+
struct node *add_beg(struct node *head,int d){
10+
11+
struct node *ptr = malloc(sizeof(struct node));
12+
ptr -> data = d;
13+
ptr -> link = NULL;
14+
15+
ptr -> link = head;
16+
head = ptr;
17+
return head;
18+
19+
}
20+
21+
int main(){
22+
23+
struct node *head = malloc(sizeof(struct node));
24+
head -> data = 45;
25+
head -> link = NULL;
26+
27+
struct node *ptr = malloc(sizeof(struct node));
28+
ptr -> data = 60;
29+
ptr -> link = NULL;
30+
head -> link = ptr;
31+
32+
int d = 3;
33+
34+
head = add_beg(head,d);
35+
36+
ptr = head;
37+
printf("Data: ");
38+
39+
while(ptr != NULL){
40+
41+
printf("%d ", ptr -> data);
42+
ptr = ptr -> link;
43+
44+
}
45+
46+
return 0;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node{
5+
int data;
6+
struct node *link;
7+
};
8+
9+
struct node *insertAt_theEnd(struct node *ptr, int data){
10+
11+
struct node *temp = malloc(sizeof(struct node));
12+
13+
temp -> data = data;
14+
temp -> link = NULL;
15+
16+
ptr -> link = temp;
17+
return temp;
18+
19+
}
20+
21+
void add_at_pos(struct node *head, int data, int position){
22+
23+
struct node *ptr = head;
24+
struct node *ptr2 = malloc(sizeof(struct node));
25+
ptr2 -> data = data;
26+
ptr2 -> link = NULL;
27+
28+
while (position != 2){
29+
30+
ptr = ptr -> link;
31+
position --;
32+
33+
}
34+
35+
ptr2 -> link = ptr -> link;
36+
ptr -> link = ptr2;
37+
38+
}
39+
40+
int main(){
41+
42+
struct node *head = malloc(sizeof(struct node));
43+
head -> data = 45;
44+
head -> link = NULL;
45+
46+
struct node *ptr = head;
47+
48+
ptr = insertAt_theEnd(ptr,60);
49+
ptr = insertAt_theEnd(ptr,77);
50+
ptr = insertAt_theEnd(ptr,108);
51+
52+
int data = 98, position = 4;
53+
54+
add_at_pos(head, data, position);
55+
56+
ptr = head;
57+
58+
printf("Data: ");
59+
60+
while (ptr != NULL){
61+
62+
printf("%d ", ptr-> data);
63+
ptr = ptr -> link;
64+
65+
}
66+
67+
return 0;
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node {
5+
6+
int data;
7+
struct node *link;
8+
9+
};
10+
11+
struct node *add_Node(struct node *ptr, int data){
12+
13+
struct node *temp = malloc(sizeof(struct node));
14+
temp -> data = data;
15+
temp -> link = NULL;
16+
17+
ptr -> link = temp;
18+
return temp;
19+
20+
}
21+
22+
struct node *del_first(struct node *head){
23+
24+
if(head == NULL)
25+
printf("List is empty");
26+
else {
27+
28+
struct node *temp = head;
29+
head = head -> link;
30+
free(temp);
31+
temp = NULL;
32+
33+
}
34+
35+
return head;
36+
37+
}
38+
39+
int main(){
40+
41+
struct node *head = malloc(sizeof(struct node));
42+
head -> data = 15;
43+
head -> link = NULL;
44+
45+
struct node *ptr = head;
46+
ptr = add_Node(ptr,25);
47+
ptr = add_Node(ptr,56);
48+
ptr = add_Node(ptr,98);
49+
50+
head = del_first(head);
51+
52+
ptr = head;
53+
54+
printf("Data: ");
55+
56+
while(ptr != NULL){
57+
58+
printf("%d ", ptr -> data);
59+
ptr = ptr -> link;
60+
61+
}
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)