Why my front view of the Linked list is not printing - linked-list

So i was trying this question in which I have written the below code:-
#include <bits/stdc++.h>
using namespace std;
struct Node
{
char data;
struct Node* next;
};
struct Node *reverseList(struct Node *head)
{
Node *current = head;
Node *prev = NULL, *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
bool isPalindrome(struct Node* head)
{
Node* frontHead = head;
Node* reverseHead = reverseList(head);
Node* temp = reverseHead;
cout<<"back view"<<endl;
while(temp)
{
cout<<temp->data<<"->";
temp = temp ->next;
}
cout<<endl;
cout<<"front view"<<endl;
temp = frontHead;
while(temp)
{
cout<<temp->data<<"->";
temp = temp ->next;
}
cout<<endl;
bool flag = true;
// while(frontHead && reverseHead)
// {
// // cout<<frontHead->data<<"-->"<<reverseHead->data<<endl;
// if(frontHead->data !=reverseHead->data)
// {
// flag=false;
// break;
// }
// frontHead = frontHead->next;
// reverseHead = reverseHead->next;
// }
return flag;
}
void push(struct Node** head_ref, char new_data)
{
struct Node* new_node = (struct Node*)malloc(
sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* ptr)
{
while (ptr != NULL)
{
cout << ptr->data << "->";
ptr = ptr->next;
}
cout << "NULL" << "\n";
}
// Driver code
int main()
{
struct Node* head = NULL;
char str[] = "abacba";
int i;
for (i = 0; i<strlen(str); i++)
push(&head, str[i]);
// printList(head);
isPalindrome(head) ? cout << "Is Palindrome" << "\n\n" : cout << "Not Palindrome" << "\n\n";
return 0;
}
When I try to print the node form the front as well as from the backward(after reversing the linked list I got this output:-
back view
a->b->a->c->b->a->
front view
a->
Is Palindrome
can Anyone tell me why i am not able to get the front view of the linked list .
Any help will be appreciated

Related

insert in simple linked list doesn't get last line

I am trying to insert into a simply linked list from a file. Do you
guys have any idea why the function doesn't insert the last line in my
file?
This is the file:
10,Ghidul Ciberbobocului,Stan Daria,3,Popescu Matei,Ionescu Gigel,Ilinca Radu,100.5
2,Spring IT,Mirodene Cristina,4,Dumitru Mihai,Vasiliu Valentin,Balasa Silvia,Dumitru Ion,400.
89,Serile teatrului studentesc,Petre Ion,2,Nicolae Ramona,Stan Alberto,1000
1,Tutoring,Petre Miruna,2,Bode Cristina,Angelescu Paul,500.5
11,IT Fest,Ciurea Ion,2,Ionescu Georgiana,Neagu Bianca,100.6
My code:
struct Proiect {
int id;
char* numeProiect;
char* numeCoordonator;
unsigned int nrStudenti;
char** studenti;
float costInscriere;
};
struct nodLista {
Proiect proiect;
nodLista *next;
};
nodLista* inserareNod(nodLista *first, Proiect p) {
nodLista* newNode = new nodLista;
newNode->next = NULL;
newNode->proiect = p;
if (!first) {
return newNode;
}
nodLista* aux = first;
while (aux->next) {
aux = aux->next;
}
aux->next = newNode;
printf("%d\n", aux->proiect.id);
return first;
}
void main() {
nodLista* first = NULL;
Proiect proiect;
FILE *f = fopen("proiecte2.txt", "r");
char line[150];
int nrProiecte = 0;
if (f) {
while (fgets(line, sizeof(line), f)) {
nrProiecte++;
}
printf("Nr proiecte: %d",nrProiecte);
fclose(f);
}
else {
printf("Fisierul nu a fost gasit");
}
f = fopen("proiecte2.txt", "r");
char *token[150], sep_list[] = ",";
Proiect* listaProiecte;
listaProiecte = (Proiect*)malloc(nrProiecte * sizeof(Proiect));
int i = 0;
if (f) {
while(fgets(line, sizeof(line), f)) {
token[0] = strtok(line, sep_list);
listaProiecte[i].id = atoi(token[0]);
first = inserareNod(first, listaProiecte[i]);
i++;
}
}
else printf("Fisierul nu aputut fi deschis");
}
Output: It only displays 10 , 2, 89 and 1.
Did you make prints inside this function to check if it's doing the while correctly?
while(fgets(line, sizeof(line), f)) {
token[0] = strtok(line, sep_list);
listaProiecte[i].id = atoi(token[0]);
first = inserareNod(first, listaProiecte[i]);
i++;
}

Linked list exercise in C, what is wrong?

The code below does compile, but it doesn't run as it should.
I'm not sure what am I doing wrong, so would someone be willing to tell me what I did wrong and what I should have done better.
What do I need to change to make it run properly?
#include<stdio.h>
#include<stdlib.h>
typedef struct sub_Node
{
int value;
struct sub_Node *next;
}sub_Node;
typedef struct Node
{
char *name;
struct Node *next;
struct sub_Node *sub_start;
}Node;
Node *start;
void add_player(char *name)
{
Node *temp;
temp = (Node *)malloc(sizeof(Node));
temp->next = start;
temp->name = name;
temp->sub_start = (sub_Node *)malloc(sizeof(sub_Node));
temp->sub_start->next = NULL;
temp->sub_start->value = -1;
start = temp;
}
void initialize()
{
char *p;
p = "\0";
add_player(p);
}
void remove_player(char *name)
{
Node *p;
for(p = start; p!= NULL; p = p->next)
if(p->name == name)
{
p->name = p->next->name;
p->next = p->next->next;
}
}
sub_Node* add_descending(sub_Node* sub_start, int piece_value)
{
sub_Node *temp, *prev, *next;
temp = (sub_Node *)malloc(sizeof(sub_Node));
temp->value = piece_value;
temp->next = NULL;
prev = NULL;
next = sub_start;
while(next && next->value >= piece_value)
{
prev = next;
next = next->next;
}
if(!next)
{
prev->next = temp;
}
else
{
if(prev)
{
temp->next = prev->next;
prev->next = temp;
}
else
{
temp->next = sub_start;
sub_start = temp;
}
}
return sub_start;
}
void add_piece(char *name, int piece_value)
{
Node *p;
int c;
for(p = start; p!=NULL; p = p->next)
if(p->name == name)
p->sub_start = add_descending(p->sub_start, piece_value);
}
void print_pieces(char *name)
{
Node *p;
sub_Node *q;
for(p = start; p!=NULL; p = p->next)
if(p->name == name)
{
printf("The values of the owned pieces are:");
for(q = p->sub_start; q->value != -1; q = q->next)
printf(" %d", q->value);
}
}
int lose_piece(char *name)
{
Node *p;
sub_Node *q;
int aux;
for(p = start; p!=NULL; p = p->next)
if(p->name == name)
{
for(q = p->sub_start; q->next->value != -1; q = q->next) {}
aux = q->value;
q->value = q->next->value;
q->next = q->next->next;
return aux;
}
}
void print_players()
{
Node *p;
printf("The players are: ");
for(p = start; p->name != "\0"; p = p->next)
printf("%s ", p->name);
printf("\n");
}
int main()
{
initialize();
int y, value;
char name[20];
printf("Insert a digit to execute the desired task:\n"
"<0> end the program\n"
"<1> add a player, who doesn't own any piece yet\n"
"<2> remove a player and all his pieces\n"
"<3> print the name of all the players\n"
"<4> a player gets a piece\n"
"<5> a player loses the piece with the lowest value out of the ones that he has\n"
"<6> prints the pieces of a player in a descending order by value\n\n");
do
{
printf("digit: ");
scanf("%d", &y);
switch(y)
{
case 1:
printf("Insert the player's name: ");
scanf("%s", name);
add_player(name);
break;
case 2:
printf("Insert the player's name: ");
scanf("%s", name);
remove_player(name);
break;
case 3:
print_players();
break;
case 4:
printf("Insert the player's name: ");
scanf("%s", name);
printf("Insert the value of the piece: ");
scanf("%d", value);
add_piece(name, value);
break;
case 5:
printf("Insert the player's name: ");
scanf("%s", name);
printf("\nThe player loses the piece: %d\n", lose_piece(name));
break;
case 6:
printf("Insert the player's name: ");
scanf("%s", name);
print_pieces(name);
}
} while(y != 0);
return 0;
}
your two main problems where this scanf("%d", value); value should be passed by reference like this scanf("%d", &value); and the second is string comparison in c as in your code p->name != "\0" and if(p->name == name) this is wrong because actually you are making comparison between addresses of strings (where it resides in memory) not strings values. to compare strings in c you have to use strcmp and families.
Actually 3 main problems. for setting string values as you did in temp->name = name; is little bit more complicated than that. because you are assigning to temp->name a string from the stack that is volatile (the stack will be more likely invalid soon you return from the function) . in your case you have to alloc a new string by using malloc (and friends) or just by using strdup.
here is as a bonus a slightly rewrite of your program, you will find many advises and is a good starting point for how to structure your code for an easy maintenance.
still want to advise you to change members and variables to more declarative names as in sub_start and sub_Node can be PieceNode and pieces respectively.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct sub_Node
{
int value;
struct sub_Node *next;
}sub_Node;
typedef struct Node
{
char *name;
struct Node *next;
struct Node *prev; // this to make life easyer
struct sub_Node *sub_start;
}Node;
Node *start = NULL;
Node *find_player(char *name){
Node *tmp = start;
while( tmp ){
if(strcmp(tmp->name,name) == 0 )
break;
tmp = tmp->next;
}
return tmp;
}
// int to return Error Code
//
int add_player(char *name)
{
Node *temp;
if( find_player(name)) {
printf("player %s already exists\n", name);
return 1;
}
// do not cast malloc
temp = malloc(sizeof(Node));
if( !temp ){
printf ("not enough memory\n");
return 2;
}
temp->name = strdup ( name); // here was your error
temp->sub_start = NULL; // keep it simple
temp->prev = NULL;
temp->next = start;
if(start)
start->prev = temp;
start = temp;
return 0; // no error
}
void DestroyPieces(sub_Node* piece){
if( piece ) {
DestroyPieces( piece->next );
free( piece );
}
}
// as usual use int to return error code
int remove_player(char *name)
{
Node *player = find_player(name);
if ( !player ){
return 1; // player not found
}
if ( player->next ){
player->next->prev = player->prev;
}
if ( player->prev ){
player->prev->next = player->next;
}
DestroyPieces(player->sub_start);
free(player->name);
free(player);
return 0; // success
}
sub_Node* new_piece(int value){
sub_Node *temp = malloc( sizeof(sub_Node) );
if(temp){
temp->value = value;
temp->next = NULL;
}
return temp;
}
// int to return error code
// pass sub_start as pointer to pointer, as it might be updated
int add_descending(sub_Node** psub_start, int piece_value)
{
sub_Node *piece, *current, *prev = NULL;
if( !psub_start){
return 5; // this should not happen
}
current = *psub_start;
piece = new_piece( piece_value );
if( !piece ) return 1; // no mem
if(!current){
// this is the first and only one
*psub_start = piece;
return 0; // OK
}
while(current && current->value >= piece_value)
{
prev = current;
current = current->next;
}
if( prev )
prev->next = piece;
piece->next = current;
if( current == *psub_start ){
*psub_start = piece;
}
return 0 ; // OK
}
void add_piece(Node * player, int piece_value)
{
if ( !player) {
return ;
}
if(add_descending (&(player->sub_start), piece_value) == 0 )
return ; //OK
printf("an error occured while adding a piece (%d) to player '%s'\n",piece_value,player->name);
}
void print_pieces(Node *player)
{
sub_Node *q;
if( !player ){
return;
}
if( !player->sub_start ){
printf("Player '%s' has no pieces\n",player->name);
return;
}
printf("The values of the owned pieces are:");
for(q = player->sub_start; q != NULL; q = q->next)
printf(" %d", q->value);
printf("\n");
}
void lose_piece(Node *player)
{
if( !player ){
return;
}
sub_Node *q, *prev = NULL;
int aux;
if( !player->sub_start ){
printf("Player '%s' has no pieces\n",player->name);
return;
}
// i think you want drop the last one
for(q = player->sub_start; q->next != NULL ;prev = q, q = q->next) {
;
}
if(prev)
prev->next = NULL;
else
player->sub_start = NULL;
aux = q->value;
free(q);
printf("\nThe player loses the piece: %d\n", aux);
return;
}
void print_players()
{
Node *p;
if( !start ){
printf("there are no players, try to add some\n");
return;
}
printf("The players are: ");
for(p = start; p != NULL; p = p->next)
printf("%s ", p->name);
printf("\n");
}
void print_menu(void){
printf("Insert a digit to execute the desired task:\n"
"<0> end the program\n"
"<1> add a player, who doesn't own any piece yet\n"
"<2> remove a player and all his pieces\n"
"<3> print the name of all the players\n"
"<4> a player gets a piece\n"
"<5> a player loses the piece with the lowest value out of the ones that he has\n"
"<6> prints the pieces of a player in a descending order by value\n\n");
}
Node * get_player(char *name){
Node *player = find_player(name);
if(!player)
printf("Player '%s' do not exists\n",name);
return player;
}
int main()
{
// initialize(); no more needed
int y, value;
char name[20];
Node *player;
print_menu();
do
{
printf("digit: ");
scanf("%d", &y);
switch(y)
{
case 1:
printf("Insert the player's name: ");
scanf("%s", name);
add_player(name);
break;
case 2:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
if( player )
break;
case 3:
print_players();
break;
case 4:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
if( player ){
printf("Insert the value of the piece: ");
scanf("%d", &value);
add_piece(player, value);
}
break;
case 5:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
lose_piece(player);
break;
case 6:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
print_pieces(player);
}
} while(y != 0);
return 0;
}

Using fgets and strtok to read in data and create linked list

Need some help with reading in lines of data from a text file using the fgets and string tokenization commands, which will then be used to create a linked list. I've followed some examples I've found on Stack Overflow and other tutorial websites, but still cannot get the read function below to work properly in my program, it just causes it to crash. The data file has lines like this:
Zucchini, Squash, pound, 2.19, 45
Yellow, Squash, pound, 1.79, 15
Based on everything I've read, I believe I have the necessary code, but obviously I'm missing something. Also, I commented out one of the fields (the one for float price) as I'm not sure what to use to copy the float value from the data, as I cannot treat it as a string (the integer value right below it seems to let me get away with it in my compiler).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Struct for linked list node
struct produceItem
{
char produce[20];
char type[20];
char soldBy[20];
float price;
int quantityInStock;
struct produceItem *next;
};
// Function to read in data from file to
void read(struct produceItem **head)
{
struct produceItem *temp = NULL;
struct produceItem *right = NULL;
//char ch[3];
char line[50];
char *value;
FILE *data = fopen("RecitationFiveInput.txt", "r");
printf("Trying to open file RecitationFiveInput.txt\n");
if (data == NULL)
{
printf("Could not open file RecitationFiveInput.txt\n");
}
else
{
while(fgets(line, sizeof(line), data))
{
value = strtok(line, ", ");
strcpy(temp->produce, strdup(value));
value = strtok(NULL, ", ");
strcpy(temp->type, strdup(value));
value = strtok(NULL, ", ");
strcpy(temp->soldBy, strdup(value));
//value = strtok(NULL, ", ");
//strcpy(temp->price, strdup(value));
value = strtok(NULL, " \n");
strcpy(temp->quantityInStock, strdup(value));
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
right = *head;
while(right->next != NULL)
{
right = right->next;
}
right->next = temp;
}
}
printf("Successfully opened file RecitationFiveInput.txt\n");
}
fclose(data);
return;
}
// Function to display the nodes of the linked list that contains the data from the data file
void display(struct produceItem *head)
{
int value = 1;
struct produceItem *temp = NULL;
temp = head;
printf("=============================================================================\n");
printf(" Item # Produce Type Sold By Price In Stock\n");
printf("=============================================================================\n");
if(temp == NULL)
{
return;
}
else
{
while(temp != NULL)
{
printf(" %d %s %s %s %lf %d\n", value, temp->produce, temp->type, temp->soldBy, temp->price, temp->quantityInStock);
value++;
temp = temp->next;
if(temp == NULL)
{
break;
}
}
}
return;
}
//Main function
int main()
{
int input = 0;
struct produceItem *head = NULL;
while(1)
{
printf("\nList Operations\n");
printf("=================\n");
printf("1. Stock Produce Department\n");
printf("2. Display Produce Inventory\n");
printf("3. Reverse Order of Produce Inventory\n");
printf("4. Export Produce Inventory\n");
printf("5. Exit Program\n");
printf("Enter your choice: ");
if(scanf("%d", &input) <= 0)
{
printf("Enter only an integer.\n");
exit(0);
}
else
{
switch(input)
{
case 1:
read(&head);
break;
case 2:
display(head);
break;
case 3:
//function
break;
case 4:
//function
break;
case 5:
printf("You have exited the program, Goodbye!\n");
return 0;
break;
default:
printf("Invalid option.\n");
}
}
}
return 0;
}
Never mind everyone, found the issue. The crashes were due to me not allocating memory for the temp pointer in the read me function.

how to write display driver

I am writing display drivers for micro oled.
board is dart4460 (omap4460) which provides dss(display subsystem).
so I am writing drivers using dss.
but I dont know what I wrote is right or not
oled display use dpi interface and i2c for commands
I referred to pico dlp projector driver source which uses dpi and i2c.
here are datasheets
dart4460: http://www.variscite.com/images/DART-4460-DS_107.pdf
micro oled display: https://www.dropbox.com/s/ixpws4qzo3ttj6e/SVGA050.pdf?dl=0
Code:
panel-svga.c
#define SLAVE_ADDR_READ 0x1F
#define SLAVW_ADDR_WRITE 0x1E
struct svga050_i2c_data {
struct mutex xfer_lock;
};
struct svga050_data {
struct i2c_client *client;
struct mutex lock;
};
static struct i2c_board_info svga050_i2c_board_info = {
I2C_BOARD_INFO("svga050_i2c_drive",SLAVE_ADDR_WRITE);
}
static struct omap_video_timings svga050_timings = {
.x_res = 800,
.y_res = 600,
.pixel_clock = 40000,
.hsw = 128,
.hfp = 40,
.hbp = 88,
.vsw = 4,
.vfp = 1,
.vbp = 23,
};
static int svga050_panel_power_on(struct omap_dss_device *dssdev)
{
int r;
if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
return 0;
r = omapdss_dpi_display_enable(dssdev);
if (r)
goto err0;
if (dssdev->platform_enable) {
r = dssdev->platform_enable(dssdev);
if (r)
goto err1;
}
return 0;
err1:
omapdss_dpi_display_disable(dssdev);
err0:
return r;
}
static void svga050_panel_power_off(struct omap_dss_device *dssdev)
{
if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
return;
if (dssdev->platform_disable)
dssdev->platform_disable(dssdev);
omapdss_dpi_display_disable(dssdev);
}
static inline struct svga050_panel_data *get_panel_data(const struct omap_dss_device *dssdev)
{
return (struct svga050_panel_data *)dssdev->data;
}
static int svga050_panel_probe(struct omap_dss_device *dssdev)
{
struct svga050_data *svga_data;
struct i2c_adapter *adapter;
struct i2c_client *svga_i2c_client;
struct svga050_panel_data *svga_pdata=get_panel_data(dssdev);
int r;
dssdev->panel.config = OMAP_DSS_LCD_TFT | OMAP_DSS_LCD_IVS |
OMAP_DSS_LCD_IHS;
dssdev->panel.timings = svga050_timings;
svga_data = devm_kzalloc(&dssdev->dev,sizeof(*svga_data), GFP_KERNEL);
if (!svga_data) {
r = -ENOMEM;
goto err;
}
mutex_init(&ld->lock);
dev_set_drvdata(&dssdev->dev, ld);
return 0;
err:
return r;
}
static void svga050_panel_remove(struct omap_dss_device *dssdev)
{
struct svga050_data *ld = dev_get_drvdata(&dssdev->dev);
kfree(ld);
}
static int svga050_panel_enable(struct omap_dss_device *dssdev)
{
struct svga050_data *ld = dev_get_drvdata(&dssdev->dev);
int r;
mutex_lock(&ld->lock);
r = svga050_panel_power_on(dssdev);
if (r)
goto err;
dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
mutex_unlock(&ld->lock);
return 0;
err:
mutex_unlock(&ld->lock);
return r;
}
static void svga050_panel_disable(struct omap_dss_device *dssdev)
{
struct svga050_data *ld = dev_get_drvdata(&dssdev->dev);
mutex_lock(&ld->lock);
svga050_panel_power_off(dssdev);
dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
mutex_unlock(&ld->lock);
}
static int svga050_panel_suspend(struct omap_dss_device *dssdev)
{
struct svga050_data *ld = dev_get_drvdata(&dssdev->dev);
mutex_lock(&ld->lock);
svga050_panel_power_off(dssdev);
dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
mutex_unlock(&ld->lock);
return 0;
}
static int svga050_panel_resume(struct omap_dss_device *dssdev)
{
struct svga050_data *ld = dev_get_drvdata(&dssdev->dev);
int r;
mutex_lock(&ld->lock);
r = svga050_panel_power_on(dssdev);
if (r)
goto err;
dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
mutex_unlock(&ld->lock);
return 0;
err:
mutex_unlock(&ld->lock);
return r;
}
static struct omap_dss_driver svga050_driver = {
.probe = svga050_panel_probe,
.remove = svga050_panel_remove,
.enable = svga050_panel_enable,
.disable = svga050_panel_disable,
.suspend = svga050_panel_suspend,
.resume = svga050_panel_resume,
.driver = {
.name = "svga050",
.owner = THIS_MODULE,
},
};
static int svga050_i2c_read(struct i2c_client *client, u8 reg)
{
u8 read_cmd[] = { SLAVE_ADDR_READ, reg }, data;
struct svga050_i2c_data *picodlp_i2c_data = i2c_get_clientdata(client);
struct i2c_msg msg[2];
mutex_lock(&svga050_i2c_data->xfer_lock);
msg[0].addr = client->addr;
msg[0].flags = 0;
msg[0].len = 2;
msg[0].buf = read_cmd;
msg[1].addr = client->addr;
msg[1].flags = I2C_M_RD;
msg[1].len = 2;
msg[1].buf = data;
i2c_transfer(client->adapter, msg, 2);
mutex_unlock(&svga050_i2c_data->xfer_lock);
return data;
}
static int svga050_i2c_write(struct i2c_client *client, u8 reg, u8 value)
{
u8 data[2];
int i;
struct i2c_msg msg;
int i, r, msg_count = 1;
struct svga050_i2c_data *picodlp_i2c_data = i2c_get_clientdata(client);
data[0] = reg;
data[1] = value;
mutex_lock(&svga050_i2c_data->xfer_lock);
msg.addr = client->addr;
msg.flags = 0;
msg.len = 2;
msg.buf = data;
r = i2c_transfer(client->adapter, &msg, msg_count);
mutex_unlock(&svga050_i2c_data->xfer_lock);
/*
* i2c_transfer returns:
* number of messages sent in case of success
* a negative error number in case of failure
*/
if (r != msg_count)
goto err;
/* In case of success */
for (i = 0; i < 2; i++)
dev_dbg(&client->dev,
"addr %x bw 0x%02x[%d]: 0x%02x\n",
client->addr, data[0] + i, i, data[i]);
return 0;
err:
dev_err(&client->dev, "svga050_i2c_write error\n");
return r;
}
static int svga050_i2c_write_array(struct i2c_client *client,
const struct svga050_i2c_command commands[],
int count)
{
int i, r = 0;
for (i = 0; i < count; i++) {
r = svga050_i2c_write(client, commands[i].reg,
commands[i].value);
if (r)
return r;
}
return r;
}
static void init_svga050_panel(struct spi_device *spi)
{
}
static int __devinit svga050_panel_i2c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
struct svga050_i2c_data *svga_i2c_data;
svga_i2c_data=kzalloc(sizeof(struct svga050_i2c_data),GFP_KERNEL);
if(svga_i2c_data == NULL)
return -ENOMEM;
i2c_set_clientdata(client,svga_i2c_data);
mutex_init(&svga_i2c_data->xfer_lock);
dev_err(&client->dev,"svga i2c initialized\n");
return 0;
}
static int __devexit svga050_panel_i2c_remove(struct i2c_client *client)
{
struct svga050_i2c_data *sd1= i2c_get_clientdata(client);
i2c_set_clientdata(client,NULL);
kfree(sd1);
return 0;
}
static const struct i2c_device_id svga050_i2c_idtable[]={
{"svga050_i2c_driver",0},
{},
};
static struct i2c_driver svga050_i2c_driver = {
.driver = {
.name = "svga050_i2c",
.owner = THIS_MODULE,
},
.probe = svga050_panel_i2c_probe,
.remove = __exit_p(svga050_panel_i2c_remove),
.id_table = svga050_i2c_idtable,
};
static int __init svga050_panel_drv_init(void)
{
int r;
r= i2c_add_driver(&svga050_i2c_driver);
if(r < 0){
printk(KERN_WARNING "svga050 i2c driver registration failed\n");
return r;
}
r=omap_dss_register_driver(&svga050_driver);
if(r < 0){
printk(KERN_WARNING "svga050 dss driver registration failed\n");
i2c_del_driver(&svga050_i2c_driver);
}
return r;
}
static void __exit svga050_panel_drv_exit(void)
{
omap_dss_unregister_driver(&svga050_driver);
i2c_del_driver(&svga050_i2c_driver);
}
module_init(svga050_panel_drv_init);
module_exit(svga050_panel_drv_exit);
MODULE_LICENSE("GPL");
Board.c
static struct omap_dss_device svga050_device = {
.name = "svga050",
.driver_name = "svga050",
.type = OMAP_DISPLAY_TYPE_DPI,
.phy.dpi.data_lines = 24,
.channel = OMAP_DSS_CHANNEL_LCD2,
.platform_enable = svga050_panel_enable_picodlp,
.platform_disable = svga050_panel_disable_picodlp,
.data = &svga050_pdata,
};
static struct omap_dss_device *svga050_dss_devices[] = {
&svga050_device,
};
static struct picodlp_panel_data sdp4430_picodlp_pdata = {
.svga050_adapter_id = 2,
};
my questions are :
My code is right?
I don't know how to write display init code by seeing datasheet.
Can I write display init code by seeing this datasheet ?
In panel_probe function, how can I get adapter ID ?
how do I choose adapter I ?
Is it right that I should write only i2c slave driver code in panel code ?
How can I select I2C master ? I want to use I2C3 or I2C4 for display commands

The Reason Segmentation Fault in Linked List

#include <iostream>
#include <string>
using namespace std;
struct Node{
Node *next;
int data;
};
int main(){
Node* head = NULL;
int data;
cin >> data;
Node*m = head;
while(data >0){
cout <<"enter a data";
cin >> data;
m -> data = data;
m -> next = m;
}
while(m -> next != NULL){
cout << m -> data << endl;
}
return 0;
}
Here is simple code that takes values when they are greater than 0 and make a linked list. after you enter a negative value, the while loop is terminated and prints the values.
However, the code gives me segmentation fault when it asks enter a data and after it takes the data. I could not solve it, what is the reason?
You get uninitialized pointer here. If you define link to next node as Node *next; you should initialise pointer with address to valid Node object before use.
And
Node* head = NULL;
...
Node*m = head;
...
m -> data = data; // NPE
as you can see, you trying to call member data of NULL object.
How to fix it:
Anways initialize pointer with valid address. For example:
int main(){
Node head;
head.next = NULL;
int data;
cout << "enter a data ";
cin >> data;
Node *prev = &head;
prev->data = data;
while(data >0){
Node *next = new Node();
cout <<"enter a data ";
cin >> data;
next->data = data;
next->next = NULL;
prev->next = next;
prev = next;
}
Node* m = &head;
while(m -> next ){
cout << m -> data << endl;
m = m->next;
}
return 0;
}

Resources