C Language, Data Structures 1.One common use of stacks is to parse certain kinds
ID: 3540263 • Letter: C
Question
C Language, Data Structures
1.One common use of stacks is to parse certain kinds of text strings. Typically the strings are lines
of code in a computer language, and the programs parsing them are compilers.
Write a program that prompts the user to enter a string, and then checks the delimiters in the
string. The delimiters are the braces '{' and '}', brackets '[' and ']', and parenthesis '(' and ')'. Each
opening or left delimiter should be matched by closing or right delimiter; that is, every '{' should
be followed by a matching '}' and so on. Also, opening delimiters that occur later in the string
should be closed before those occurring earlier. Examples:
c[d] // correct
a{b[c]d}e // correct
a{b(c]d}e // not correct; } doesn't match (
a[b{c}d]e} // not correct; nothing matches final }
a{b(c) // not correct; Nothing matches opening {
The program works by reading characters from the string one at a time and placing opening
delimiters, when it finds them, on a stack. When it reads a closing delimiter from the input, it
pops the opening delimiter from the top of the stack and attempts to match it with the closing
delimiter. If they're not the same type, then an error has occurred.
2.write an algorithm that compresses a string by deleting all space characters in the string.
One way to do so is to use a queue of characters. Insert nonspace characters from the string
into the queue. When you reach the end of the string, dequeue the characters from the queue
and place them back
Given a queue of integers, write an algorithm that, using only the queue ADT, calculates and
prints the sum and the average of the integers in the queue without changing the contents of the
queue.
Given a queue of integers that deletes all negative integers without changing changing the
order of the remaining elements in the queue.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//------------------------------------------
// Defination of the structure for stack
//------------------------------------------
typedef struct stack{
char character;
struct stack *link;
}stack;
//----------------------------------------------
// Push function for stack
//----------------------------------------------
void push(struct stack **top, char character)
{
struct stack *temp;
temp=(struct stack*)malloc(sizeof(struct stack));
temp->character = character;
temp->link = *top;
*top = temp;
}
//----------------------------------------
// isEmpty function for stack
//----------------------------------------
int isEmpty(struct stack **top)
{
if(*top == NULL)
return 0;
else
return 1;
}
//--------------------------------
// Pop function for stack
//--------------------------------
char pop(struct stack **top,int *isempty)
{
struct stack *temp=*top;
char character;
int y=isEmpty(&temp);
if(y==0)
{
*isempty = 1;
//printf("stack is empty ");
return;
}
temp = *top;
character = temp->character;
*top = temp->link;
free(temp);
return character;
}
void delimiterChecker(stack **top,char str[],int length){
char character;
int i = 0,isempty = 0;
while(str[i] != ''){
character = str[i];
switch(character){
case '[':
case '(':
case '{':
push(top,character);
break;
case ']':
case ')':
case '}':
pop(top,&isempty);
break;
//default:
}
i++;
}
int y = isEmpty(top);
//printf("y = %d ",y);
if(!(isEmpty(top)) && isempty == 0 ){
printf("You have entered a valid string. ");
}
else
printf("You have entered a invalid string. ");
}
int main(){
stack *top = NULL;
char str[15];
int length;
printf("Enter the string : ");
scanf("%s",str);
length = strlen(str);
delimiterChecker(&top,str,length);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.