storing a string in linked list in c - linked-list

i am very new in programming and its my 2nd semester in my college an dour teacher teach us about linked list and malloc function where we can make a node dynamically and increase or decrease its size . our teacher teach us how to store a integer value in list like this
struct node
{
int num;
struct node *next;
}head;
but i want to store a string say "name" of students in linked list instead of integer .. so i want to know how to do this ..
i write this code for that
#include <stdio.h>
#include <conio.h>
struct node
{
char name[20];
int age;
struct node *next;
};
//
void addatbegin(struct node **q,char name [20],int age)
{
struct node *r;
r=(struct node *)malloc(sizeof(struct node));
r->name=name;
r->age=age;
r->next=*q;
*q=r;
}
//
void addatend(struct node **q,char name[20],int age)
{
struct node *temp,*r;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->name=name;
temp->age=age;
temp->next=NULL;
*q=temp;
}
else {
temp=*q;
while(temp->next!=NULL)
{
temp=temp->next;
}
r=(struct node *)malloc(sizeof(struct node));
r->name=name;
r->age=age;
r->next=NULL;
temp->next=r;
}}
void main()
{
struct node *head;
int age,ch;
char name[20];
head=NULL;
while(1)
{
printf("***** MENU ****** \n");
printf("1.Add at Begining of List\n");
printf("2.Add at End of List\n");
printf("3.Print the list\n");
printf("4.Exit\n \n");
printf("Enter Your choice:- ");
scanf(" %d",&ch);
switch(ch)
{
case1:printf("Enter Name:- ");
scanf(" %s",name);
printf("Enter Age:- ");
scanf(" %d",&age);
addatbegin(&head,name,age);
break;
}
getch(); }}
but its shwoing error lvalue missing

Related

Set the number of linked list nodes C

I have a trace text file with several numbers written on it.
I want to set the number of output nodes to 100, 500, or 1000.
I want to measure the hit rate of the implemented FiFO page replacement algorithm using the given trace file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
char *string;
struct list *next;
};
typedef struct list LIST;
int main(void) {
FILE *fp;
char line[128];
LIST *current, *head;
head = current = NULL;
fp = fopen("test.txt", "r");
while(fgets(line, sizeof(line), fp)){
LIST *node = malloc(sizeof(LIST));
node->string = strdup(line);//note : strdup is not standard function
node->next =NULL;
if(head == NULL){
current = head = node;
} else {
current = current->next = node;
}
}
fclose(fp);
//test print
for(current = head; current ; current=current->next){
printf("%s", current->string);
}
//need free for each node
return 0;
}

Size of circular list in C++ using Double Pointer

The function in resulting in an infinite loop. It is not able to return the number of nodes. Where am I going wrong?
int Count(struct node **head)
{
printf("entered");
struct node **temp;
int count = 0;
temp = &((*head)->next);
while ((&(*temp)->next) != head)
{
printf("entered");
temp = &((*temp)->next);
count++;
}
return count;
}
In the provide source code, the while-condition is referring to addresses of pointers (where the pointer is stored) and the struct node **head is pointing to a static location in the main() but (&(*temp)->next) is pointing to the allocated of the last item.
To compare items in a linked-list, you should compare pointers struct
node * instead of addresses of pointers struct node **.
In the Count() function, because the *head exists (not compared to
NULL), the counter count should start from 1 and to count all items in the circular-list, you should start by temp = &(*head); instead of the next item temp = &((*head)->next);.
Here after is a "Minimal, Complete, and Verifiable example".
#include <stdio.h>
#include <stdlib.h>
#define NODE_MAX (5)
struct node {
struct node *next;
};
int Count(struct node **head)
{
printf("entered");
struct node **temp;
int count = 1; // at least the '*head' node exists
temp = &(*head);// &((*head)->next);
while (((*temp)->next) != *head) // use '*head'
{
printf("entered");
temp = &((*temp)->next);
count++;
}
return count;
}
int main()
{
struct node array[NODE_MAX];
struct node *head, *temp;
head = &(array[0]);
temp = head;
for(int i=1;i<NODE_MAX;i++) {
temp->next = &(array[i]);
temp = temp->next;
}
temp->next = &(array[0]);
printf("\nCount = %d\n",Count(&head));
system("pause");
return (0);
}
It would be more simple to manage linked-list at pointers level like the following example:
int Count2(struct node **head)
{
printf("entered");
struct node *temp;
int count = 1;
temp = (*head); // pointers to the first
while (temp->next != *head) // direct pointer comparison
{
printf("entered");
temp = temp->next; // natural linked-list exploration
count++;
}
return count;
}

Adding a node to linked list

