⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 link3.c

📁 A small program about finding weekday
💻 C
字号:
#include<stdio.h>

#include<conio.h>

 struct node
{
       int data;
    struct node *next;
};
typedef struct node  node;
node *head = NULL;
node *temp;

void insert_at_start(int n){

	node *temp;             //create a temporary node
temp = (node*)malloc(sizeof(node)); //allocate space for node
temp->data = n;             // store data(first field)
temp->next=head;  // store the address of the pointer head(second field)
head = temp;                  // transfer the address of 'temp' to 'head'
}

void insert_at_end(int n)
{
     node *temp1;                         // create a temporary node
temp1=(node*)malloc(sizeof(node));   // allocate space for node
temp1 = head;                  // transfer the address of 'head' to 'temp1'

while(temp1->next!=NULL) // go to the last node

      temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'

			  // create a temporary node
temp = (node*)malloc(sizeof(node));  // allocate space for node
temp->data = n;                   // store data(first field)
temp->next = NULL;                   // second field will be null(last node)
temp1->next = temp;                  // 'temp' node will be the last node

}

void insert_at_position(int n,int position)
{
     int i;
     node *temp1;
     node *temp; 			// create a temporary node
temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head;

for(  i = 1 ; i < position ; i++ )
{
      temp1 = temp1->next;           // go to the next node

      if( temp1 == NULL )
      {
	    printf(" %d node is not exist",position);
	    break;
      }
}
			 // create a temporary node
temp = (node*)malloc(sizeof(node));  // allocate space for node
temp->data = n;                   // store data(first field)
temp->next = temp1->next;            //transfer the address of temp1->next to temp->next
temp1->next = temp;     //transfer the address of temp to temp1->next
}


int  display()
{
     node *temp1;
     temp1 = (node*)malloc(sizeof(node));
     temp1 = head;
     while( temp1!=NULL )
{
 printf("\n %d",temp1->data) ;// show the data in the linked list
 temp1 = temp1->next;   // tranfer the address of 'temp->next' to 'temp'
}
}

void search_node(int n){
	node* p=head;
	int position=0;

	while (p!=NULL && p->data!=n){
		p=p->next;
		position++;
	}

	if (p!=NULL){
		printf("\n Element %d found at position %d",n,position);
	}
	else {
		printf("\n %d not found",n);
	}

}


void main()
{

int n,p;
clrscr();
/* Function to add the item to the beginning of the linklist */
insert_at_start(40);
insert_at_start(30);
insert_at_start(20);
insert_at_start(10);
printf("Items added at the beginning of the list:\n");

/* Function to display all the items in the linklist*/
display();
//printf("\nPress any key ..");
getch();
/* Function to add item at the end of the linklist */
printf("\n\nItem added at the end of list:\n");
insert_at_end(50);
display();
//printf("\nPress any key ..");
getch();
/* Function to add item at a given location */
printf("\n\nItem added in the middle of the list:\n");
insert_at_position(60,4);
display();
printf("\nPress any key ..");
getch();
/* Function to search a particular node */
search_node(40);
getch();
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -