Code
#include#includetypedefstructnode
{intvalue;structnode*next;
}node,*node_list;voidaddNode(node_list&l)
{
node_list temp2=l;for(inti=1; i<10; i++)
{
node_list temp=(node_list)malloc(sizeof(structnode));
temp->next=NULL;
temp->value=i;
l->next=temp;
l=l->next;
}
l=temp2;
}voidmain()
{
node_list l;
node_list p;
l=(node_list)malloc(sizeof(structnode));
l->next=NULL;
l->value=0;
p=l;
addNode(p);
p=l;while(p!=NULL)
{
printf("%d \n",p->value);
p=p->next;
}
}
Code
#include#includetypedefstructnode
{intvalue;structnode*next;
}node,*node_list;
node_list addNode(node_list&l,inti)
{
node_list temp=(node_list)malloc(sizeof(structnode));
temp->next=NULL;
temp->value=i;
l->next=temp;returntemp;
}voidmain()
{
node_list l;
node_list p, temp;
l=(node_list)malloc(sizeof(structnode));
l->next=NULL;
l->value=0;
temp=l;for(inti=1;i<10; i++)
{
l=addNode(l, i);
}
p=temp;while(p!=NULL)
{
printf("%d \n",p->value);
p=p->next;
}
}
#include
using namespace std;
typedef struct test
{
int id;
test *next;
};
test * newnode;
/*删除后返回当前结点*/
test* delete_node(test *headnode, test *deletenode)
{
if (headnode->next == NULL)
return NULL;
test * f_head = headnode;
test * f_head_next = f_head->next;
while (f_head_next != NULL)
{
if (f_head_next == deletenode)
{
if (f_head_next->next == NULL)
{
f_head->next = NULL;
cout<id<
}
else
{
f_head->next = f_head_next->next;
return f_head_next->next;
}
break;
}
f_head = f_head->next;
f_head_next = f_head->next;
}
}
// 增加新结点
test * addnode(test * node, int idnum)
{
newnode = new test;
newnode->id = idnum;
newnode->next = NULL;
node->next = newnode;
return newnode;
}
//排序
void sort(test *head)
{
if (head->next == NULL)
return;
int temp;
test * headnode = head;
test * second = headnode->next;
while (headnode != NULL)
{
second = headnode->next;
while (second != NULL)
{
if ((headnode->id )> (second->id))
{
temp = headnode->id;
headnode->id = second->id;
second->id = temp;
}
second = second->next;
}
headnode = headnode->next;
}
}
int main(void)
{
cout<
test head;
int nodenum = 0 ;
test *temp = &head;
test *currentNode = &head; //此处变量相当于中间变量,为链表当前结点位置
test *before_node = head.next;
bool ishead = false;
cout<
while (nodenum ++ != 30)
{
if (!ishead)
{
head.id =nodenum;
ishead = true;
continue;
}
before_node = currentNode; /*保存指针的前一个位置*/
currentNode = addnode(currentNode, nodenum);
}
sort(temp);
temp = &head;
int i = 1;
cout<
while (temp!= NULL)
{
cout<id<
temp = temp->next;
i++;
}
/*尾部指针指向头指针*/
currentNode->next = &head;
cout<
//before_node = delete_node(&head, before_node);
//cout<id<
/*开始选大王*/
temp = &head;
i = 0;
int node_num = 30;
while (node_num != 1)
{
i++;
if (7 == i)
{
cout<id<
temp = delete_node(&head, temp);
i = 0;
node_num--;
}
else
temp = temp->next;
}
cout<id<
delete [] newnode; //释放
return 0;
}