我的英语不是很好,但希望你们能理解我想说的话。所以这是链表的代码,在运行程序并添加信息后,它可以在printListStart()处打印。现在我在printListEnd()处编写代码时遇到了问题,我想从末尾显示代码(反向)。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
//function prototype
void addToStart(); //add node to beginning of linked list
void addToEnd(); //add node to end of linked list
void removeNodeAt(); //remove node that matches element entered
void printListStart(); //print nodes from start
void printListEnd(); //print node from end
void startlist(); //create NULL list
void menu(); //selection
//global variables
int option, number;
char name[20], gender[10],address[50],description[50];
//declare structure for node
struct node
{
char customer_name[20];
int customer_number;
char gender_[10];
char customer_address[50];
char order_description[50];
struct node *next;
}*newnode, *list, *prev, *temp, *tmpdisplay;
void main()
{
startlist(); //function call to create empty list
do
{
menu(); //function call to show menu
switch (option)
{
case 1: system("cls"); addToStart(); break;
case 2: system("cls"); addToEnd(); break;
case 3: system("cls"); removeNodeAt(); break;
case 4: system("cls"); printListStart(); break;
case 5: system("cls"); printListEnd(); break;
case 6: exit(0);
default:
printf("Invalid Option");
getch();
}
} while (option != 6);
}//end main
void startlist()
{
list = NULL; //create empty list
}
void menu()
{
printf("***LINKED LIST***nn");
printf(" 1. Add New Node At Startn");
printf(" 2. Add New Node At Endn");
printf(" 3. Remove Noden");
printf(" 4. Print Linked List From Startn");
printf(" 5. Print Linked List From Endn");
printf(" 6. Quitn");
printf("nSelect a task: "); //allow user to select choice
scanf("%d", &option);
}
void addToStart()
{
newnode = (struct node*) malloc(sizeof(struct node)); //allocates memory space for new node
printf("Enter the customer name:n");
scanf("%s", &name);
printf("Enter then customer number:n");
scanf("%d", &number);
printf("Enter the Oder Description:n");
scanf("%s", &description);
printf("Enter the Gender:n");
scanf("%s", &gender);
printf("Enter the Customer Address:n");
scanf("%s", &address);
newnode->customer_number = number;
strcpy(newnode->customer_name, name); //using stringcopy to copy name to customer_name in node
strcpy(newnode->order_description, description); //using stringcopy to copy transdes to transaction_description in node
strcpy(newnode->gender_, gender);
strcpy(newnode->customer_address, address);
newnode->next = NULL; //set node pointer to NULL
if (list == NULL)
list = newnode; //if list is empty, node is assigned to list
else
{
newnode->next = list; //if list not empty, newnode pointer equals to list first node
list = newnode; //assign newnode to list, newnode is at the start of the list
}
}
void addToEnd()
{
newnode = (struct node*) malloc(sizeof(struct node)); //allocate new memory space for new node
printf("Enter the customer name:n");
scanf("%s", &name);
printf("Enter then customer number:n");
scanf("%d", &number);
printf("Enter the Oder Description:n");
scanf("%s", &description);
printf("Enter the Gender:n");
scanf("%s", &gender);
printf("Enter the Customer Address:n");
scanf("%s", &address);
newnode->customer_number = number;
strcpy(newnode->customer_name, name);
strcpy(newnode->order_description, description);
strcpy(newnode->gender_, gender);
strcpy(newnode->customer_address, address);
newnode->next = NULL;
if (list == NULL)
list = newnode; //if list is empty, assign newnode to list as first node
else
{
temp = list; //list not empty, assign temp as list
while (temp->next != NULL) //while pointer does not point to NULL/empty
{
temp = temp->next; //move to subsequent node
}
temp->next = newnode; //loop exits when last node is reached, last node's pointer points to newnode
}
}
void removeNodeAt()
{
printf("Enter customer number to delete: n");
scanf("%d", &number);
if (list == NULL) //check if list is empty
printf("nnLIST IS EMPTYnn");
//if list not empty, match number to cust_no in first node
else if (number == list->customer_number)
{
list = list->next; //match found, first node is skipped (deleted)
}
else //match not found in first node, move to subsequent nodes
{
temp = list; //assign temp as list
while (temp->customer_number != number)
{
//if match not found
prev = temp; //prev is pointing to linked list
temp = temp->next;//temp is pointing to next node
}
printf("Node deleted:");
printf("n%sn", prev->customer_name);
printf("%dn", prev->customer_number);
printf("%snn", prev->gender_);
prev->next = prev->next->next; //match found, skip/jump the node (delete)
}
}
void printListStart()
{
if (list == NULL)
printf("nnLIST IS EMPTYnn");
else
{
tmpdisplay = list;
while (tmpdisplay != NULL)
{
printf("n%sn", tmpdisplay->customer_name);
printf("%dn", tmpdisplay->customer_number);
printf("%sn", tmpdisplay->gender_);
printf("%sn", tmpdisplay->order_description);
printf("%sn", tmpdisplay->customer_address);
tmpdisplay = tmpdisplay->next;
}
}
}
void printListEnd()
{
}
最简单的方法是创建一个双链表,其中每个节点都有一个下一个和上一个指针,下一个指向下一个节点(就像您现在所做的那样),上一个指向上一个节点。您还需要指向列表前面和末尾的指针。若要反向打印,请从指向最后一个节点的指针开始,并跟随前一个指针而不是下一个指针。
struct node
{
char customer_name[20];
int customer_number;
char gender_[10];
char customer_address[50];
char order_description[50];
struct node *next;
struct node *prev;
}*newnode, *list, *prev, *temp, *tmpdisplay, *listend;
将节点添加到末尾时,prev将新节点设置为listend,并将listend设置为新节点。
如果由于某种原因无法使用双链表,则可以选择使用嵌套循环或递归。这两种解决方案都很可怕。这是嵌套循环解决方案,它很可怕,因为它在O(N^2)时间内运行:
void printListEnd()
{
struct node *end;
if (list == NULL)
printf("nnLIST IS EMPTYnn");
else
{
end = NULL;
while (end != list)
{
tmpdisplay = list;
while (tmpdisplay->next != end)
tmpdisplay = tmpdisplay->next;
printf("n%sn", tmpdisplay->customer_name);
printf("%dn", tmpdisplay->customer_number);
printf("%sn", tmpdisplay->gender_);
printf("%sn", tmpdisplay->order_description);
printf("%sn", tmpdisplay->customer_address);
end = tmpdisplay;
}
}
}
这是递归解决方案,它很可怕,因为它浪费了与列表长度成比例的堆栈空间:
static void printListEndRecurse(struct node *list)
{
if (list->next)
printListEndRecurse(list->next);
tmpdisplay = list;
printf("n%sn", tmpdisplay->customer_name);
printf("%dn", tmpdisplay->customer_number);
printf("%sn", tmpdisplay->gender_);
printf("%sn", tmpdisplay->order_description);
printf("%sn", tmpdisplay->customer_address);
}
void printListEnd()
{
if (list == NULL)
printf("nnLIST IS EMPTYnn");
else
printListEndRecurse(list);
}
对不起,我马上想到了递归。为什么不给这个神奇的技术一个机会呢?让我试试:
void printListEnd(struct node *myList)
{
if (myList != NULL) {
printListEnd(myList->next);
printf("n%sn", myList->customer_name);
printf("%dn", myList->customer_number);
printf("%sn", myList->gender_);
printf("%sn", myList->order_description);
printf("%sn", myList->customer_address);
}
}
要调用函数,需要传递变量列表(列表的根节点)。
接受应答后
分配一个数组来存储链接地址。然后按相反的顺序打印数组元素
O(n)时间。
// Find size
size_t n = 0;
struct node *p = myList;
while (p) {
p = p->next;
n++;
}
// Build array
struct node **list = malloc(sizeof *list * n);
if (list) {
struct node **walker = list;
p = myList;
while (p) {
*walker = p;
walker++;
p = p->next;
}
// Print list
while (walker != list) {
walker--;
print_list(*walker);
}
free(list);
list = NULL;
}
这种方法不使用函数递归。