| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 637 人关注过本帖
标题:链表的基本操作(标题待定)
只看楼主 加入收藏
liyanhong
Rank: 3Rank: 3
来 自:水星
等 级:禁止访问
威 望:8
帖 子:1867
专家分:0
注 册:2008-5-3
收藏
 问题点数:0 回复次数:6 
链表的基本操作(标题待定)

struct node
{
   int data;
   struct node *next;
};
typedef struct node SLIST;
SLIST *creat_slist()
{
   int c;
   SLIST *head,*s,*r;
   head=(SLIST *)malloc(sizeof(SLIST));
   r=head;
   scanf("%d",&c);
   while(c!=-1)
   {
      s=(SLIST *)malloc(sizeof(SLIST));
      s->data=c;
      r->next=s;
      r=s;
      scanf("%d",&c);
  }
  r->next='\0';
  return head;
}


void print_slist(SLIST *head)
{
   SLIST *p;
   p=head->next;
   if(p=='\0')
     printf("Linklist is null!\n");
   else
      {
         printf("head");
         do
         {
            printf("->%d",p->data);
            p=p->next;
         }while(p!='\0');
         printf("End\n");
      }
}


void insert_snode(SLIST *head,int x,int y)
{
   SLIST *s,*p,*q;
   s=(SLIST *)malloc(sizeof(SLIST));
   s->data=y;
   q=head;
   p=head->next;
   while((p!='\0')&&p->data!=x)
   {
      q=p;
      p=p->next;
   }
   s->next=p;
   q->next=s;
}

delete_snode(SLIST *head,int m)
{
   SLIST *p,*q;
   q=head;p=head->next;
   while(p!='\0'&&p->data!=m)
   {
      q=p;p=p->next;
   }
    if(p->data==m)
    q->next=p->next;
}

main()
{
   SLIST *head;
   int x,y,m;
   clrscr();
   scanf("%d%d",&x,&y);
   scanf("%d",&m);
   head=creat_slist();
   print_slist(head);  /*加入一個訪問函數*/
   insert_snode(head,x,y);/*加入一個插入函數*/
   print_slist(head);
   delete_snode(head,m);/*加入一個刪除函數*/
   print_slist(head);
}



#include <stdio.h>
struct student
{
      long num; float score; struct student *next;
};int n;
struct student *creat()
  {
      struct student *head;      struct student *p1,*p2;n=0;
      p1=p2=( struct student*) malloc(sizeof(struct student));
      scanf("%ld%f",&p1->num,&p1->score);
      head=NULL;
      while(p1->num!=0)
      {
           n=n+1;
           if(n==1)
             head=p1;
          else
             p2->next=p1;/*重點在這里*/
          p2=p1;
      p1=(struct student*)malloc(sizeof(struct student));
      scanf("%ld%f",&p1->num,&p1->score);
      }
       p2->next=NULL;/*重點在這里*/
       return(head);
  }

void print(struct student *head)
{
   struct student *p;
   printf("\nNow,These %d records are:\n",n);  p=head;/*重點在這里*/
   if(head!=NULL)
  do
  {
     printf("%ld %5.1f\n",p->num,p->score);
     p=p->next;
  }while(p!=NULL);
}

struct student *del(struct student *head,long num)
 {
    struct student *p1,*p2;
    if (head==NULL)
   {
      printf("\nlist null!\n");goto END;
   }
   p1=head;
   while(num!=p1->num && p1->next!=NULL)
   {
      p2=p1;p1=p1->next;
   }
   if(num==p1->num)
   {
     if(p1==head)
        head=p1->next;
     else
        p2->next=p1->next;
   printf("delete:%ld\n",num);
   n=n-1;
   }
   else
     printf("%ld not been found!\n",num);
   END:
   return(head);
}


struct student *inser(struct student *head, struct student *stud)
{
    struct student *p0,*p1,*p2;
    p1=head;p0=stud;    
    if(head==NULL)
    {
       head=p0; p0->next=NULL;
    }
    else
    {
       while((p0->num>p1->num) && (p1->next!=NULL))
       {
          p2=p1; p1=p1->next;
       }
       if(p0->num<=p1->num)   
       {
          if(head==p1)
             head=p0;
          else
             p2->next=p0;
             p0->next=p1;
       }
      else  
      {
          p1->next=p0;
          p0->next=NULL;
      }
   }
   n=n+1;       
   return(head);
}

main()
{
   struct student *head,stud;
   long num;
   clrscr();
   scanf("%ld%f",&stud.num,&stud.score);
   scanf("%ld",&num);
   head=creat();
   print(head);/*加入一個訪問函數*/
   del(head,num);/*加入一個刪除函數*/
   print(head);
   inser(head,&stud);/*加入一個插入函數*/
   print(head);

}
搜索更多相关主题的帖子: 链表 待定 
2008-08-30 09:26
liyanhong
Rank: 3Rank: 3
来 自:水星
等 级:禁止访问
威 望:8
帖 子:1867
专家分:0
注 册:2008-5-3
收藏
得分:0 
跟你们这些不上路子的人完全没有共同语言

我决定转行去搞单片机了

88  BCCN

爱上你 是 我的错  可是离 开  又舍不得  听着你为我写的歌     好难过
如果说 我说如果  我们还 能  重新来过   不去计 较 谁对谁错  会怎么做
2008-08-30 11:10
wNumberOney
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-7-12
收藏
得分:0 
不太懂lz的意思。。。。
2008-08-30 12:21
yt414204458
Rank: 2
等 级:论坛游民
帖 子:260
专家分:55
注 册:2008-3-1
收藏
得分:0 
那个白痴说我的题目不上档次,结果自己也不会,哎,真是无语了,像他这样的人还能干什么

一切从爱C开始
2008-08-30 14:20
网易
Rank: 1
来 自:金星
等 级:禁止访问
帖 子:193
专家分:0
注 册:2008-6-10
收藏
得分:0 
你说的哪道题他不会?
你先说说你能干什么事?

答案是:雨中飞燕!
2008-08-30 15:25
网易
Rank: 1
来 自:金星
等 级:禁止访问
帖 子:193
专家分:0
注 册:2008-6-10
收藏
得分:0 
你还不配无语

答案是:雨中飞燕!
2008-08-30 15:26
asd6791868
Rank: 1
来 自:逆流
等 级:新手上路
帖 子:362
专家分:7
注 册:2008-7-27
收藏
得分:0 
单片机???
我 晕·!c51???

─條路 :  ┈片天  ┈個人  ─瞬間:
2008-08-30 15:41
快速回复:链表的基本操作(标题待定)
数据加载中...
 
   



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

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