[ create a new paste ] login | about

Link: http://codepad.org/BvKHcx4w    [ raw code | output | fork ]

C++, pasted on Nov 5:
// Ch15_7.cpp : 定義主控台應用程式的進入點。
//

#include <iostream>
#include <cstring>
using namespace std;
struct Element
{
	int Value;
	Element* Next;
};

void ShowElement(Element* pshow);
void Insert(Element* pE);
void Delete(Element* pE);
int main()
{
	int size;
	cout << "Element的數目:5" << endl;
        size=5;
	Element* pE=new Element[size];
	for(int i=0;i<(size-1);i++)
	{
		pE[i].Next=pE+i+1;
	}
	pE[size-1].Next=NULL;
	for (int i=0;i<size;i++)
	{
		pE[i].Value=i*2;
	}
	cout << "Element的內容是:" << endl;
	ShowElement(pE);
	Insert(pE);
	ShowElement(pE);
	Delete(pE);
	ShowElement(pE);
	delete [] pE;
	system("PAUSE");
	return 0;
}
void Insert(Element* pE)
{
	int node;
	int value;
	cout << "插入第三個節點後" << endl;
	node=3;
	cout << "插入的值為10" << endl;
	value=10;
	Element* new_node=new Element;
	new_node->Value=value;
	new_node->Next=NULL;
	new_node->Next=pE[node-1].Next;
	pE[node-1].Next=new_node;
}
void Delete(Element* pE)
{
	int node;
	cout << "刪除第三個節點" << endl;
	cin >> node;
	Element* temp;
	temp = pE[node-2].Next;
	cout << pE[node-2].Next << endl;
	pE[node-2].Next=pE[node-1].Next;
	cout << pE[node-2].Next << endl;
	delete temp;
}
void ShowElement(Element* pShow)
{
	while(pShow != NULL)
	{
		cout << pShow->Value<<' ';
		pShow=pShow->Next;
	}
	cout << endl;
}


Output:
1
2
3
4
5
6
7
8
9
Element的數目:5
Element的內容是:
0 2 4 6 8 
插入第三個節點後
插入的值為10
0 2 4 10 6 8 
刪除第三個節點

Segmentation fault


Create a new paste based on this one


Comments: