#include <stdio.h>
#include <malloc.h>
typedef struct elem{
char name[ 50 ] ;
int code;
} Element;
typedef struct node{
Element elem;
struct node* next;
} Stack;
void push( Stack** head, Element el) {
Stack
* q
= ( Stack
* ) malloc ( sizeof ( Stack
) ) ; q-> elem = el;
q-> next = * head;
* head = q;
}
void pop( Stack** head) {
Stack* q = * head;
if ( isEmpty( * head) ) return ;
* head = ( * head) -> next;
}
Element top( Stack** head) {
Stack* q = * head;
Element el = ( * head) -> elem;
* head = ( * head) -> next;
return el;
}
int isEmpty( Stack* head) {
if ( head == NULL) return 1 ;
else
return 0 ;
}
void create( Stack** head) {
Stack* q;
Element e;
* head = NULL;
int n, i;
printf ( "%s" , "How many elements do you want N = " ) ; for ( i = 0 ; i < n; ++ i) {
q
= ( Stack
* ) malloc ( sizeof ( Stack
) ) ; printf ( "Element#%d:\n " , i
+ 1 ) ; //scanf("%s",q->elem.name);
//scanf("%d",&q->elem.code);
scanf ( "%s %d" , e.
name , & e.
code ) ; q-> elem = e;
q-> next = * head;
* head = q;
}
}
void write( Stack* head) {
while ( head) {
printf ( "(%s, %d)\n " , head
-> elem.
name , head
-> elem.
code ) ; head = head-> next;
}
}
void strcp( char str1[ 50 ] , char str2[ ] ) {
int i = 0 ;
while ( str2[ i] != '\0 ' ) {
str1[ i] = str2[ i] ;
i++;
}
str1[ i] = '\0 ' ;
}
int main( int argc, char const * argv[ ] ) {
Stack* head;
create( & head) ;
write( head) ;
Element el = top( & head) ;
printf ( "(%s,%d)\n " , el.
name , el.
code ) ; strcp( el.name , "adrian" ) ;
el.code = 1234 ;
push( & head, el) ;
write( head) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KCnR5cGVkZWYgc3RydWN0IGVsZW17CiAgY2hhciBuYW1lWzUwXTsKICBpbnQgY29kZTsKfSBFbGVtZW50OwoKdHlwZWRlZiBzdHJ1Y3Qgbm9kZXsKICBFbGVtZW50IGVsZW07CiAgc3R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OwogICAgICBwcmludGYoIiVzOiIsICJDb2RlPSIpOwogICAgICAvL3NjYW5mKCIlZCIsJnEtPmVsZW0uY29kZSk7CiAgICAgIHNjYW5mKCIlcyAlZCIsZS5uYW1lLCAmZS5jb2RlKTsKICAgICAgcS0+ZWxlbSA9IGU7CiAgICAgIHEtPm5leHQgPSAqaGVhZDsKICAgICAgKmhlYWQgPSBxOwogICAgfQp9Cgp2b2lkIHdyaXRlKFN0YWNrKmhlYWQpIHsKICB3aGlsZShoZWFkKSB7CiAgICBwcmludGYoIiglcywgJWQpXG4iLCBoZWFkLT5lbGVtLm5hbWUsIGhlYWQtPmVsZW0uY29kZSk7CiAgICBoZWFkID0gaGVhZC0+bmV4dDsKICB9Cn0KCnZvaWQgc3RyY3AoY2hhciBzdHIxWzUwXSxjaGFyIHN0cjJbXSkgewogICAgIGludCBpID0gMDsKICAgICB3aGlsZShzdHIyW2ldIT0nXDAnKXsKICAgICAgIHN0cjFbaV0gPSBzdHIyW2ldOwogICAgICAgaSsrOwogICAgIH0KICAgICBzdHIxW2ldID0gJ1wwJzsKfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSkgewogIFN0YWNrKmhlYWQ7CiAgY3JlYXRlKCZoZWFkKTsKICB3cml0ZShoZWFkKTsKICBFbGVtZW50IGVsID0gdG9wKCZoZWFkKTsKICBwcmludGYoIiglcywlZClcbiIsIGVsLm5hbWUsIGVsLmNvZGUpOwogIHN0cmNwKGVsLm5hbWUsImFkcmlhbiIpOwogIGVsLmNvZGUgPSAxMjM0OwogIHB1c2goJmhlYWQsZWwpOwogIHdyaXRlKGhlYWQpOwogIHJldHVybiAwOwp9
stdout
How many elements do you want N = Element#1:
Name=:Code=:Element#2:
Name=:Code=:Element#3:
Name=:Code=:(Campanera, 901)
(Ann, 321)
(May, 123)
(Campanera,901)
(adrian, 1234)
(Ann, 321)
(May, 123)