Skip to content

Commit 789a0c7

Browse files
committed
add linked list problem set
1 parent fb3b5d9 commit 789a0c7

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

Diff for: a.out

-48.3 KB
Binary file not shown.

Diff for: linked-list-insert.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
/*
5+
Structure of the linked list node is as*/
6+
struct node
7+
{
8+
int data;
9+
struct node *next;
10+
};
11+
12+
13+
// function inserts the data in front of the list
14+
void insertAtBegining(struct node** headRef, int newData)
15+
{
16+
// Code here
17+
if(*headRef == NULL){
18+
(*headRef)->data = newData;
19+
(*headRef)->next = NULL;
20+
}else{
21+
node *newnode = new node();
22+
newnode->data = newData;
23+
newnode->next = (*headRef);
24+
(*headRef) = newnode;
25+
}
26+
}
27+
28+
// function appends the data at the end of the list
29+
void insertAtEnd(struct node** headRef, int newData)
30+
{
31+
// Code here
32+
node *newnode = new node();
33+
newnode->data = newData;
34+
newnode->next = NULL;
35+
if(*headRef ==NULL){
36+
*headRef = newnode;
37+
return;
38+
}
39+
40+
node* curr = *headRef;
41+
while(curr->next!=NULL){
42+
curr = curr->next;
43+
}
44+
curr->next = newnode;
45+
return;
46+
}
47+
48+
node* deleteNode(node *head,int x)
49+
{
50+
//Your code here
51+
if(head!=NULL){
52+
if(x==0){
53+
node *temp = head;
54+
head = head->next;
55+
delete temp;
56+
return head;
57+
}else{
58+
node *temp = head;
59+
for(int i=0;i<x-1;i++){
60+
head = head->next;
61+
}
62+
node *todelete = head->next;
63+
head->next = todelete->next;
64+
delete todelete;
65+
return temp;
66+
}
67+
}
68+
}
69+
70+
bool search(node *head,int x){
71+
if(head==NULL) return false;
72+
if(head->data == x) return true;
73+
search(head->next,x);
74+
}
75+
76+
int main(){
77+
int t,n,k,num;
78+
cin>>t;
79+
while(t--){
80+
cin>>n;
81+
node *head = NULL;
82+
for(int i=0;i<n;i++){
83+
// cin>>num>>k;
84+
// if(k==0) insertAtBegining(&head,num);
85+
// else insertAtEnd(&head,num);
86+
cin>>num;
87+
insertAtEnd(&head,num);
88+
}
89+
//delete node
90+
/*
91+
cout<<"enter pos to delete:: \n";
92+
int pos;
93+
cin>>pos;
94+
deleteNode(head,pos);
95+
*/
96+
97+
//search for nodevalue
98+
int val;
99+
cout<<"enter value to be searched:: \n";
100+
cin>>val;
101+
bool res = search(head,val);
102+
if(res == true)cout<<"value found\n";
103+
else cout<<"value not found\n";
104+
105+
while(head!=NULL){
106+
cout<<head->data<<" ";
107+
head = head->next;
108+
}
109+
cout<<endl;
110+
delete head;
111+
}
112+
return 0;
113+
}

Diff for: number-starting-with-1-in-b-base.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//http://practice.geeksforgeeks.org/problems/number-starting-with-1-in-b-base/0
2+
//number-starting-with-1-in-b-base
3+
#include<iostream>
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
int main(){
7+
int t,b;
8+
cin>>t;
9+
while(t--){
10+
int n;
11+
cin>>n>>b;
12+
int m = log2(n);
13+
int flag=0;
14+
for(int i=1;i<=m;i++){
15+
if(n>=pow(b,i) && n<=2*pow(b,i)-1){
16+
cout<<"Yes\n";
17+
flag=1;
18+
break;
19+
}
20+
}
21+
if(flag==0)cout<<"No\n";
22+
}
23+
return 0;
24+
}

0 commit comments

Comments
 (0)