I have a struct blocks within a long c file
struct node {
int val;
struct node *next;
};
How do I use the sed function to find this struct block and convert it into one line. So it looks like this:
struct node { int val; struct node *next;};
Thanks in advance
My input is this:
struct node {
int val;
struct node *next;
};
typedef struct {
int numer;
int denom;
} Rational;
int main()
{
struct node head;
Rational half, *newf = malloc(sizeof(Rational));
head = (struct node){ 5, NULL };
half = (Rational){ 1, 2 };
*newf = (Rational){ 2, 3 };
}
My output is :
struct node { int val; struct node *next;};
typedef struct { int numer; int denom;} Rational;int main(){struct node head;Rational half, *newf = malloc(sizeof(Rational));head = (struct node){ 5, NULL };
half = (Rational){ 1, 2 };
*newf = (Rational){ 2, 3 };
}
I only want the struct node: struct node { int val; struct node *next;};
and the typedef struct: typedef struct { int numer; int denom;} Rational;
to be in one line. However int main() is being appended to the end of Rational;
I want the stuff in the main function to remain as it is.
With sed:
sed '/struct[^(){]*{/{:l N;s/\n//;/}[^}]*;/!t l;s/ */ /g}' input.c
When sed sees a struct definition (/struct[^{]*{/), it will read lines until a }; is seen on a line (:l N;s/\n//;/[}];/!t l;) while also removing newlines. When it matches }; it removes extra spaces (;s/ */ /g).
Related
C++ noob reporting in. I'm trying to write a function that will create and initialize a doubly linked list using values that are stored in two different arrays. Here's how my linked list class is set up:
class node {
public:
node *prev;
node *next;
int key;
char type;
};
and here's how my dList class (containing various functions to alter my linked list) is set up:
class dList {
private:
node *head; // dummy head
node *tail; // dummy tail
public:
dList() { // default constructor, creates empty list
head = tail = NULL;
}
~dList() { // deconstructor
node *ptr = head;
while (head != NULL) {
head = head->next;
delete ptr;
}
tail = NULL;
}
dList(int arrayNums[], char arrayChars[], int size); // parametrized constructor, initialize list w/ contents of arrays
void addFront(int k, char t); // creates new node at front of list
void addBack(int k, char t); // creates new node at back of list
node *search(int k); // searches list for occurence of int parameter and returns pointer to node containing key
void find(char t); // outputs all keys that have type equal to character parameter, front to back
void moveFront(node* ptr); // moves node pointed to by parameter to front of list
void moveBack(node* ptr); // moves node pointed to by parameter to back of list
void out(int num, char = 'f'); // outputs first int elements of list, starting at front or back depending on char parameter
void sort(); // peforms a quick or mergesort on items; list should be in increasing order based on integer key
};
I need help implementing my parametrized constructor. Could someone tell me if the function I have now is written correctly? I think it is, but when I run my program, it runs forever--a clear problem. Here's my function as-is:
dList::dList(int arrayNums[], char arrayChars[], int size) {
node *newNode = NULL;
for (int i = 0; i < size; i++) {
if (head == NULL) {
newNode = new node;
newNode->key = arrayNums[i];
newNode->type = arrayChars[i];
newNode->prev = NULL;
newNode->next = NULL;
head = newNode;
tail = newNode;
}
else { // needs work!
newNode = new node;
newNode->key = arrayNums[i];
newNode->type = arrayChars[i];
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
}
if (i == (size - 1)) {
tail->next = NULL;
}
}
}
Thank you very much!
EDIT: here is my main.cpp (code I'm using to test my dList.cpp file)
#include <iostream>
using namespace std;
#include "dList.cpp"
#define SMALL 200
#define MAX 100000
#define ROUNDS 100
int main(){
int i, x[MAX];
char ch[MAX];
for(i=0;i<SMALL;i++) {
x[i] = 2 * (SMALL - i);
ch[i] = 'a' + (i % 26);
}
dList A(x,ch,SMALL), B;
A.out(10);
node *tmp = A.search(2*SMALL-8);
A.moveFront(tmp);
A.out(10);
A.moveBack(tmp);
A.out(10);
A.find('b');
A.sort();
A.out(10);
A.out(10,'b');
A.addBack(500,'d');
A.addFront(501,'z');
A.out(10);
A.out(10,'b');
B.addFront(1,'a');
B.addBack(2,'b');
B.out(2);
for(int j=0; j<ROUNDS; j++){
cout << endl << "round " << j << endl;
for(i=0;i<MAX;i++) {x[i] = 2*MAX-i; ch[i] = 'a'+ (i%26);}
dList A(x,ch,MAX);
node *tmp = A.search(2*MAX-8);
A.moveFront(tmp);
A.moveBack(tmp);
A.sort();
A.out(10);
A.out(10,'b');
}
}
I am trying to use recursion to solve this problem where if i call
decimal<0,0,1>();
i should get the decimal number (4 in this case).
I am trying to use recursion with variadic templates but cannot get it to work.
Here's my code;
template<>
int decimal(){
return 0;
}
template<bool a,bool...pack>
int decimal(){
cout<<a<<"called"<<endl;
return a*2 + decimal<pack...>();
};
int main(int argc, char *argv[]){
cout<<decimal<0,0,1>()<<endl;
return 0;
}
What would be the best way to solve this?
template<typename = void>
int decimal(){
return 0;
}
template<bool a,bool...pack>
int decimal(){
cout<<a<<"called"<<endl;
return a + 2*decimal<pack...>();
};
The problem was with the recursive case, where it expects to be able to call decltype<>(). That is what I have defined in the first overload above. You can essentially ignore the typename=void, the is just necessary to allow the first one to compile.
A possible solution can be the use of a constexpr function (so you can use it's values it's value run-time, when appropriate) where the values are argument of the function.
Something like
#include <iostream>
constexpr int decimal ()
{ return 0; }
template <typename T, typename ... packT>
constexpr int decimal (T const & a, packT ... pack)
{ return a*2 + decimal(pack...); }
int main(int argc, char *argv[])
{
constexpr int val { decimal(0, 0, 1) };
static_assert( val == 2, "!");
std::cout << val << std::endl;
return 0;
}
But I obtain 2, not 4.
Are you sure that your code should return 4?
-- EDIT --
As pointed by aschepler, my example decimal() template function return "eturns twice the sum of its arguments, which is not" what do you want.
Well, with 0, 1, true and false you obtain the same; with other number, you obtain different results.
But you can modify decimal() as follows
template <typename ... packT>
constexpr int decimal (bool a, packT ... pack)
{ return a*2 + decimal(pack...); }
to avoid this problem.
This is a C++14 solution. It is mostly C++11, except for std::integral_sequence nad std::index_sequence, both of which are relatively easy to implement in C++11.
template<bool...bs>
using bools = std::integer_sequence<bool, bs...>;
template<std::uint64_t x>
using uint64 = std::integral_constant< std::uint64_t, x >;
template<std::size_t N>
constexpr uint64< ((std::uint64_t)1) << (std::uint64_t)N > bit{};
template<std::uint64_t... xs>
struct or_bits : uint64<0> {};
template<std::int64_t x0, std::int64_t... xs>
struct or_bits<x0, xs...> : uint64<x0 | or_bits<xs...>{} > {};
template<bool...bs, std::size_t...Is>
constexpr
uint64<
or_bits<
uint64<
bs?bit<Is>:std::uint64_t(0)
>{}...
>{}
>
from_binary( bools<bs...> bits, std::index_sequence<Is...> ) {
(void)bits; // suppress warning
return {};
}
template<bool...bs>
constexpr
auto from_binary( bools<bs...> bits={} )
-> decltype( from_binary( bits, std::make_index_sequence<sizeof...(bs)>{} ) )
{ return {}; }
It generates the resulting value as a type with a constexpr conversion to scalar. This is slightly more powerful than a constexpr function in its "compile-time-ness".
It assumes that the first bit is the most significant bit in the list.
You can use from_binary<1,0,1>() or from_binary( bools<1,0,1>{} ).
Live example.
This particular style of type-based programming results in code that does all of its work in its signature. The bodies consist of return {};.
I just test the function on one of the struct which is name but it won't reach them.
This is the full code so far:
update:
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstddef>
using namespace std;
struct Node{
string name;
string address;
int phoneNum;
struct Node* next;
};
Node insertInOrder(Node p, string theName);
void push(struct Node*& head, string theName);
int main(){
cout<<"\tEnter S or s to show the list contents \n"
<<"\tEnter A or a to add a node to the list \n"
<<"\tEnter D or d to delete a node from the list \n"
<<"\tEnter Q or q to quiet the program \n"
<<"Make your selection: "<<endl;
struct Node* newNode = new Node;
push(newNode, "yeah");
cout<<newNode;
return 0;
}
void push(struct Node*& head, string theName){
struct Node* newNode = new Node;
newNode->name = theName;
newNode->next = head;
head = newNode;
}
Node insertInOrder(Node p, string theName){
if(p == NULL || p.name >= theName){
return new Node(p, theName);
}
else{
p.next = insertInOrder(p.next, theName);
return p;
}
}
I get an error that says: invalid application of ‘sizeof’ to incomplete type ‘Node’ for this code:
void push(struct Node*& head, string theName){
struct Node* newNode = malloc(sizeof(struct Node));
newNode->name = theName;
newNode->next = head;
head = newNode;
}
I'm trying to translate this code into my code but I got an error:
Node insertInOrder( int k, Node p ) {
if( p == " " || p.item >= k )
return new Node( k, p );
else {
p.next = insertInOrder( k, p.next );
return p;
}
}
This is how I translated it:
Node insertInOrder(Node p, string theName){
if(p == " " || p.name >= theName){
return new Node(p, theName);
}
else{
p.next = insertInOrder(p.next, theName);
return p;
}
}
here is the error for this code:
if(p == " " || p.name >= theName){
return new Node(p, theName);
Errors:
- comparison with string literal results in unspecified behaviour [-Waddress]
- request for member ‘name’ in ‘p’, which is of pointer type ‘Node*’ (maybe you meant to use ‘-
>’ ?)
- comparison between distinct pointer types ‘Node*’ and ‘const char*’ lacks a cast [-
fpermissive]
p.next = insertInOrder(p.next, theName);
return p;
errors:
Invalid arguments ' Candidates are: Node insertInOrder(Node, std::basic_string<char,std::char_traits<char>,std::allocator<char>>) '
- could not convert ‘p.Node::next’ from ‘Node*’ to ‘Node’
Some points:
forget malloc since you are working with C++ and use new and delete
you don't need to specify that node is a struct again whenever you use it so sizeof(Node) is enough, but you won't directly use malloc
your function Node insertInOrder(Node p, string theName) accepts a concrete Node and return a concrete Node but the field next inside your struct is a pointer to a Node, I guess you shoul be consistent in what you are using and since you are working with a linked list using pointers is more suitable
you can't use a comparison operator directly between a value and a string literal (p == " "), you should check name field only
I'm trying to measure a performance difference between using Boost.Variant and using virtual interfaces. For example, suppose I want to increment different types of numbers uniformly, using Boost.Variant I would use a boost::variant over int and float and a static visitor which increments each one of them. Using class interfaces I would use a pure virtual class number and number_int and number_float classes which derive from it and implement an "increment" method.
From my testing, using interfaces is far faster than using Boost.Variant.
I ran the code at the bottom and received these results:
Virtual: 00:00:00.001028
Variant: 00:00:00.012081
Why do you suppose this difference is? I thought Boost.Variant would be a lot faster.
** Note: Usually Boost.Variant uses heap allocations to guarantee that the variant would always be non-empty. But I read on the Boost.Variant documentation that if boost::has_nothrow_copy is true then it doesn't use heap allocations which should make things significantly faster. For int and float boost::has_nothrow_copy is true.
Here is my code for measuring the two approaches against each other.
#include <iostream>
#include <boost/variant/variant.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/format.hpp>
const int iterations_count = 100000;
// a visitor that increments a variant by N
template <int N>
struct add : boost::static_visitor<> {
template <typename T>
void operator() (T& t) const {
t += N;
}
};
// a number interface
struct number {
virtual void increment() = 0;
};
// number interface implementation for all types
template <typename T>
struct number_ : number {
number_(T t = 0) : t(t) {}
virtual void increment() {
t += 1;
}
T t;
};
void use_virtual() {
number_<int> num_int;
number* num = &num_int;
for (int i = 0; i < iterations_count; i++) {
num->increment();
}
}
void use_variant() {
typedef boost::variant<int, float, double> number;
number num = 0;
for (int i = 0; i < iterations_count; i++) {
boost::apply_visitor(add<1>(), num);
}
}
int main() {
using namespace boost::posix_time;
ptime start, end;
time_duration d1, d2;
// virtual
start = microsec_clock::universal_time();
use_virtual();
end = microsec_clock::universal_time();
// store result
d1 = end - start;
// variant
start = microsec_clock::universal_time();
use_variant();
end = microsec_clock::universal_time();
// store result
d2 = end - start;
// output
std::cout <<
boost::format(
"Virtual: %1%\n"
"Variant: %2%\n"
) % d1 % d2;
}
For those interested, after I was a bit frustrated, I passed the option -O2 to the compiler and boost::variant was way faster than a virtual call.
Thanks
This is obvious that -O2 reduces the variant time, because that whole loop is optimized away. Change the implementation to return the accumulated result to the caller, so that the optimizer wouldn't remove the loop, and you'll get the real difference:
Output:
Virtual: 00:00:00.000120 = 10000000
Variant: 00:00:00.013483 = 10000000
#include <iostream>
#include <boost/variant/variant.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/format.hpp>
const int iterations_count = 100000000;
// a visitor that increments a variant by N
template <int N>
struct add : boost::static_visitor<> {
template <typename T>
void operator() (T& t) const {
t += N;
}
};
// a visitor that increments a variant by N
template <typename T, typename V>
T get(const V& v) {
struct getter : boost::static_visitor<T> {
T operator() (T t) const { return t; }
};
return boost::apply_visitor(getter(), v);
}
// a number interface
struct number {
virtual void increment() = 0;
};
// number interface implementation for all types
template <typename T>
struct number_ : number {
number_(T t = 0) : t(t) {}
virtual void increment() { t += 1; }
T t;
};
int use_virtual() {
number_<int> num_int;
number* num = &num_int;
for (int i = 0; i < iterations_count; i++) {
num->increment();
}
return num_int.t;
}
int use_variant() {
typedef boost::variant<int, float, double> number;
number num = 0;
for (int i = 0; i < iterations_count; i++) {
boost::apply_visitor(add<1>(), num);
}
return get<int>(num);
}
int main() {
using namespace boost::posix_time;
ptime start, end;
time_duration d1, d2;
// virtual
start = microsec_clock::universal_time();
int i1 = use_virtual();
end = microsec_clock::universal_time();
// store result
d1 = end - start;
// variant
start = microsec_clock::universal_time();
int i2 = use_variant();
end = microsec_clock::universal_time();
// store result
d2 = end - start;
// output
std::cout <<
boost::format(
"Virtual: %1% = %2%\n"
"Variant: %3% = %4%\n"
) % d1 % i1 % d2 % i2;
}
It might be for pedantic purposes but not homework. I have below question about 'Sorting a singly linked list' (Any kind of list for that matter) Assume for this questions we want to sort in ascending order of values in each node.
Does this question expect me to just sort the list by swapping the values of each node in the list so that the values are in some order(ascending/descending). Here the original values of nodes (before sorting) would be changed in order to reflect the sorted list. Here the same earlier head pointer is returned but now having the smallest element in it, which might be different than its earlier element.
Like this C code I have below(The code below might not compile as is , but I have it working fine. Posted here as illustrative to clear my point):
struct lnk_lst
{
int val;
struct lnk_lst *next;
};
main()
{
//Assume curr is the head node of the singly linked list created with some values
curr = llist_bubble_sort(curr);
}
struct lnk_lst* llist_bubble_sort(struct lnk_lst *lst)
{
int i,n=0,tmpint;
struct lnk_lst *tmp=lst,*tmp2=lst;
while(tmp->next)
{
lst = tmp2;
while(lst->next)
{
if(lst->val > lst->next->val)
{
tmpint = lst->val;
lst->val = lst->next->val;
lst->next->val = tmpint;
}
lst = lst->next;
}
tmp = tmp->next;
}
return tmp2;
}
OR
Is it expected to that the pointer to the node with the smallest element(assuming ascending order) in the original ordering is moved as new head node, then the node having the next smallest element is linked to the head node, and so on such that the list is reordered completely and now the head node returned is not same pointer as earlier.
If the interpretation of list sort is this second, then I have to see how yet get the idea done in code.
The whole idea behind linked lists is that the links can be changed without affecting the content. Changing a pointer sometimes involves creating a pointer to pointer variable. Also: if you only swap the contents, you could just as well leave out the ->next pointers, use an array instead, and swap the array's contents.
IMHO the natural way of sorting a linked list is mergesort. The fragment below splits the list in two part: the nodes that are already in place, and those that are not. The second list is sorted, and the two lists are merged. Both splitting&merging involve some pointer-to-pointer variables.
#include <stdio.h>
#include <string.h>
struct llist {
struct llist *next;
char *payload;
};
int llist_cmp(struct llist *l, struct llist *r);
struct llist * llist_split(struct llist **hnd
, int (*cmp)(struct llist *l, struct llist *r) );
struct llist * llist_merge(struct llist *one, struct llist *two
, int (*cmp)(struct llist *l, struct llist *r) );
struct llist * llist_sort(struct llist *ptr
, int (*cmp)(struct llist *l, struct llist *r) );
struct llist * llist_split(struct llist **hnd, int (*cmp)(struct llist *l, struct llist *r) )
{
struct llist *this, *save, **tail;
for (save=NULL, tail = &save; this = *hnd; ) {
if (! this->next) break;
if ( cmp( this, this->next) <= 0) { hnd = &this->next; continue; }
*tail = this->next;
this->next = this->next->next;
tail = &(*tail)->next;
*tail = NULL;
}
return save;
}
struct llist * llist_merge(struct llist *one, struct llist *two, int (*cmp)(struct llist *l, struct llist *r) )
{
struct llist *result, **tail;
for (result=NULL, tail = &result; one && two; tail = &(*tail)->next ) {
if (cmp(one,two) <=0) { *tail = one; one=one->next; }
else { *tail = two; two=two->next; }
}
*tail = one ? one: two;
return result;
}
struct llist * llist_sort(struct llist *ptr, int (*cmp)(struct llist *l, struct llist *r) )
{
struct llist *save;
save=llist_split(&ptr, cmp);
if (!save) return ptr;
save = llist_sort(save, cmp);
return llist_merge(ptr, save, cmp);
}
int llist_cmp(struct llist *l, struct llist *r)
{
if (!l) return 1;
if (!r) return -1;
return strcmp(l->payload,r->payload);
}
struct llist lists[] =
{{ lists+1, "one" }
,{ lists+2, "two" }
,{ lists+3, "three" }
,{ lists+4, "four" }
,{ lists+5, "five" }
,{ lists+6, "six" }
,{ lists+7, "seven" }
,{ lists+8, "eight" }
,{ NULL, "nine" }
};
int main()
{
struct llist *root,*tmp;
root = lists;
fprintf(stdout, "## %s\n", "initial:" );
for (tmp=root; tmp; tmp=tmp->next) {
fprintf(stdout, "%s\n", tmp->payload);
}
fprintf(stdout, "## %s\n", "sorting..." );
root = llist_sort(root, llist_cmp);
for (tmp=root; tmp; tmp=tmp->next) {
fprintf(stdout, "%s\n", tmp->payload);
}
fprintf(stdout, "## %s\n", "done." );
return 0;
}
RESULT:
## initial:
one
two
three
four
five
six
seven
eight
nine
## sorting...
eight
five
four
nine
one
seven
six
three
two
## done.
The correct way of sorting a linked list is the second one. This also makes sense because often you would want to sort objects on some attribute while retaining their other attribute values. In that case, the first approach doesn't help much.
The idea is similar to traditional sorting approaches. For example you can use insertion sort by iterating through the elements and then inserting the next element in the right position in the correctly sorted linked list upto that element. For detailed code in C, you can see the Wikipedia page here.