zoukankan      html  css  js  c++  java
  • 数据结构实验之链表九:双向链表

                                              数据结构实验之链表九:双向链表

    Time Limit: 1000MS Memory Limit: 65536KB

    Submit Statistic

    Problem Description

    学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内存中找到下一个位置,这是一个不会轻易断裂的链。但单链表有一个弱点——不能回指。比如在链表中有两个节点A,B,他们的关系是B是A的后继,A指向了B,便能轻易经A找到B,但从B却不能找到A。一个简单的想法便能轻易解决这个问题——建立双向链表。在双向链表中,A有一个指针指向了节点B,同时,B又有一个指向A的指针。这样不仅能从链表头节点的位置遍历整个链表所有节点,也能从链表尾节点开始遍历所有节点。对于给定的一列数据,按照给定的顺序建立双向链表,按照关键字找到相应节点,输出此节点的前驱节点关键字及后继节点关键字。

    Input

    第一行两个正整数n(代表节点个数),m(代表要找的关键字的个数)。第二行是n个数(n个数没有重复),利用这n个数建立双向链表。接下来有m个关键字,每个占一行。

    Output

    对给定的每个关键字,输出此关键字前驱节点关键字和后继节点关键字。如果给定的关键字没有前驱或者后继,则不输出。
    注意:每个给定关键字的输出占一行。
               一行输出的数据之间有一个空格,行首、行末无空格。

    Example Input

    10 31 2 3 4 5 6 7 8 9 0350

    Example Output

    2 44 69
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #define LISTSIZE 100000
    #define LISTINCREAMENT 1000
    typedef int ElemType;
    int n;
    typedef struct DNode  //定义双向链表结点类型
    {
    	ElemType data;  //数据域
    	struct DNode *prior,*next;  //前驱和后继指针
    }DNode,*DLinklist;
     DNode *insertDlinklist(DNode *L,int n)
    {
    	int i;
        DNode *tail,*p;
    	L=(DNode *)malloc(sizeof(DNode));
    	L->next = NULL;
    	L->prior = NULL;
    	tail= L;
    	for(i=0;i<n;i++)
    	{
             p = ( DNode *)malloc( sizeof(DNode));
    		 scanf("%d",&p->data);
    		 p->next = NULL;
    		 p->prior = tail;
    		 tail->next = p;
    		 tail = p;
    	}
    	return L;
    }
    void find(DNode *L,int m)
    {
    	DNode *p=L->next;
    	while(m--) //m次查找
    	{
    		int key,count=0; //count记录位置,计数
    		scanf("%d",&key); //数要查找的key
    		p=L->next;//从头开始遍历
    		while(p)
    		{
    			count++;
    			if(key==p->data)
    			{
    				if(count==1)
    				{
    					printf("%d\n",p->next->data);
    				}
    				else if(count==n)
    				{
    					printf("%d\n",p->prior->data);
    				}
    				else
    				{
    					printf("%d %d\n",p->prior->data,p->next->data);
    				}
    			}
    			p=p->next;
    		}
    	}
    }
    int main()
    {
    	DNode *L;
    	int m;
    	scanf("%d%d",&n,&m);
    	L = insertDlinklist(L,n);
    	find(L,m);
    	return 0;
    }
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -1
    typedef int ElemType;
    typedef int Status;
    typedef struct LNode{
    	int data;
    	struct LNode *font,*rear;
    }*LinkList,LNode;
    void createLinkList(LinkList &head,int n){
    
    	LNode *p,*tail;
    	head = (LinkList)malloc(sizeof(LNode));
    	head->rear = NULL;
        tail=head;
    	for(int i=0;i<n;i++){
    		p = (LinkList)malloc(sizeof(LNode));
    		p->rear = NULL;
    		scanf("%d",&p->data);
    		tail->rear = p; //新节点挂在链表尾部
    		p->font = tail;
    		tail = p;       //指针变量tail总是指向链表尾部
    	}
    }
    void display(LinkList head){
    	LinkList p = head->rear;
    	while(p){
    		printf("%d %d %d\n",p->font->data,p->data,p->rear->data);
    		p = p->rear;
    	}
    	printf("\n");
    }
    void search(LinkList head,ElemType key){
    	LinkList p = head->rear;
    	while(p){
    		if (p->data==key)
    		{
    				break;
    		}
    		p=p->rear;
    	}
    	if (p->font!=head && p->rear) //如果p的前驱不是head,后驱也不为空
    	{
    		printf("%d %d\n",p->font->data,p->rear->data);
    	}
    	else if (p->font!=head) //如果p的前驱不是head,后驱为空
    	{
    		printf("%d\n",p->font->data);
    	}else{ //后驱不为空
    		printf("%d\n",p->rear->data);
    	}
    				
    
    }
    int main(){
    	int n,m;
    	LinkList head =NULL;
    	scanf("%d%d",&n,&m);
    	createLinkList(head,n);
    	for (int i=0;i<m;i++)
    	{
    		int zzz;
    		scanf("%d",&zzz);
    	search(head,zzz);
    	}
    	return 0;
    }
  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11782140.html
Copyright © 2011-2022 走看看