Skip to content

Instantly share code, notes, and snippets.

@gunaytemur
Last active July 6, 2018 22:02
Show Gist options
  • Save gunaytemur/232d7c95f8e937fb74684cb24323806f to your computer and use it in GitHub Desktop.
Save gunaytemur/232d7c95f8e937fb74684cb24323806f to your computer and use it in GitHub Desktop.
Data Structure C++
stack list implementation
@gunaytemur
Copy link
Author

gunaytemur commented Jul 6, 2018

#include"iostream"
using namespace std;
int dizi[5];
int top = -1;

struct node {
int data;
node *next;
node *prev;
};

node *push(int x, node *r) {

node *yrd;

if (r == NULL)
{
	r = new node;
	r->data = x;
	r->prev = NULL;
	r->next = NULL; 
	return r;
}
else
{
	yrd = new node;
	yrd->data = x;
	yrd->prev = NULL;
	yrd->next = r;
	r->prev = yrd;
	r = yrd;
	return r;
}

}

node *pop(node *r) {
int data;

if (r->next == NULL)
{
	node *yrd;
	yrd = r;
	r = NULL;
	//		data=yrd->data;   sistemi kurdum kullanirken datayi kullan
	delete[] yrd;
}
else if (r == NULL)
	cout << "stack bos1";
else
{
	node *yrd;
	yrd = r;
	r = yrd->next;
	r->prev = NULL;
	yrd->next = NULL;
	//		data=yrd->data;   sistemi kurdum kullanirken datayi kullan
	delete[] yrd;
}
return r;

}

void yazdir(node *r) {

node *yrd;
yrd = r;
if (r == NULL)
	cout << "stack bos";
else
{
	cout << yrd->data << " ";
	while (yrd->next != NULL)
	{
		yrd = yrd->next;
		cout << yrd->data << " ";
	}
	cout << endl;
}

}
void main() {

node *root;
root = NULL;

root = push(3, root);
root = push(7, root);
root = push(8, root);
yazdir(root);
root = push(13, root);
root = push(17, root);
root = push(18, root);
yazdir(root);
root = pop(root);
root = pop(root);
root = pop(root);
yazdir(root);
root = push(23, root);
root = push(37, root);
root = push(48, root);
yazdir(root);
root = pop(root);
root = pop(root);
root = pop(root);
root = pop(root);
root = pop(root);
root = pop(root);

yazdir(root);
cout << endl;
system("pause");

}

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