Skip to content

Instantly share code, notes, and snippets.

@douglas-vaz
Forked from tanayseven/gist:2928879
Created June 14, 2012 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglas-vaz/2928890 to your computer and use it in GitHub Desktop.
Save douglas-vaz/2928890 to your computer and use it in GitHub Desktop.
Stack
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int info;
struct stack *next;
};
typedef struct stack *s;
void push(s *sptr ,int p)
{
s s2;
s2=(s)malloc(sizeof(struct stack));
if(s2==NULL)
{
printf("no memory");
exit(1);
}
else
{
s2->info=p;
s2->next=*sptr;
*sptr=s2;
}
}
int pop(s *sptr)
{
s s2=*sptr; //Why???
int px;
if(*sptr == NULL)
{
printf("stack is empty");
return;
}
else
{
px = (*sptr) -> info;
*sptr = (*sptr) -> next;
//free(sptr);
return px;
}
}
void disp(s *sptr)
{
s temp=*sptr;
printf("the elements in stack are:\n");
while(temp->next!=NULL)
{
printf("%d\n",(temp)->info);
(temp)=(temp)->next;
}
printf("%d\n",temp -> info);
}
void main()
{
int c,i;
s s1=NULL;
do{
printf("enter operation:-");
scanf("%d",&c);
switch(c)
{
case 1: printf("enter data:-");
scanf("%d",&i);
push(&s1,i);
break;
case 2:pop(&s1);break;
case 3:disp(&s1); break;
}
}while(s1!=NULL);
}
@douglas-vaz
Copy link
Author

[douglas@dell-studio Foo_codes]$ gcc stach_s.c -o stac
[douglas@dell-studio Foo_codes]$ ./stac
enter operation:-1
enter data:-23
enter operation:-1
enter data:-23
enter operation:-1
enter data:-345
enter operation:-3
the elements in stack are:
345
23
enter operation:-2
*** glibc detected *** ./stac: munmap_chunk(): invalid pointer: 0x00007fff603ed9f0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x78a56)[0x7ffc392cfa56]
./stac[0x400734]
./stac[0x400816]
/lib/libc.so.6(__libc_start_main+0xf5)[0x7ffc39278455]
./stac[0x400589]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:06 1977217 /home/douglas/workspace/Foo_codes/stac
00600000-00601000 rw-p 00000000 08:06 1977217 /home/douglas/workspace/Foo_codes/stac
021a7000-021c8000 rw-p 00000000 00:00 0 [heap]
7ffc39042000-7ffc39057000 r-xp 00000000 08:06 142818 /usr/lib/libgcc_s.so.1
7ffc39057000-7ffc39256000 ---p 00015000 08:06 142818 /usr/lib/libgcc_s.so.1
7ffc39256000-7ffc39257000 rw-p 00014000 08:06 142818 /usr/lib/libgcc_s.so.1
7ffc39257000-7ffc393ee000 r-xp 00000000 08:06 1573015 /lib/libc-2.15.so
7ffc393ee000-7ffc395ee000 ---p 00197000 08:06 1573015 /lib/libc-2.15.so
7ffc395ee000-7ffc395f2000 r--p 00197000 08:06 1573015 /lib/libc-2.15.so
7ffc395f2000-7ffc395f4000 rw-p 0019b000 08:06 1573015 /lib/libc-2.15.so
7ffc395f4000-7ffc395f8000 rw-p 00000000 00:00 0
7ffc395f8000-7ffc39619000 r-xp 00000000 08:06 1573124 /lib/ld-2.15.so
7ffc397e7000-7ffc397ea000 rw-p 00000000 00:00 0
7ffc39815000-7ffc39819000 rw-p 00000000 00:00 0
7ffc39819000-7ffc3981a000 r--p 00021000 08:06 1573124 /lib/ld-2.15.so
7ffc3981a000-7ffc3981b000 rw-p 00022000 08:06 1573124 /lib/ld-2.15.so
7ffc3981b000-7ffc3981c000 rw-p 00000000 00:00 0
7fff603cf000-7fff603f0000 rw-p 00000000 00:00 0 [stack]
7fff603ff000-7fff60400000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted

@douglas-vaz
Copy link
Author

Fixed and working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment