| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:帮忙 查看下这个链表问题
取消只看楼主 加入收藏
语言
Rank: 2
等 级:论坛游民
帖 子:17
专家分:19
注 册:2009-10-24
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:1 
帮忙 查看下这个链表问题
利用线性表的链式存储结构实现线性表的创建、插入、删除、查找等算法
   编译时:Cannot open include file: 'graphics.h': No such file or directory
           这个 'graphics.h'文件是自带的还是要自己写的啊 ——怎么解决?
                  如果是自己写的  那位大哥能不能帮忙写下 我有急用  同时想学学,谢谢了!!!

/*单链表*/
#include<dos.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define P printf
typedef struct lnode
{
 int data;
 struct lnode *next;
}linklist;
linklist *l;
int  n;
window_3d(int x1,int y1,int x2,int y2,int bk_color)
{
 textbackground (BLACK);
 window(x1,y1,x2,y2);
 clrscr();
 textbackground(bk_color);
 window(x1,y1,x2,y2);
 clrscr();
}
create_l()  /*头输入法建立单链表*/
{
 linklist *s;
 int i,m;
 printf("\n\n\n 1.创建链表:\n:");
 printf("请输入结点个数:");
 scanf("%d",&m);
 l=(linklist *)malloc(sizeof(linklist));
 l->next=NULL;
 printf("\n",m);
 printf("输入结点值:\n");
 for(i=m;i>0;--i)
  {
    s=(linklist*)malloc(sizeof(linklist));
    scanf("%d",&s->data);
    s->next=l->next;
    l->next=s;
   }
}
traverse_l()    /*遍历单链表*/
{
  linklist *q;
  q=l->next;
  printf("\n 结果:");
  n=0;
  while(q)
   {
     printf("->%d",q->data);
     q=q->next;
     n=n+1;
   }
 printf("\nlength of list is:%d",n);
}

delete_l()   /*删除结点*/
{
   int i,x;int e;
   linklist *p,*q;   /*p指向需删除的结点,q指向p的前驱*/
   p=l;
   printf("\n 输入要删除的结点:x=");
   scanf("%d",&x);
   while(p->data!=x && p!=NULL)
     {
      q=p;
      p=p->next;
     }
   if(p!=NULL)
     {
       q->next=p->next;
       free(p);
       n=n-1;
     }
   else
       printf("\nnot find !!!");
}
destroy_l()    /*撤消链表*/
{
  linklist *head;
  while(l!=NULL)
   {
      head=l;
      l=l->next;
      free(head);
   }
   printf("\n撤消链表");
}

find_l()     /*查找元素*/
{
  linklist *p;
  int i,j=1;
  int x;
  p=l->next;   /*p指向第一个结点,j为计数器*/
  printf("\n输入链表中元素的指定位置的链表元素:");
  scanf("%d",&i);
  while(p&&j<i)    /*顺指针向后查找,直到p指向第i个结点或p为空*/
   {
     p=p->next;
     j++;
   }
  if(!p||j>i)
    printf("\n查找错误");
  else
   {
     x=p->data;
     printf("\n%3d%3d",i,x);
   }
}

void insert_l()    /*插入*/
{
  int i,j,x;
  linklist *p,*s;
  printf("\n输入插入结点的值");
  scanf("%d",&x);
  s=(linklist *)malloc(sizeof(linklist));
  s->data=x;
  printf("\n输入插入位置");
  scanf("%d",&i);
  j=1;
  p=l;
  while(p!=NULL&&j<i)  /*i结点的前驱*/
    {
      j++;
      p=p->next;
    }
  if(p!=NULL)
    {
      s->next=p->next;
      p->next=s;
      n=n+1;
    }
  else
      printf("\nnot insert !!!");
}
sort_l()   /*排序*/
{
   linklist *p,*q,*r;   /*r指向最小结点*/
   int min,i,t;
   q=l->next;
   for(i=1;i<n;i++)
     {
       p=q->next;
       min=q->data;
       r=q;
       while(p!=NULL)   /*找最小结点*/
     {
       if(p->data<min)
         {
        r=p;
        min=p->data;
        p=p->next;
          }
        else
         p=p->next;
      }
      t=q->data;
      q->data=r->data;
      r->data=t;
      q=q->next;
     }
}

linklist *look(int elem)    /*查找单链表结点*/
{
   linklist *p;
   p=l;
   while(p->next!=NULL)
     {
       if(p->next->data==elem)
       return p->next;
       p=p->next;
     }
   return NULL;
}

