兩個鏈表的交集
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node;
void initpointer(struct Node *p){
p=NULL;
}
int printlist(struct Node* head){
int flag=1;
head=head->next;
/*
因為標記1的地方你用了頭結點,所以第一個數據域無效,應該從下一個頭元結點開始
*/
if(head==NULL)
printf("NULL\n");
else
{
while(head!=NULL)
{
if(flag==1)
{
printf("%d",head->data);
flag=0;
}
else
{
printf(" %d",head->data);
}
head=head->next;
}
printf("\n");
}
return 0;
}
struct Node *creatlist(struct Node *head)
{
int n;
struct Node *p1=(struct Node *)malloc(sizeof(struct Node));
p1->next=NULL;
while(scanf("%d",&n),n!=-1)
{
struct Node *pnode=(struct Node *)malloc(sizeof(struct Node));
pnode->next=NULL;
pnode->data=n;
if(head==NULL)
head=pnode;
p1->next=pnode;
p1=pnode;
}
return head;
}
struct Node *Intersect(struct Node *head1, struct Node *head2)
{
struct Node *p1=head1,*p2=head2;/*我這里沒有用頭指針和頭結點,這里是首元結點head1里面就是第一個數據,一定要理解什么事頭指針,
頭結點,和首元結點
具體你一定要看這個博客:http://blog.sina.com.cn/s/blog_71e7e6fb0101lipz.html*/
struct Node *head,*p,*q;
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
p = head;
while( (p1!=NULL)&&(p2!=NULL) )
{
if (p1->data == p2->data)
{
q = (struct Node *)malloc(sizeof(struct Node));
q->data = p1->data;
q->next = NULL;
p->next = q;//我可以認為你這里用了頭結點,也就是說第一個數據域無效 **標記1**
p = q;
p1 = p1->next;
p2 = p2->next;
}
else if (p1->data < p2->data)
{
p1 = p1->next;
}
else
{
p2 = p2->next;
}
}
return head;
}
int main()
{
struct Node *head=NULL,*headt=NULL,*t;
//initpointer(head);//這里的函數相當于head=NULL;
// initpointer(headt);//上面已經寫了headt=NULL那么這里可以不用調用這個函數
head=creatlist(head);
headt=creatlist(headt);
t=Intersect(head,headt);
printlist(t);
}
標簽:
c語言編程
上傳時間:
2015-04-27
上傳用戶:coco2017co
/****************************************************************
外部晶振8M
PA0~3:四位數碼管的位選
PB0~7:數碼管的8位段選
外部中斷0用于計數
定時器0溢出中斷的定時為1ms
說明 :檢測到水流較小時,繼電器延時1秒關閉
******************************************************************/
#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
char led_7[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //數碼管段選
char position[4]={0xfe,0xfd,0xfb,0xf7};//數碼管位選
uint sumnum=0; //用于記錄1000ms內進入中斷的次數
uint time=0; //記錄進入比較定時器0的次數
uint num=0; //記錄1ms內進入中斷的次數
uint count=0; //進入外部中斷0的次數
uchar flag;
uint sumnum1; //記錄100ms內的數目
/***************************函數聲明***************************/
void delay();
void display(uint m );
void init();
void init_0();
void init_2();
void _delay_us(uint l)
{
unsigned int i;
for(i=0;i<l;i++)
{
asm("nop");
}
}
/**************************主函數***********************************/
void main()
{
init();
init_0();
init_2();
while(sumnum<5)
{
PORTD=0XBF;
segdisplay(sumnum1);
}
while(1)
{
segdisplay(sumnum1);
}
}
/*************************掃描數碼管時的延時函數*********************/
void delay()
{
uchar i,j;
for(i=6;i>0;i--)
for(j=225;j>0;j--);
}
/************************數碼管顯示函數*****************************/
void segdisplay( int temp)
{
int seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
int temp1,temp2,temp3,temp4;
temp1=temp/1000;
temp2=(temp/100)%10;
temp3=(temp/10)%10;
temp4=temp%10;
DDRB=0xff;
DDRA|=0x0f;
PORTA=~BIT(3);
PORTB=seg[temp1];
_delay_us(100);
PORTA=~BIT(2);
PORTB=seg[temp2];
_delay_us(100);
PORTA=~BIT(1);
PORTB=seg[temp3];
_delay_us(100);
PORTA=~BIT(0);
PORTB=seg[temp4];
_delay_us(100);
}
/***********************管腳初始化函數*********************/
void init()
{
DDRD|=0X40; //PD4 設置為輸出
PORTD=0XBF;
DDRA=0XFF;
DDRB=0XFF;
PORTA=0XFF;
PORTB=0XFF;
}
/***********************外部中斷0初始化*********************/
void init_0()
{
MCUCR=0X02; //INT0為下降沿觸發
GICR=0X40; //使能INT0中斷
SREG=0X80; //使能總中斷
}
/**********************定時器2初始化***********************/
void init_2()
{
TCCR0=0x03; // 內部時鐘,64 分頻(8M/64=125KHz)
TCNT0=0x83; //裝初值
TIMSK=0x01; // 允許 T/C0溢出中斷中斷
}
/***********************外部中斷0子函數********************/
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
count++;
}
/*********************定時計數器0溢出中斷子函數*****************/
#pragma interrupt_handler int0_over:10
void int0_over(void)
{
TCNT0=0x83; //重裝初值
if((time%100) == 0)
sumnum1 = num;
if(time == 1000)
{
sumnum=num;
if(sumnum<10)
{
if((flag==1)&&(sumnum<10))
{
PORTD=0XFF;
flag=0;
}
flag++;
}
else
PORTD=0XBF;
num=0;
time=0;
}
num+=count;
count=0;
++time;
}
標簽:
C語言
上傳時間:
2016-03-09
上傳用戶:彥 yan