具体要求:两个参数
1. 一个list_t mylist
2.一个新char p ,
比如mylist的样子是b-a-c-d-c-d ,p = ‘d’
要求p转成node加到d*最后一次**出现的之前,变为
b-a-c-d-c-**d**-d
typedef struct node node_t;
struct node {
char data;
node_t *next;
};
typedef struct {
node_t *head;
node_t *foot;
} list_t;
void insert_in_list(list_t *mylist, char p) {
·····
// Q702498.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
typedef struct node node_t;
struct node {
char data;
node_t *next;
};
typedef struct {
node_t *head;
node_t *foot;
} list_t;
void printlist(list_t list)
{
node_t * p = list.head;
while (p != NULL)
{
if (p->next == NULL)
printf("%c\n", p->data);
else
printf("%c->", p->data);
p = p->next;
}
}
node_t * gennode(char data)
{
node_t * p = (node_t *)malloc(sizeof(node_t));
p->data = data;
p->next = NULL;
return p;
}
void insert(list_t list, char p)
{
node_t * p1 = list.head;
node_t * lastfound = NULL;
while (p1 != NULL)
{
if (p1->data == 'd')
lastfound = p1;
p1 = p1->next;
}
node_t * pnew = (node_t *)malloc(sizeof(node_t));
pnew->next = lastfound->next;
pnew->data = p;
lastfound->next = pnew;
}
int _tmain(int argc, _TCHAR* argv[])
{
node_t * node1 = gennode('b');
node_t * node2 = gennode('a');
node1->next = node2;
node_t * node3 = gennode('c');
node2->next = node3;
node_t * node4 = gennode('d');
node3->next = node4;
node_t * node5 = gennode('c');
node4->next = node5;
node_t * node6 = gennode('d');
node5->next = node6;
list_t mylist;
mylist.head = node1;
mylist.foot = node6;
printlist(mylist);
insert(mylist, 'd');
printlist(mylist);
return 0;
}
运行结果:
b->a->c->d->c->d
b->a->c->d->c->d->d
Press any key to continue . . .
如果问题得到解决,麻烦点下我回答右边的采纳
list_t
*reverselist(node_t *node) {
list_t *list = make_empty_list();
if(node == NULL || node->next == NULL) {
list->head = node;
list->foot = NULL;
return list;
}
node_t *p=node, *newhead = NULL;
while(p != NULL) {
node_t *tmp = p->next;
p->next = newhead;
newhead = p;
p = tmp;
}
list->head = newhead;
list->foot = NULL;
return list;
}