int prior_l()    /*求前驱结点*/
{
  linklist *p,*pos;
  int elenum,prior;
  p=l;
  printf("\n请输入所求前驱的结点");
  scanf("%d",&elenum);
  pos=look(elenum);
  if(pos==NULL)     /*在表里没找到*/
     return NULL;
  if(pos==l->next)  /*定位元素是第一个元素,没有前驱,返回失败*/
     return NULL;
  while(p->next!=pos) /*定位pos的前驱*/
     p=p->next;
  prior=p->data;

  return(prior);
}

int next_l()    /*求后继结点*/
{
  linklist *list,*pos;
  int elenum,next;
  printf("input elem:");
  scanf("%d",&elenum);
  pos=look(elenum);
  if(pos==NULL)
     return NULL;
  if(pos->next==NULL)  /*在表尾的元素没有后继*/
     return NULL;
  next=pos->next->data;
  return next;
}
void back()   /*逆置*/
{
   linklist *p,*q;
   p=l->next;
   l->next=NULL;
   while(p!=NULL)
   {
q=p->next;
p->next=l->next;
l-next=p;
p=q;
    }
}
main()
{
  while(1)
    {
       int choice, num;
       clrscr();
       window_3d(6,6,35,21,RED);
       gotoxy(2,2);
       P("  *>链表功能菜单.<*\n");
       P("\n");
       P("       1.      建立链表\n");
       P("       2.      遍历链表\n");
       P("       3.      删除结点\n");
       P("       4.      插入结点\n");
       P("       5.      撤消链表 \n");
       P("       6.      查询结点 \n");
       P("       7.      链表排序 \n");
       P("       8.      求前驱结点\n");
       P("       9.      求后继结点\n");
       P("      10.      单链表逆置\n");
       P("      11.      output length of list:\n");
       P("       0.      退出程序  \n");
       gotoxy(6,15);
       cputs("  请选择(0-9):");
       scanf("%d", &choice);
       switch(choice)
     {
       case 0: window_3d(3,6,45,21,RED);
           gotoxy(10,6);
           printf("\n");
           gotoxy(10,9);
           printf("\n");
           gotoxy(12,12);
           printf("\n");
           getch();
           exit(0);
       case 1: P("\n");
           window_3d(3,6,76,21,RED);
           create_l();
           traverse_l(l);
           getch();
           break;
       case 2: printf("\n");
           window_3d(3,6,76,21,RED);
           traverse_l(l);
           getch();
           break;
       case 3: printf("\n");
           window_3d(3,6,76,21,RED);
           gotoxy(2,2);
           printf("   ");
           traverse_l(l);
           delete_l(l);
           printf("删除后的结果:\n\n");
           traverse_l(l);
           getch();
           break;
       case 4: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n");
           traverse_l(l);
           insert_l(l);
           printf("插入后的结果:\n\n\n");
           traverse_l(l);
           getch();
           break;
       case 5: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n");
           traverse_l(l);
           destroy_l(l);
           printf("\n");
           getch();
           break;
       case 6: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n");
           traverse_l(l);
           find_l(l);
           getch();
           break;
       case 7: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n");
           traverse_l(l);
           sort_l(l);
           printf("\n");
           traverse_l(l);
           getch();
           break;
       case 8: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n");
           traverse_l(l);
           num=prior_l();
           printf("\n  前驱的元素:");
           printf("%d\n",num);
           getch();
           break;
       case 9: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n");
           traverse_l(l);
           num=next_l(l);
           printf("\n");
           printf("%d\n",num);
           getch();
           break;
      case 10: printf("\n");
           window_3d(3,6,76,21,RED);
           printf("\n单链表顺序为\n");
           back();
           printf("\n\n单链表逆序为:\n");
           traverse_l(l);
           getch();
           break;
      case 11: clrscr();
           printf("\n");
           printf("\noutput length of list:    ");
           printf("%d",n);
           getch();
           break;
     }
   }
}
搜索更多相关主题的帖子: 链表 
2009-11-17 15:44
语言
Rank: 2
等 级:论坛游民
帖 子:17
专家分:19
注 册:2009-10-24
收藏
得分:0 
我们是刚刚学的  VC  
  TC没有学   在VC可以用不
2009-11-17 18:04
快速回复:帮忙 查看下这个链表问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026789 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved