#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
struct nodeStack{
char operator;
struct nodeStack *next;
};
typedef struct nodeStack node;
node *start=NULL;
node *tail=NULL;
int top=-1;
int isEmpty()
{
if(top==-1)
return 1;
}
void push(char c){
node *tempNode,*tail;
tempNode=(node*) malloc(sizeof(node));
if(tempNode==NULL){
printf("Memory Unvailable\n");
return;
}
tempNode->operator=c;
if(start==NULL){
start=tempNode;
tail=start;
tempNode->next=NULL;
top++;
}
else{
tail->next=tempNode;
tempNode->next=NULL;
tail=tail->next;
top++;
}
}
/*
struct node* pop(){
if(top==-1){
printf("stack is empty");
return;
}
else
{
node *temp;
temp=start;
while(temp->next!=tail){
temp->next=NULL;
free(tail);
tail=temp;
}
}
}*/
void displayStack(){
node *i;
for(i=start;i!=tail;i=i->next){
printf("%c -> ",i->operator);
}
}
int main(){
int i;
int flag=1;
char choice='y';
printf("pushing data into the stack......");
while(flag==1){
char ch;
printf("enter a character\n");
scanf(" %c",&ch);
push(ch);
printf("want to push more operator (y\n)");
scanf(" %c",choice);
if(choice=='y')
flag=1;
else
flag=0;
}
displayStack();
return 0;
}
Its giving me segmentation error when I am trying to run this.
It is accepting only one input and not taking further and at the same time it is giving segmentation error
Its giving me segmentation error when I am trying to run this.
It is accepting only one input and not taking further and at the same time it is giving segmentation error
Take a look at:
scanf(" %c",choice);
The second parameter should be passed as address, because scanf modifies it.
Try to modify with:
scanf("%c", &choice);
I'm not going to explain the reason in detail, because I think you just made a mistake since in some previous lines scanf(" %c",&ch); you've invoke right.
Note: I'd avoid the space in the scanf string format.
This is a bit modified code! Working code!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct nodeStack{
char op;
struct nodeStack *next;
}node;
node *start = NULL;
node *tail = NULL;
int top=-1;
int isEmpty()
{
if(top==-1)
return 1;
else return 0;
}
void push(char c){
node *tempNode;
tempNode=(node*) malloc(sizeof(node));
tempNode->op=c;
if(start==NULL){
//start = (node*) malloc(sizeof(node));
start=tempNode;
tail=start;
tempNode->next=NULL;
top++;
}
else{
tail->next=tempNode;
tempNode->next=NULL;
tail=tail->next;
top++;
}
}
/*
struct node* pop(){
if(top==-1){
printf("stack is empty");
return;
}
else
{
node *temp;
temp=start;
while(temp->next!=tail){
temp->next=NULL;
free(tail);
tail=temp;
}
}
}*/
void displayStack(){
node *i;
for(i=start;i!=NULL;i=i->next){
printf("%c -> ",i->op);
}
}
int main(){
int i;
int flag=1;
char choice='y', ch;
printf("pushing data into the stack......");
start=NULL; tail=NULL;
while(flag){
printf("enter a character\n");
scanf(" %c",&ch);
push(ch);
printf("want to push more operator (y/n)");
scanf(" %c",&choice);
if(choice=='y')
flag=1;
else
flag=0;
}
displayStack();
return 0;
}
Related
I am having trouble inserting the second element the program just waits and exits, the first element is printing correctly, the problem occurs when I try to enter 2nd element using function Insert.
please help
case 7: uses the function Insert which passes a "d" which is data,and Display
prints the list.
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
void Insert(int d){
struct node *temp,*newnode;
newnode= (struct node*) malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("not there\n");
}
else{newnode->data= d;
newnode->next=NULL; }
if(head==NULL)
{
head=newnode;temp=newnode; count++;
}
else
{
temp->next=newnode;
temp=newnode;
count++;
}
}
void Display(){
struct node*temp;
temp=head;
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
}
void main()
{ int c=0;int d;
do{
printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position\n 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert\n 8. Exit\n");
scanf("%d",&c);
switch (c) {
case 7: printf("enter element\n");
scanf("%d",&d);Insert(d);Display();break;
default : c=8; break;
}
}while(c!=8);
}
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*temp;
void Insert(int d){
struct node *newnode;
newnode= (struct node*) malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("not there\n");
}
else{newnode->data= d;
newnode->next=NULL; }
if(head==NULL)
{
head=newnode;temp=newnode; count++;
}
else
{
temp->next=newnode;
temp=newnode;
count++;
}
}
void Display(){
struct node*temp;
temp=head;
while(temp!=NULL){
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n", );
}
void main()
{ int c=0;int d;
do{
printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position\n 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert\n 8. Exit\n");
scanf("%d",&c);
switch (c) {
case 7: printf("enter element\n");
scanf("%d",&d);Insert(d);Display();break;
default : c=8; break;
}
}while(c!=8);
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
while(n!=NULL)
{
printf("%d ",n->info);
n=n->next;
}
}
int main()
{
printf("Hello world!\n");
list head,temp;
char ch;
head=NULL;
printf("Want to add data:\n");
scanf("%c",&ch);
while(ch=='y'||ch=='Y')
{
temp=(list)malloc(sizeof(Node));
scanf("%d",&temp->info);
temp->next=head;
head=temp->next;
printf("Want to add more data:\n");
scanf("%c",&ch);
}
printlist(head);
return 0;
}
this is my code.
my problem is here that I cannot and data in my list but the node is added ...
I think there is something wrong in my "scanf" function....
please help me to solve this problem and send me the corrected code
thank u...hope I can get a reply soon
Try changing head=temp->next to head=temp. You are assigning head to itself again.
In addition to the above answers, if you wish to maintain the order in which the elements are added to a linked list (the head always remains fixed, only changes to point to the first element if it was NULL initially), the following adjustments take care of that. Any new element is always added to the end of the linked list with the head fixed at the first element.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
while(n!=NULL)
{
printf("%d ",n->info);
n=n->next;
}
}
int main(){
printf("Hello world!\n");
list head,temp;
char ch;
head=NULL;
printf("Want to add data:\n");
scanf("%c",&ch);
while(ch=='y'||ch=='Y'){
temp=(list)malloc(sizeof(Node));
scanf("%d",&temp->info);
temp->next=NULL;
if(head == NULL){
head = temp;
}
else{
list temp2 = head;
while(temp2->next != NULL){
temp2 = temp2->next;
}
temp2->next = temp;
}
printf("Want to add more data:\n");
scanf(" %c",&ch);
}
printlist(head);
return 0;
}
Change your code as below. scanf("%c",&ch); to scanf(" %c",&ch); and head=temp->next; to head=temp; For Scanf see below link
See link scanf() function doesn't work?
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
while(n!=NULL)
{
printf("%d ",n->info);
n=n->next;
}
}
int main()
{
printf("Hello world!\n");
list head,temp;
char ch;
head=NULL;
printf("Want to add data:\n");
scanf(" %c",&ch);
while(ch=='y'||ch=='Y')
{
temp=(list)malloc(sizeof(Node));
scanf("%d",&temp->info);
temp->next=head;
head = temp;
printf("Want to add more data:\n");
scanf(" %c",&ch);
}
printlist(head);
return 0;
}
I'm having trouble implementing a Stack using a linked list with struct. The program compiles fine but when I run it, it prints the first element but then reads the next node as a NULL. I think it might be an error with my passing of the stack to the push method but I am not sure and I have not been successful in fixing it so I'm asking for your help:
#include <stdio.h>
#include <stdlib.h>
struct stackNode{
char data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
void convertToPostfix(char infix[], char postfix[]);
int isOperator(char c);
int precedence(char operator1, char operator2);
void push(StackNodePtr *topPtr, char value);
char pop(StackNodePtr *topPtr);
char stackTop(StackNodePtr topPtr);
int isEmpty(StackNodePtr topPtr);
void printStack(StackNodePtr topPtr);
int main(){
convertToPostfix(NULL, NULL);
return 0;
}
void convertToPostfix(char infix[], char postfix[]){
StackNode stack = {'(', NULL};
StackNodePtr stackPtr = &stack;
push(stackPtr, 'a');
//printf("%s\n", stackPtr->data);
printStack(&stack);
}
void push(StackNodePtr *topPtr, char value){
StackNode *node;
node=(StackNodePtr)malloc(sizeof(StackNodePtr));
node->data=value;
node->nextPtr=*topPtr;
*topPtr=node;
}
void printStack(StackNodePtr topPtr){
if(topPtr == NULL){
printf("%s\n", "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
return;
}
printf("%c\n", topPtr->data);
printStack(topPtr->nextPtr);
}
Any help would be appreciated.
Thanks
Several problems I could see:
1) printStack(&stack); should be printStack(stackPtr); as you are passing address of stackPtr to the push function.
2)
node = (StackNodePtr)malloc(sizeof(StackNodePtr));
should be:
node = malloc(sizeof(StackNode));
3)
push(stackPtr, 'a');
should be:
push(&stackPtr, 'a');
As you need to pass the address of the top pointer.
This is incorrect:
node=(StackNodePtr)malloc(sizeof(StackNodePtr));
as it is only allocating memory for a struct stackNode* (commonly 4-bytes for any pointer type), when it should be allocating memory for a struct stackNode (at least 5 bytes):
node = malloc(sizeof(StackNode));
--
See Do I cast the result of malloc?
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * link;
};
void push(struct node **, int);
int pop(struct node **);
void display(struct node *);
void printMenu();
int main() {
struct node * p;
p = NULL;
int data, ch, data1;
//char choice[10];
do {
printMenu();
printf("Enter your choice\n");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to be pushed\n");
scanf("%d", &data);
push(&p, data);
break;
case 2:
data1 = pop(&p);
if (data1 != -1000)
printf("The popped element is %d\n", data1);
break;
case 3:
printf("The contents of the stack are");
display(p);
printf("\n");
break;
default:
return 0;
}
} while (1);
return 0;
}
void printMenu() {
printf("Choice 1 : Push\n");
printf("Choice 2 : Pop\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
}
void push(struct node **q, int num) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->link = (*q);
temp->data = num;
(*q) = temp;
}
void display(struct node *q) {
//Fill in the code here
struct node *temp = q;
if (temp == NULL)
printf(" {}");
while (temp != NULL)
{
printf(" %d", temp->data);
temp = temp->link;
}
}
int pop(struct node **q) {
//Fill in the code here
struct node *temp;
int item;
if (*q == NULL)
{
printf("Stack is empty\n");
return -1000;
}
temp = *q;
item = temp->data;
*q = (*q)->link;
free(temp);
return item;
}
I am getting below error message.
Could not able to solve it. Googled a lot. Finally thought of to put it here.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int stop;
struct stack
{
int data;
struct stack *next;
};
typedef struct stack *node, *top;
//typedef struct stack *top;
void push()
{
int i;
struct stack *x;
x = malloc(sizeof(struct stack));
printf("\n Enter the element your want to insert");
scanf("%d", &i);
x->data = i;
x->next = top;
top = x;
}
void pop()
{
int i;
if(top == NULL)
{
printf("\nStack is empty\n");
}
else{
i = top->data;
free(top);
top = top->next;
}
}
void display()
{
if(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int main()
{
int ch;
while(1)
{
printf("\nEnter your option \n1. Insert(Push) \n2. Delete(Pop) \n3. Display : \n");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("Invalid Entery, Try Again");
}
}
return 0;
}
remove typedef and all will be fine.
You don't want a new type:
typedef struct stack *node, *top;
Instead, you want a new variable:
struct stack *node, *top;
You cannot combine variable declarations with typedef. If you would like to create an alias for struct stack and call it simply stack, modify your code as follows:
struct stack {
int data;
struct stack *next;
};
typedef struct stack stack;
stack *node, *top;
There are huge amounts of error, Seriously what's wrong ?
I tried using it wihout typedef but what's the problem? can anyone help me debug this please?
struct node {
int info;
struct node *link;
};
int main (void)
{
int choice;
struct node *top;
top = NULL;
while (1) {
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}
}
return 0;
}
void push (void)
{
struct node *tmp;
int pushed_item;
tmp = malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d", &pushed_item);
tmp->info = pushed_item;
tmp->link = top;
top = tmp;
}
void pop (void)
{
struct node *tmp;
if (top == NULL)
printf("Stack is empty\n");
else {
tmp = top;
printf("Popped item is %d\n", tmp->info);
top = top->link;
free(tmp);
}
}
void display (void)
{
struct node *ptr;
ptr = top;
if (top == NULL)
printf("Stack is empty\n");
else {
printf("Stack elements :\n");
while (ptr != NULL) {
printf("%d\n", ptr->info);
ptr = ptr->link;
}
}
}
First off, the main function is going to have to be below the functions it calls. Secondly, you need to #import <stdio.h> to use printf. Thirdly, top is not a global variable so you can't just use it inside the display function.
Work from there.
Try putting this snippet at the top of your file:
#include <stdio.h>
#include <stdlib.h>
void push(void);
void pop(void);
void display(void);
struct node* top; // good catch by Borealid