1. 程式人生 > >結構體與連結串列上機題目 2018年12月13日

結構體與連結串列上機題目 2018年12月13日

結構體與連結串列 2018年12月13日
以下內容僅供娛樂,歡迎隨時探討,請多指教
有如下的結構體:

typedef struct link_list
{
	int num;
	char sex[20];
	char name[30];
	struct link_list *next;
}STU,*linklist;

利用此結構體設計單鏈表,並在單鏈表上完成如下操作:
(1) 建立連結串列。
(2) 輸出連結串列。
(3) 連結串列查詢。
(4) 連結串列插入。
(5) 連結串列刪除。

#include "stdio.h"
#include "string.h"
#include
"stdlib.h"
//結構體 typedef struct link_list { int num; char sex[20]; char name[30]; struct link_list *next; } STU,*linklist; //建立連結串列函式 linklist createlist() { int x; linklist head=NULL,p,q=NULL;//一定要初始化 printf("請輸入學號(負數結束)\n"); scanf("%d",&x); while(x>0) { p=(linklist)malloc(sizeof(STU)); p->
num=x; printf("請輸入性別和姓名\n"); scanf("%s%s",p->sex,p->name); if(head == NULL)head=p; else q->next=p; q=p; printf("請輸入學號(負數結束)\n"); scanf("%d",&x); } if(q)q->next=NULL; return head; } //輸出連結串列函式 void ouput(linklist head) { while(head) { printf("%d\t%s\t%s\n",head->num,
head->sex,head->name); head=head->next; } } //找學號函式 linklist find_num(linklist head,int num) { while(head && head->num!=num) { head=head->next; } return head; } //找位置函式 linklist find_pos(linklist head,int pos) { int i=1; while(head && i<pos) { i++; head=head->next; } return head; } //求長度函式 int length_linklist(linklist head) { int n=0; while(head) { n++; head=head->next; } return n; } //插入函式 linklist insert_linklist(linklist head,STU s,int pos) { linklist p,q; int n=length_linklist(head); if(pos<1 || pos>1+n) return head; p=(linklist)malloc(sizeof(STU)); p->num=s.num; strcpy(p->sex,s.sex); strcpy(p->name,s.name); if(pos == 1) { p->next=head; head=p; } else { q=find_pos(head,pos-1); p->next=q->next; q->next =p; } return head; } //刪除函式 linklist delete_linklist(linklist head,int pos) { int n=length_linklist(head); linklist p,q; if(pos<1 || pos>n) return head; if(pos == 1) { p=head; head=head->next; free(p); } else if(pos == n) { q=find_pos(head,pos); free(q); } else { p=find_pos(head,pos-1); q=find_pos(head,pos); p->next =q->next ; free(q); } return head; } //主函式 int main() { linklist head=createlist(); ouput(head); printf("長度length=%d\n",length_linklist(head)); int ni,posi; printf("輸入查詢的位置pos(越界結束)\n"); scanf("%d",&posi); while(posi>=1 && posi<=length_linklist(head)) { linklist i=find_pos(head,posi); printf("%d\t%s\t%s\n",i->num,i->sex,i->name); printf("輸入查詢的位置pos(越界結束)\n"); scanf("%d",&posi); } printf("輸入查詢的學號num(0 結束)\n"); scanf("%d",&ni); while(ni != 0) { linklist j=find_num(head,ni); printf("%d\t%s\t%s\n",j->num,j->sex,j->name); printf("輸入查詢的學號num(0 結束)\n"); scanf("%d",&ni); } STU s; int num,pos; printf("輸入插入的位置(pos)和學生資訊\n"); scanf("%d%d%s%s",&pos,&s.num,s.sex,s.name); insert_linklist(head,s,pos); ouput(head); printf("輸入刪除的位置(pos)\n"); scanf("%d",&pos); delete_linklist(head,pos); ouput(head); return 0; }

帶頭結點的連結串列

#include "stdio.h"
#include "stdlib.h"
struct student
{
	int age;
	student *next;
};
student *create()
{
	student *head,*p,*q;
	int x;
	head=(student *)malloc(sizeof(student));
	q=head;
	printf("input age:");
	scanf("%d",&x);
	while(x>=0)
	{
		p=(student *)malloc(sizeof(student));
		p->age=x;
		q->next=p;
		q=p;
		scanf("%d",&x);
	}
	q->next=NULL;
	return head;
}
void  print(student *head)
{
	while(head=head->next,head) printf("%5d",head->age);
}
int  Length(student *head)
{
	int s=0;
	while(head=head->next,head) s++;
	return s;
}
student *Find_pos(student *head,int pos)
{
	int s=0;
	while(head && s<pos)
	{
		head=head->next;
		s++;
	}
	return head;
}
void Insert(student *head,int age,int pos)
{
	student *q,*p;
	int n=Length(head);
	if (pos<1|| pos>n+1) return ;
	q=Find_pos(head,pos-1);
	p=(student *)malloc(sizeof(student));;
	p->age=age;
	p->next=q->next;
	q->next=p;
}
student *Find_Value(student *head,int age)
{
	while(head && head->age!=age)
	{
		head=head->next;
	}
	return head;
}
void  Delte_Pos(student *head,int pos)
{
	student *q,*p;
	int n=Length(head);
	if (pos<1 || pos>n) return;
	q=Find_pos(head,pos-1);
	p=q->next;
	q->next=p->next;
	free(p);
}
void  Delte_Value(student *head,int age)
{
	student *r;
	r=head->next;
	while(r!=NULL && r->age!=age)
	{
		head=r;
		r=r->next;
	}
	if (r)
	{
		head->next=r->next;
		free(r);
	}
}
void main()
{
	student *head,*ret;
	int age,pos;
	head=create();
	printf("pos:");
	scanf("%d",&pos);
	//Insert(head,age,pos);
	Delte_Pos(head,pos);
	//ret= Find_Value(head,age);
	//printf("%d\n",ret->age);
	print(head);
}