Skip to content

Commit 93d3013

Browse files
authored
Merge pull request #102 from sakshi-negi123/master
singly linked list
2 parents e65bdc1 + 808c1ff commit 93d3013

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
struct Node {
5+
int data;
6+
struct Node *next;
7+
};
8+
9+
struct Node* head = NULL;
10+
void insert(int new_data) {
11+
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
12+
new_node->data = new_data;
13+
new_node->next = head;
14+
head = new_node;
15+
}
16+
void display() {
17+
struct Node* ptr;
18+
ptr = head;
19+
while (ptr != NULL) {
20+
cout<< ptr->data <<" ";
21+
ptr = ptr->next;
22+
}
23+
}
24+
int main() {
25+
insert(3);
26+
insert(1);
27+
insert(7);
28+
insert(2);
29+
insert(9);
30+
cout<<"The linked list is: ";
31+
display();
32+
return 0;
33+
}

data_structures/Cpp/stack.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
using namespace std;
3+
int stack[100], n=100, top=-1;
4+
void push(int val) {
5+
if(top>=n-1)
6+
cout<<"Stack Overflow"<<endl;
7+
else {
8+
top++;
9+
stack[top]=val;
10+
}
11+
}
12+
void pop() {
13+
if(top<=-1)
14+
cout<<"Stack Underflow"<<endl;
15+
else {
16+
cout<<"The popped element is "<< stack[top] <<endl;
17+
top--;
18+
}
19+
}
20+
void display() {
21+
if(top>=0) {
22+
cout<<"Stack elements are:";
23+
for(int i=top; i>=0; i--)
24+
cout<<stack[i]<<" ";
25+
cout<<endl;
26+
} else
27+
cout<<"Stack is empty";
28+
}
29+
int main() {
30+
int ch, val;
31+
cout<<"1) Push in stack"<<endl;
32+
cout<<"2) Pop from stack"<<endl;
33+
cout<<"3) Display stack"<<endl;
34+
cout<<"4) Exit"<<endl;
35+
do {
36+
cout<<"Enter choice: "<<endl;
37+
cin>>ch;
38+
switch(ch) {
39+
case 1: {
40+
cout<<"Enter value to be pushed:"<<endl;
41+
cin>>val;
42+
push(val);
43+
break;
44+
}
45+
case 2: {
46+
pop();
47+
break;
48+
}
49+
case 3: {
50+
display();
51+
break;
52+
}
53+
case 4: {
54+
cout<<"Exit"<<endl;
55+
break;
56+
}
57+
default: {
58+
cout<<"Invalid Choice"<<endl;
59+
}
60+
}
61+
}while(ch!=4);
62+
return 0;
63+
}
64+

0 commit comments

Comments
 (0)