求帮助|c语言链表问题,请相助

C语言 码拜 7年前 (2017-05-01) 1312次浏览
问题是:写一个程序模拟浏览器的前进后退功能,按v访问网址,输入b回到上一网址,再输入f可以前进
本人的代码老是不能后退,感觉链表构建的没有问题(应该是感觉错了求帮助|c语言链表问题,请相助求帮助|c语言链表问题,请相助
请高手们帮本人看看。本人使用的VS2015

#include<stdio.h>
#include<malloc.h>
struct Node {
	char site[500];
	struct Node*next;
	struct Node*back;
};
int main() {
	struct Node *head = NULL;
	struct Node *p = NULL, *k = NULL, *q = NULL;
	char c;
	int flag = 1;
	q = p = k = head = (struct Node *)malloc(sizeof(struct Node));
	head->next = p->next = k->next = (struct Node*)malloc(sizeof(struct Node));
	p->back = k->back = (struct Node*)malloc(sizeof(struct Node));
	printf("请输入浏览器命令:V(访问新网站),F(前进),B(后退),X(退出)\n");
	scanf_s("%c", &c, 1);
	while (c != "X"&&c != "x") {
		if (c == "V" || c == "v") {
			p->next = (struct Node*)malloc(sizeof(struct Node));
			p->back  = (struct Node*)malloc(sizeof(struct Node));
			printf("请输入新的网址:");
			scanf_s("%s", p->site, 500);
			if (flag == 1) {
				k = head = p;
				p->next = NULL;
				p->back = NULL;
			}
			else {

				p->back = k;
				k->next = p;
				p->next = NULL;
				k = p;
			}
			printf("当前网址是:%s\n", p->site);
			flag = 0;
		}
		else if (c == "F" || c == "f") {
			if (k->next != NULL) {
				k = k->next;
				printf("当前网址是:%s\n", k->site);
			}
			else printf("无法前进!\n");
		}
		else if (c == "B" || c == "b") {
			if (k != head) {
				k = k->back;
				printf("当前网址是:%s\n", k->site);
			}
			else printf("不能后退!\n");
		}
		else
			printf("请输入正确指令!\n");
		getchar();
		scanf_s("%c", &c, 1);
	}
}
解决方案

20

你每次输入新网址的时候,head都被更新到最后输入的那个网址
在输入 b 的时候,if (k != head) 不成立,故一输出不能后退

10

访问新网站,后退还有前进有没有基于哪个网站(节点),这个不明确怎么来确定前进,后退,还有新网站的正确性。
你的需求还不够晚上或有歧义。

20

引用:
Quote: 引用:

你每次输入新网址的时候,head都被更新到最后输入的那个网址
在输入 b 的时候,if (k != head) 不成立,故一输出不能后退

这个本人在输入第二次是flag=0,不是把head跳过去了吗?能具体说清楚一点儿吗?本人发现调试的时候也是head每次都会变,哪些代码需要改呢?

建议你在关键位置设置断点,然后单步跟踪程序运行,观察变形的变化情况,很容易找出原因

60

想复杂了吧,既然是链表,head一般不会变,建立起光标的概念就好了,每次分配内存应该只是一个节点,而不是你现在这样两三个的分配

#include<stdio.h>
#include<malloc.h>
struct Node
{
	char site[500];
	struct Node*next;
	struct Node*back;
};
void free_nodes(struct Node *p)
{
	struct Node *q;
	while (p!=NULL)
	{
		q=p->next;
		free(p);
		p=q;
	}
}
int main()
{
	struct Node *head=NULL;
	struct Node *cursor=head;
	char c;
	bool exit=false;
	printf("请输入浏览器命令:V(访问新网站),F(前进),B(后退),X(退出)\n");
	while (!exit)
	{
		scanf_s("%c",&c,1);
		switch (c)
		{
		case "X":
		case "x":
			exit=true;
			break;
		case "V":
		case "v":
			if (head==NULL)
			{
				head=(struct Node*)malloc(sizeof(struct Node));
				cursor=head;
				cursor->next=NULL;
				cursor->back=NULL;
			}
			else
			{
				if (cursor->next==NULL)
				{
					cursor->next=(struct Node*)malloc(sizeof(struct Node));
					cursor->next->next=NULL;
					cursor->next->back=cursor;
					cursor=cursor->next;
				}
				else
				{
					cursor=cursor->next;
					free_nodes(cursor->next);
					cursor->next=NULL;
				}
			}
			printf("请输入新的网址:");
			scanf_s("%s",cursor->site,500);
			printf("当前网址是:%s\n",cursor->site);
			break;
		case "F":
		case "f":
			if (cursor!=NULL&&cursor->next!=NULL)
			{
				cursor=cursor->next;
				printf("当前网址是:%s\n",cursor->site);
			}
			else
				printf("无法前进!\n");
			break;
		case "B":
		case "b":
			if (cursor!=NULL&&cursor->back!=NULL)
			{
				cursor=cursor->back;
				printf("当前网址是:%s\n",cursor->site);
			}
			else
				printf("无法后退!\n");
			break;
		default:
			printf("请输入正确指令!\n");
			break;
		}
		getchar();
	}
	free_nodes(head);
}

10

仅供参考:

//不带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <locale.h>
struct NODE {
    int          data;
    struct NODE *next;
} *head,*p,*q,*s,*p1,*p2,*q1,**ta;
int i,k,n,t,m,v,N=10;
int main() {
    setlocale(LC_ALL,"chs");
    srand(time(NULL));
    head=NULL;
    printf("创建%d个节点的单链表:",N);//创建N个节点的单链表
    p=head;
    for (i=0;i<N;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) exit(1);
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        if (NULL==p) {
            head=q;
            p=head;
        } else {
            p->next=q;
            p=q;
        }
    }
    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }
    k=3;
    v=5;
    printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==1) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) exit(1);
            q->data=v;
            q->next=head;
            head=q;
            break;
        } else {
            if (k-1==n) {
                q=(struct NODE *)malloc(sizeof(struct NODE));
                if (NULL==q) exit(1);
                q->data=v;
                q->next=p->next;
                p->next=q;
                break;
            }
        }
        p=p->next;
    }
    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }
    k=5;
    printf("删除第%d个节点:",k);//删除第k个节点
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==1) {
            q=head;
            head=head->next;
            free(q);
            break;
        } else {
            if (k-1==n) {
                q=p->next;
                if (q) {
                    p->next=q->next;
                    free(q);
                }
                break;
            }
        }
        p=p->next;
    }
    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }
    printf("从小到大排序:");//从小到大排序
    for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {
        for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {
            if (p->data > q->data) {
                //交换data
//              printf("swap %02d %02d\n",p->data,q->data);
//              t=p->data;p->data=q->data;q->data=t;
                //或
                //交换next
//              printf("swap %02d %02d\n",p->data,q->data);
                if (p==head) {//p是头
                    if (p->next==q) {//pq挨着
                        head=q;
                        p->next=q->next;
                        q->next=p;
                        q=p;
                        p=head;
                    } else {//pq不挨着
                        head=q;
                        p2=p->next;
                        p->next=q->next;
                        q->next=p2;
                        q1->next=p;
                        q=p;
                        p=head;
                    }
                } else {//p不是头
                    if (p->next==q) {//pq挨着
                        p1->next=q;
                        p->next=q->next;
                        q->next=p;
                        q=p;
                        p=p1->next;
                    } else {//pq不挨着
                        p1->next=q;
                        p2=p->next;
                        p->next=q->next;
                        q->next=p2;
                        q1->next=p;
                        q=p;
                        p=p1->next;
                    }
                }
                //输出整个单链表
//              s=head;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }
    //输出整个单链表并计算链表长度n
    n=0;
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        n++;
        s=s->next;
    }
    printf("将整个链表逆序:");//将整个链表逆序
    if (n>=2) {
        p=head;
        q=p->next;
        p->next=NULL;
        while (1) {
            q1=q->next;
            q->next=p;
            p=q;
            q=q1;
            if (NULL==q) break;
        }
        head=p;
    }
    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }
    m=4;
    n=6;
    printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长
    k=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        k++;
        if (m==k) {
            q=p;
        }
        s=p;
        p=p->next;
    }
    q1=head;
    head=q->next;
    s->next=q1;
    q->next=NULL;
    //输出整个单链表
    s=head;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }
    //释放全部节点
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }
    return 0;
}
//创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16->
//将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16->
//删除第5个节点:08->74->05->07->03->99->31->56->88->16->
//从小到大排序:03->05->07->08->16->31->56->74->88->99->
//将整个链表逆序:99->88->74->56->31->16->08->07->05->03->
//将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56->
//

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求帮助|c语言链表问题,请相助
喜欢 (0)
[1034331897@qq.com]
分享 (0)