C program to perform stack operation using singly linked list
Answer Posted / bin fang
the above has bugs! for example, the count is not
decremented in Pop function...
I rewrote the Push and Pop code as follows:
node *cur = NULL;
node *head = NULL;
void Push(int info)
{
node *new;
new = (node *)malloc(sizeof(node));
new->data = info;
new->next = NULL;
if (head == NULL)
head = new;
else
cur->next = new;
cur = new;
count++;
}
void Pop(void)
{
node *pre = NULL;
node *temp = head;
while (temp != cur) {
pre = temp;
temp = temp->next;
}
printf("\n\tNode (%d) is deleted.", cur->data);
free(cur);
count--;
cur = pre;
if (cur)
cur->next = NULL;
else
head = NULL;
}
| Is This Answer Correct ? | 10 Yes | 1 No |
Post New Answer View All Answers
At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with these names could be kept in that shelf as in this example: bottom of shelf ---> [AAAJKRDFDEWAAYFYYKK]-----Top of shelf. All these packets are to be loaded on cars. The cars are lined in order, so that the packet could be loaded on them. The cars are also named [A, B, C, D, E,………….]. Each Car will load the packet with the same alphabet. So, for example, car ‘A’ will load all the packets with name ‘A’. Each particular car will come at the loading point only once. The cars will come at the loading point in alphabetical order. So, car ‘B’ will come and take all the packets with name ‘B’ from the shelf, then car ‘C’ will come. No matter how deep in the shelf any packet ‘B’ is, all of the ‘B’ packets will be displaced before the ‘C’ car arrives. For that purpose, some additional shelves are provided. The packets which are after the packet B, are kept in those shelves. Any one of these shelves contains only packets, having the same name. For example, if any particular shelf is used and if a packet with name X is in it, then only the packets having names X will be kept in it. That shelf will look like [XXXXXXX]. If any shelf is used once, then it could be used again only if it is vacant. Packets from the initial shelf could be unloaded from top only. Write a program that finds the minimum total number of shelves, including the initial one required for this loading process.
What is the maximum length of an identifier?
What is pre-emptive data structure and explain it with example?
Can we compile a program without main() function?
If I have a char * variable pointing to the name of a function ..
Are there any problems with performing mathematical operations on different variable types?
write a c program to calculate sum of digits till it reduces to a single digit using recursion
Write a program to maintain student’s record. Record should
not be available to any unauthorized user. There are three
(3) categories of users. Each user has its own type. It
depends upon user’s type that which kind of operations user
can perform. Their types and options are mentioned below:
1. Admin
(Search Record [by Reg. No or Name], View All Records,
Insert New Record, Modify Existing Record)
2. Super Admin
(Search Record [by Reg. No or Name], View All Records,
Insert New Record, Modify Existing Record, Delete Single Record)
3. Guest
(Search Record [by Reg. No or Name], View All Records)
When first time program runs, it asks to create accounts.
Each user type has only 1 account (which means that there
can be maximum 3 accounts). In account creation, following
options are required:
Login Name: <6-10 alphabets long, should be unique>
Password: <6-10 alphabets long, should not display
characters when user type>
Confirm Password:
What is a static variable in c?
Can one function call another?
write a program to display all prime numbers
How will you find a duplicate number in a array without negating the nos ?
How are variables declared in c?
What is assignment operator?
How can I find out how much free space is available on disk?