I have written my first datastructures code in C and I am baffled as to what I am doing wrong. I am just trying to add a node to the front of the linked list or to an empty linked list and print the list at the end and it is resulting in segmentation fault.
#include<stdio.h>
#include<stddef.h>
#include<cstdlib>
/* Node representing each node of the linked list */
struct Node {
int data;
struct Node *next;
};
/* Fist node is always null as there are no nodes in the linked list to begin with */
struct Node *first = NULL;
void add_node(int data) {
struct Node *newptr = (Node *)malloc(sizeof(Node));
// Check if the list is empty
if (first == NULL){
printf("The list is empty\n");
newptr->data = data;
newptr->next = NULL;
first = newptr;
}
else {
printf("Adding to the existing list\n");
printf("Data in the first node is %d",first->data);
}
}
void display() {
struct Node *ptr;
printf("In the display function\n");
ptr = first;
do {
printf("Printing the data in the node %d",ptr->data);
ptr= ptr->next;
}while(ptr->next != NULL);
}
int main() {
/*
* Just try and add one node
*/
int y = 100;
printf("Adding a node \n");
add_node(y);
display();
return 1;
}
I messed up the display function, I changed it a bit to have the correct output.
The following is the new display function:
void display(struct Node *first) {
struct Node *ptr;
printf("In the display function\n");
ptr = first;
do {
printf("Printing the data in the node %d",ptr->data);
ptr= ptr->next;
}while(ptr != NULL);
}

Implements stack using linked list - is all this code needed?

I code what implementing stack using linked list as I coded I look some of code that somebody coded I have something awkward.
The point that I ask is what is different when I use
if (top == NULL){
top=temp;
top->next=NULL;
}
this part with when I don't use this. Is it needed? In my opinion that part is needless.
This is my full code
#include <stdio.h>
#include <stdlib.h>
#define Max 5
struct Node{
int value;
struct Node *next;
};
struct Node *top;
int count;
int main(){
int pop();
void push(int);
int stack_full();
int stack_empty();
void printStack();
void init();
void freeAllRes();
int opt;
int value;
init();
printf("initialization is performed!\n\n");
printf("1. Push to stack");
printf("\n2. Pop from Stack");
printf("\n3. values in Stack");
printf("\n4. Exit\n");
while(1){
printf("\nChoose Option : ");
scanf("%d",&opt);
switch(opt){
case 1:
if(stack_full()==-1){
printf("Error : Stack is full!!");
break;
}
else{
printf("\nEnter a value to push into Stack : ");
scanf("%d",&value);
push(value);
break;
}
case 2:
if(stack_empty()==-1){
printf("Error : Stack is empty!!");
break;
}
else{
printf("Popped num : %d ",pop());
break;
}
case 3:
if(stack_empty()==-1){
printf("Error : Stack is empty!!");
break;
}
else{
printStack();
break;
}
case 4:
freeAllRes();
printf("All resources are free!");
exit(0);
default:
printf("Error : wrong choice!!");
break;
}
}
}
int stack_full(){
if(count==Max)
return -1;
else
return 0;
}
int stack_empty(){
if(top==NULL)
return -1;
else
return 0;
}
void init(){
top=NULL;
count=0;
}
void freeAllRes(){
struct Node *temp;
while(top!=NULL){
temp = top->next;
free(top);
top=temp;
}
}
int pop(){
int popNum;
struct Node *temp=top;
popNum = temp->value;
top = top->next;
free(temp);
count--;
return popNum;
}
void push(int num){
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
if(temp==NULL){
printf("Error : malloc fail!!");
exit(0);
}
temp->value=num;
/*
if (top == NULL){
top=temp;
top->next=NULL;
}
*/
//else{
temp->next=top;
top=temp;
//}
count++;
}
void printStack(){
struct Node *temp=top;
printf("\nElements are as:\n");
while(temp!=NULL){
printf(" %d ",temp->value);
temp=temp->next;
}
}
And how to I code this code fancy and efficient and nice?

Sorting char array in ascending order (with Link list)

I want to arrange my link list (which contains char arrays) in ascending order. This program should allow the user to input some names and then display them in ascending order. I have used the strncpy function. There are no compilation errors.But instead of names, the output gives some integers (perharps addresses). Please help me! I am new to C!
#include <stdio.h>
#include <malloc.h>
#include <string.h>
char name [10];
struct node
{
char nm [10];
struct node *next;
}*newnode, *prev, *temp, *display, *current, *list;
void createlist()
{
list=NULL;
};
void insert ()
{
newnode=(struct node*) malloc (sizeof (struct node));
printf("Enter the Name: ");
scanf("%s",&name);
strncpy(newnode->nm,name, 10);
newnode->next=NULL;
if (list==NULL)
{
list=newnode;
}
else if (name<list->nm)
{
newnode->next=list;
list=newnode;
}
else
{
temp=list;
int place;
place=0;
while (temp!=NULL && place ==0)
{
if (name>temp->nm)
{
prev=temp;
temp=temp->next;
}
else
{
place=1;
}
newnode->next=prev->next;
prev->next=newnode;
}
}
}
void displayname()
{
if (list==NULL)
printf("\n\nList is empty");
else
{
display=list;
while(display!=NULL)
{
printf("%d\n",display->nm);
display=display->next;
}
}
}
int main()
{
char choice;
choice=='y';
createlist();
do
{
insert ();
printf("Do you want to continue? ");
scanf("%s",&choice);
}while (choice='y'&& choice!='n');
displayname();
}
In the display function you have
printf("%d\n",display->nm);
The %d formatter outputs the argument as an integer. Use printf's %s formatter to get character arrays
printf("%s\n",display->nm);
You will still need to write the sorting code ... put the problem of outputtin numbers instead of text.

Resources